Tarcisio Wensing
Tarcisio Wensing

Reputation: 442

Call dynamic feature code from base module

im trying to implement a dynamic feature where from my base module i should call some methods in the feature, buts its only possible to call base methods from the feature, is there any way to make feature code accessible from base module? (i understand what can happen when feature is not installed)

I've implemented it using the default google docs, so i have no different code or implementation

Today without dynamic feature i have two .apks and im using communication thru AIDL, im trying to remove AIDL usage and use direct call

Upvotes: 5

Views: 1960

Answers (2)

shubhamgarg1
shubhamgarg1

Reputation: 1995

As you can't have a compile time dependency of the dynamic feature module on the app module:

  1. You need to first ensure the module is installed:

Use the SplitInstallManager's, getInstalledModules() method and check that it exists.

  1. Using reflection, create an instance of the Class which you want to call and call the different methods:

    Class<?> dynamicFeatureClass = Class.forName("packagename.DFClass")
    Constructor<?> cons = dynamicFeatureClass.getConstructor();
    Object dynamicFeatureClassInstance = cons.newInstance();
    

Using reflection, you can call the different methods now.

  1. It would be better if you define a common interface in your base module and implement those methods in your dynamic feature class.

Then you can do:

FeatureContract feature = (FeatureContract) dynamicFeatureClassInstance;

// In the base module.
public interface FeatureContract
{
     void performSomething();
}


// In the dynamic feature module.
DFClass implements FeatureContract
{
     performSomething()
     {
          //code
     }

      //other Class methods
 }

With this approach, you will only need to make a single reflection call to create an instance of your class in the dynamic feature module and after that you will be able to use the compile time benefits as you would be calling the methods using the interface.

Upvotes: 1

Judger Yang
Judger Yang

Reputation: 51

I'm also trying dynamic feature modules.

Here is my approach:

  1. Keep a base abstract class in base app.
  2. Implements the base class in dynamic feature.
  3. Feature feature = (Feature) Class.forName("full.class.name.FeatureImpl").newInstance();

Then you got the instance to call into the feature code.

Upvotes: 5

Related Questions