1 min read

How to use packages specifically for Debug/Release builds in Android.

How to use packages specifically for Debug/Release builds in Android.
Photo by Mildlee / Unsplash

Preface

Currently, I am working on an Android app for one of the most exciting startups in Fintech. I have been choosy about the packages that are getting shipped with this app simply because it involves a lot of money-related functionalities. During the development, I came across a requirement that debug builds should have Instabug integrated for reporting UI issues quickly. APK size matters greatly, so I wanted to achieve this without shipping Instabug SDK in production builds.

How to do this?

Gradle file

...
dependencies {
    ...
    compile appDependencies.rateUs
    compile appDependencies.markdownJ
    debugCompile(appDependencies.instabug) {
	    exclude group: 'com.mcxiaoke.volley'
    }
}
...  

I changed the way Gradle compiles Instabug dependency. Now it’s done during debug builds only. Release builds will not consider this dependency.

How to use this conditional dependency in code?

Create debug and release folders inside the src folder of your app. Create the same package structure (same as the main folder) in both of these folders. Create a class with overriding nature i.e., same name and methods. Now write add Instabug initialization inside the debug flavor, while the release flavor won’t have this bit. Update your main Application class to include the initialization of this newly created class.

Application class in main.

@Override
public void onCreate() {
    super.onCreate();  
     ...
    SimplAppInitializer.init(this);
    ...
}

And you are done; now the Instabug will be only compiled with the debug builds and not with your production builds.

Let me know if there are some issues/suggestions related to this post. Happy coding \m/