how to communicate in multi module dynamice feature Application

970 views Asked by At

I have project structure like

app :

InstantApp :

library:

dynamic-feature1 :

dynamic-feature2 :

dynamic-feature3 :

Now there is some dependency between dynamic-feature1 and dynamic-feature2. Now if i add it as dependency in dynamic-feature2 build.gradle then it will cause cyclic dependency. Above is just one example there are many other cases too. How to handle such dependency conflicts properly or any suggestion ?

1

There are 1 answers

0
user14678216 On

Communicate from the main app module to a dynamic feature module

You can do this by reflection in Java. Make sure your class and method names are not obfuscated in the dynamic modules.

Get a Fragment/Activity or other class from module:

Class class = Class.forName("your.dynamic.module.package.name.classname");

Get a method from your class:

Method method = class.getMethod("GenerateQuestion");

Invoke the method:

method.invoke(objectYouWantToInvokeTheMethodOn);

Communicate from dynamic feature module to main module

To get a parent activity of a dynamic feature module's fragment you can do:

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    activity = (Activity) context;
}

Then you can call a method on that activity.