How to make Hugo and AndroidDevMetrics plugins compiled only during debugging:

I use the Jack Wharton Hugo Library and AndroidDevMetrics plugins to measure method execution time and application performance in Android. I need to make these libraries compiled only in the debug build and exclude them from release collections.

Because both libraries only use plugin syntax:

apply plugin: 'com.frogermcs.androiddevmetrics' apply plugin: 'com.jakewharton.hugo' 

and they don’t require any dependencies in the Gradle file, I cannot exclude them using the testCompile parameter. The only way that Hugo controls it is to set this in the gradle file:

 hugo { enabled false } 

whereas the only way to control AndroidDevMetrics is :

 public class ExampleApplication extends Application { @Override public void onCreate() { super.onCreate(); //Use it only in debug builds if (BuildConfig.DEBUG) { AndroidDevMetrics.initWith(this); } } } 

Question: These control settings do not prevent these library files from running in the version version of the application. I am looking for a way to exclude these plugins in gradle if I create a release version.

Thanks in advance.

+7
android android-gradle build.gradle gradle hugo-logging
source share
2 answers

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.

+3
source share

Hugo auto guarantees that:

 project.dependencies { debugCompile 'com.jakewharton.hugo:hugo-runtime:1.2.2-SNAPSHOT' // TODO this should come transitively debugCompile 'org.aspectj:aspectjrt:1.8.6' compile 'com.jakewharton.hugo:hugo-annotations:1.2.2-SNAPSHOT' } 

find here

Only the annotation can do nothing, this is normal.

About AndroidDevMetrics You Can Read Get Started

 public class ExampleApplication extends Application { @Override public void onCreate() { super.onCreate(); //Use it only in debug builds if (BuildConfig.DEBUG) { AndroidDevMetrics.initWith(this); } } } 

It may not be so clean, but it is work. When you start the release, the code is clear.

0
source share

All Articles