Reputation: 442
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
Reputation: 1995
As you can't have a compile time dependency of the dynamic feature module on the app module:
Use the SplitInstallManager's, getInstalledModules() method and check that it exists.
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.
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
Reputation: 51
I'm also trying dynamic feature modules.
Here is my approach:
Feature feature = (Feature) Class.forName("full.class.name.FeatureImpl").newInstance();
Then you got the instance to call into the feature code.
Upvotes: 5