Pregunta

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:

  1. Define An AndroidManifest.xml file in the root of src/debug with an alternate definition of the <application> element. Gradle warns me that Main manifest has X but library has Y and the DebugMyApplication isn't used.
  2. Declare the Application subclass's fully qualified name as a resource which can be defined separately in my debug res files, but <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!

¿Fue útil?

Solución

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

Otros consejos

You can add an Application subclass for your debug type by creating a MyDebugApplication class and adding it to the DEBUG source folder, and setting

<application
    android:name="com.example.MyDebugApplication"
    tools:replace="android:name">
</application>

as the application tag in your AndroidManifest.xml file in the DEBUG source folder.

If you already have a Application subclass, MyApplication, in your main source set, you can make MyDebugApplication extend MyApplication.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top