Android with Gradle and Proguard

I just created a new Android library project using Gradle and would like the code to be optimized and obfuscated with Proguard.

Here is the android build.gradle part :

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    release {
        runProguard true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.txt'
    }
}

      

When I run gradle build command from terminal it fails at : library: proguardRelease with message:

* What went wrong:
Execution failed for task ':library:proguardRelease'.
> java.io.IOException: The output jar is empty. Did you specify the proper '-keep' options?

      

Does anyone know why this is?

gradle 1.10 JVM 1.6.0_65 Progruard 4.10

+1


source to share


1 answer


The following build.gradle

file works for me with Proguard when executed gradlew assembleRelease

.

Note that it is configured to read keystore information from the config file (and I have included the debug key certificate in the project as it is required for Maps API v2 in debug mode) and passwords from the command line

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
    }

    if (project.hasProperty("secure.properties")
            && new File(project.property("secure.properties")).exists()) {

        Properties props = new Properties()
        props.load(new FileInputStream(file(project.property("secure.properties"))))

        signingConfigs {
            debug {
                storeFile file("gpstest.debug.keystore")
            }

            release {
                storeFile file(props['key.store'])
                keyAlias props['key.alias']
                storePassword "askmelater"
                keyPassword "askmelater"
            }
        }
    } else {
        signingConfigs {
            debug {
                storeFile file("gpstest.debug.keystore")
            }

            release {
                // Nothing here
            }
        }
    }

    buildTypes {
        release {
            runProguard true
            proguardFile 'proguard.cfg'
            signingConfig signingConfigs.release
        }
    } 
}

task askForPasswords << {
    // Must create String because System.readPassword() returns char[]
    // (and assigning that below fails silently)
    def storePw = new String(System.console().readPassword("\nKeystore password: "))
    def keyPw = new String(System.console().readPassword("Key password: "))

    android.signingConfigs.release.storePassword = storePw
    android.signingConfigs.release.keyPassword = keyPw
}

tasks.whenTaskAdded { theTask ->
    if (theTask.name.equals("packageRelease")) {
        theTask.dependsOn "askForPasswords"
    }
}

dependencies {
    compile project(':ShowcaseViewLibrary')
    compile 'com.google.android.gms:play-services:3.2.65'
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile 'org.jraf:android-switch-backport:1.2'
    compile 'com.google.maps.android:android-maps-utils:0.2.1'
}

      

Here are the instructions for setting up config files to read storage information:

To create a release build, you need to create a gradle.properties "file that points to the" secure.properties "file and a" secure.properties "file that points to your keystore and alias. The command gradlew assembleRelease

prompts you for a keyword passphrase.

The gradle.properties "file is in the GPSTest directory and has the following content:

secure.properties=<full_path_to_secure_properties_file>

      



The "secure.properties" file (at the location specified in gradle.properties) has the following content:

key.store=<full_path_to_keystore_file>

key.alias=<key_alias_name>

      

Note that the paths in these files always use the Unix path separator /

, even on Windows. If you are using the Windows path separator \

you will get the errorNo value has been specified for property 'signingConfig.keyAlias'.

Here's the file / project path on Github if you want to clone and test it yourself: https://github.com/barbeau/gpstest/blob/master/GPSTest/build.gradle

proguard.cfg

also located in the GPSTest subdirectory (same directory as build.gradle

): https://github.com/barbeau/gpstest/blob/master/GPSTest/proguard.cfg

+5


source







All Articles