我没有覆盖应用程序的答案,但我为您的更广泛目标提出了一个解决方案:
|-- 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);
}
}
请让我知道这对你有没有用。
Jake Wharton 在他的带有 Dagger 的 Android 应用程序演示文稿中介绍了这种技术