You can use a combination of Proguard and Sourcesets to ensure that libraries are not compiled into your application for release, and gradle properties for conditionally applying plugins.
Conditional inclusion of plugins
You can conditionally enable the gradle plugin by declaring it, as usual, at the top of your build.gradle and surrounding it with a conditional clause. For example, the code below checks if a property exists, and if so, uses the plugin.
if (hasProperty('shouldApplyDevMetrics')) { println "Applying devmetrics plugin" apply plugin: 'com.frogermcs.androiddevmetrics' } else { println "Not applying devmetrics plugin in release build" }
To enable the property, you can use the command line flag below when calling gradle. You can create similar launch configurations if you want to use Android Studio.
./gradlew assembleDebug -PshouldApplyDevMetrics=true
This removes the gradle plugin from the release build, but depending on the library, it may leave the compiled code in your application. You can access this using one of the two methods described below.
Using Proguard
The first (and easiest) approach to completely removing the library from the APK is to cut out all the relevant code using the Proguard tool. You must update the release buildType version to enable proguard, and upload the custom rules file.
release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }
By default, this should cross out annotations. You may need to update the proguard configuration for other dependencies that are reflection or annotation dependent. If enabling proguard generates compiler warnings related to Hugo, you can disable them by adding the following line:
-dontwarn hugo.weaving**
This approach means you need to maintain the dependency in the build.gradle file, but this is the best approach for something like Hugo, which is used everywhere, adding annotations.
Using Sourcesets
To completely remove the AndroidDevMetrics library from the release collection, we need to start by creating a debug and release set, then add a functional class under src / debug and a no-op class under src / release.
// src/debug public class DevMetricWrapper() { void doMetricsThings(Context context) { AndroidDevMetrics.initWith(context); } } // src/release public class DevMetricWrapper() { void doMetricsThings(Context context) { // no-op } }
Then you can modify the build.gradle file for your module so that the library is included only as a debugging dependency:
debugCompile 'com.example.yourlibrary'
Please note that if you plan to do something more complex, Dagger is a very useful library as it allows you to enter different dependencies depending on what taste you are building.