Gradle Build

DexGuard can be integrated in the Gradle build process of the Android SDK Tools (version 0.14.0, for Gradle version 2.1).

Setting up the Gradle build

You can enable DexGuard in the Android Gradle build by modifying the build.gradle file of your project:
buildscript {
    repositories {
        flatDir { dirs '/usr/local/DexGuard6.0/lib' }
        mavenCentral()
    }
    dependencies {
        classpath ':dexguard:'
        classpath 'com.android.tools.build:gradle:0.13.0'
    }
}

apply plugin: 'dexguard'

android {
    .....
    buildTypes {
        debug {
            proguardFile getDefaultDexGuardFile('dexguard-debug.pro')
            proguardFile 'dexguard-project.txt'
            proguardFile 'proguard-project.txt'
        }
        release {
            proguardFile getDefaultDexGuardFile('dexguard-release.pro')
            proguardFile 'dexguard-project.txt'
            proguardFile 'proguard-project.txt'
        }
    }
}
Please make sure the repository path in the build script is set correctly for your system. The 'dexguard' plugin replaces the 'android' plugin — it's an extension.

The standard Gradle build process will now use DexGuard's Gradle task. You'll see this in the console output: the line :dexguardRelease in the console replace all lines :dexRelease, :packageRelease, and :zipalignRelease.

You'll most comonly apply DexGuard to final application projects. This is the easiest and most effective approach, since it processes the combined code in a single step.

However, you can also apply DexGuard to library projects, if the library aar file or jar file is the final product:

buildscript {
    repositories {
        flatDir { dirs '/usr/local/DexGuard6.0/lib' }
        mavenCentral()
    }
    dependencies {
        classpath ':dexguard:'
        classpath 'com.android.tools.build:gradle:0.13.0'
    }
}

apply plugin: 'dexguard-library'

android {
    .....
    buildTypes {
        debug {
            runProguard false
            proguardFile getDefaultDexGuardFile('dexguard-library-debug.pro')
            proguardFile 'dexguard-project.txt'
            proguardFile 'proguard-project.txt'
        }
        release {
            runProguard true
            proguardFile getDefaultDexGuardFile('dexguard-library-release.pro')
            proguardFile 'dexguard-project.txt'
            proguardFile 'proguard-project.txt'
        }
    }
}
Note the -library- in the configuration file names.

You should take some care with class encryption, as discussed for the class encryption option. Furthermore, tamper detection is only supported if the final application is also built and packaged using DexGuard.

You can find examples of working projects in the directory samples.

Building

The procedure for building Android applications and libraries remains the same. You can invoke gradle with the usual targets, such as assemble, build, install, and connectedInstrumentTest. For instance, to build the release version of your application and install it on a connected device:
gradle installRelease

To build the release version of a library:

gradle assembleRelease

Debug builds use debug settings, without optimization or obfuscation. Release builds use release settings, with full optimization and obfuscation. Applications can optionally be signed. The entries in application archives are always zip-aligned for efficiency.

Further tips

As before, you can specify some useful options and properties for your Gradle build:

Quick troubleshooting

If you are having problems running the Gradle build itself:

Please consult the more extensive troubleshooting section if you encounter other issues building or running your application.

Reverting to the traditional Gradle build

You can go back to the basic Gradle build of the Android SDK by removing the changes from the build.gradle file of your project.
Copyright © 2002-2014 Saikoa BVBA.