Adding and using debug build specific dependencies in Android
Android-being the versatile giant it is-has a lot of tools to help us not just in development, but also while debugging the apps. One of such powerful tools is Stetho.
Stetho not only helps us inspect our database and SharedPreferences, but also helps us inspect our network calls. One of the major things which I have faced with Stetho is that I do not want my production apps to have these capabilities (And you should not be okay with that too).
A very simple solution to this would be to do something like this
if(BuildConfig.DEBUG){
Stetho.initializeWithDefaults(this);
}
This solves the problem. But wait if we do so, we still have to ship our app with the dependency for Stetho, which- although it does not contribute much in size-is useless in release builds.

So, here is a simple way by which we can only have debugImplementation of Stetho (and so release builds will not at all include this dependency).
Create a new AndroidManifest file under src->debug like this
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.launcher.android">
<application
android:name=".DebugApp"
tools:replace="android:name">
</application>
</manifest>
Add the debugImplementation of Stetho in your app build.gradle file
debugImplementation 'com.facebook.stetho:$stetho_version'
Create a class named DebugApp (notice that this is the same class I have used in the AndroidManifest file, you might as well want to add the package name corresponding to the class DebugApp).
class DebugApp : App() {
override fun onCreate() {
super.onCreate()
Stetho.initializeWithDefaults(this)
}
}
As we can see the DebugApp extends the App class (the main Application class of our project) so that it automatically inherits all the other things which our application class is supposed to have, along with the additional debug specific changes.
Voila!!. And we are done. Also, this does not just apply to Stetho. We can replace this with any dependency and the story would still be the same.