In an effort to limit debug code from making its way into my release APK, I'm attempting to use a DebugMyApplication subclass for debug builds, but the normal MyApplication class in release.
Is it possible to use an alternate Application subclass in Debug vs Release builds? I've tried:
<application> element. Gradle warns me that Main manifest has X but library has Y and the DebugMyApplication isn't used.<application android:name="@string/xyz"/> is interpreted as a literal package name instead of a reference.For some background on my broader goal:
Provide an alternate Dagger module which supplies some static mocked web service responses that will not be found in the release APK. Subclassing my application's getModuleList() method and changing the module list declaration seemed like the best bet.
Thanks for any help!
I haven't the answer for the Application overriding but I propose a solution for your broader goal:
|-- src
| +-- debug
| +-- java
| +-- mypackage
| +-- Modules.java
| +-- MockModule.java
| +-- main
| +-- java
| +-- mypackage
| +-- MainModule.java
| +-- release
| +-- java
| +-- mypackage
| +-- Modules.java
src/debug/java/mypackage/Modules.java
public final class Modules {
public static List<Object> getModules(App application) {
ArrayList<Object> modules = new ArrayList<Object>();
modules.add(new MainModule(application));
modules.add(new MockModule());
return modules;
}
}
src/debug/java/mypackage/MockModule.java
@Module(
overrides = true,
library = true,
injects = { MainActivity.class })
public class MockModule {
@Provides MyType buildMock() {
return //...
}
}
src/release/java/mypackage/Modules.java
public final class Modules {
public static List<Object> getModules(App application) {
ArrayList<Object> modules = new ArrayList<Object>();
modules.add(new MainModule(application));
return modules;
}
}
src/main/java/mypackage/App.java
public class App extends Application {
private ObjectGraph graph;
@Override public void onCreate() {
super.onCreate();
graph = ObjectGraph.create(Modules.getModules(this).toArray());
}
public void inject(Object object) {
graph.inject(object);
}
}
Let me know if it works for you.
This technique was presented by Jake Wharton in his Android Apps with Dagger presentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With