Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android studio - How to make two custom application classes

I'd like to design two custom applications for my android project. A custom application is one that extends application. I'd like my release build to have a custom application and the debug build to have another one. LeakCanary site talks about this here in the section labeled "Customizing and using the no-op dependency".

I cant seem to figure out how i would declare another application in the androidManifest. Right now i have it set to use a single custom application like this:

public class MyFirstCustomApplication extends Application {

@Override public void onCreate() {
    super.onCreate();
    LeakCanary.install(this);
}
}

How do i make the above code for debug only ?

and the androidManifest override looks like this:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:name="org.somesite.cool.MyFirstCustomApplication">

How would i have another MySecondCustomApplication for release ?

UPDATE: on the suggestions of looking into bulidTypes in gradle imagine i did the following structure for my debug application override:

[Project Root]
  -[Module]
    -src
      -main
        -ExampleApplication 
        -res

      -debug
        -DebugExampleApplication
        -res

so if i understand buildTypes correctly then if i only have a main folder its used for both release and debug (its the default/fall back) but if i specify a debug folder then if i run any debug flavors i might have like mockDebug,qaDebug, etc then it will first look in the debug folder for the file and then go to main if not found, is that correct ?

Also, then how would i declare in my manifest which one to use ? What would the gradle file look like ?

like image 952
j2emanue Avatar asked Aug 01 '15 22:08

j2emanue


2 Answers

If you need to use two different classes, you can always take advantage of the manifest merger. In the debug folder, add your second application class and a manifest containing only the new app name

<manifest package="your.package"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <application
        android:name=".DebugExampleApplication"
        tools:replace="android:name"/>
</manifest>
like image 179
Jose Luis Benito Esteban Avatar answered Oct 20 '22 12:10

Jose Luis Benito Esteban


I cant seem to figure out how i would declare another application in the androidManifest.

You only have one in the manifest.

so if i understand buildTypes correctly then if i only have a main folder its used for both release and debug (its the default/fall back) but if i specify a debug folder then if i run any debug flavors i might have like mockDebug,qaDebug, etc then it will first look in the debug folder for the file and then go to main if not found, is that correct ?

Assuming that mockDebug is a mock flavor and a debug build type (and likewise for qaDebug), then yes.

So, you have your Application class in the same Java package in the debug and release build types. The manifest, and the Java code using your Application class, will refer to it in that one Java package. Which edition of your Application class you get will depend on whether you are doing a debug build or a release build.

Also, then how would i declare in my manifest which one to use ?

The same as normal.

What would the gradle file look like ?

No changes are needed for any Gradle build files for this.

on the suggestions of looking into bulidTypes in gradle imagine i did the following structure for my debug application override

No, at least as how I would view the project tree. You would have:

[Project Root]
  -[Module]
    -src
      -main
        -java
          - your.package.here
            - Things.java
            - ThatAre.java
            - NotYourCustomApplication.java
            - ButOtherwiseAreCommon.java
            - ForAllBuildTypes.java
        -res

      -debug
        -java
          - your.package.here
            - YourCustomApplication.java
        -res

      -release
        -java
          - your.package.here
            - YourCustomApplication.java
        -res

Note that it is the same your.package.here and the same YourCustomApplication.java in each of the build type-specific sourcesets.

like image 41
CommonsWare Avatar answered Oct 20 '22 12:10

CommonsWare