Question

I have an app with a bunch of product flavors which are basically white labels of a single app. However, once in a while, there is some divergence from the main flow in some flavor because the client wants something slightly different. So far, we have been editing the code for those cases and using spaghetti code (lots of ifs and elses) to make sure the other apps don't break. Needless to say this isn't a very scalable (or even sane) way to do this.

One option would be to write the activity classes in the productFlavor source folders i.e. src/flavor1/java/AnActivity.java, src/flavor2/java/AnActivity.java, etc. Since productFlavor code can't override the src/mainclasses, this would require the same classes to be copied over for each new flavor even if there is not customization. I don't really like this option much. It results in a lot of redundant code and class names end up being not so descriptive anymore since they all have to have the same names in order to override others even though they might be doing something different.

Another option could be to use something like Dagger to build an ObjectGraph and inject Intents for different implementations. For example, if it's flavor1, when button X is clicked, an intent for ActivityA is injected and if it's flavor2, an intent for ActivityB is injected.

This seems like a better way to do this but I'm still not sure how to implement classes that would override bindings in the default ObjectGraph.

Any ideas on implementations or other options? I'm not bound to Dagger, I'm just starting to look into dependency injection and testing so other frameworks work just as well.

Was it helpful?

Solution

Here's what I do...

In src/flavor1/java/com/myapp/Modules.java:

public class Modules {
  public static Object[] get(Application app) {
    return new Object[] {
      new MyAppModule(app),
      new Flavor1Module(app)
    };
  }
}

In src/flavor2/java/com/myapp/Modules.java:

public class Modules {
  public static Object[] get(Application app) {
    return new Object[] {
      new MyAppModule(app),
      new Flavor2Module(app)
    };
  }
}

In src/main/java/com/myapp/MyAppApplication.java:

ObjectGraph og = ObjectGraph.create(Modules.get(this));

MyAppModule has common dependencies. Flavor1Module and Flavor2Module can contribute additional dependencies or override ones from MyAppModule if overrides=true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top