lördag 23 augusti 2014

Android : IntelliJ, Gradle & Signing

IntelliJ IDEA now offers good IDE support for Gradle. We can just go through their 'New Project', select an 'Gradle: Android Module' and project is up and running.



Signing applications need a bit more work though, IDEA points us to Gradle Plugin User Guide.

Default gradle build script in IDEA:
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 20
    buildToolsVersion "19.0.0"

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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
}

We need to add the container signingConfigs { }.
Example build.gradle file:
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 20
    buildToolsVersion "19.0.0"

    signingConfigs {
        debug {
             // Debug - uses default debug keystore
        }

        release {
                storeFile file("C:\\Android\\Key\\keystore")
                storePassword "myPassword"
                keyAlias "key0"
                keyPassword "testPassword"
            }

    }

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguardandroid.txt'), 'proguard-rules.txt'
            signingConfig signingConfigs.release
            zipAlign true
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
}

As you can see we've added a 'signingConfig' flag in the release build type.
You can also add a debug {} under buildTypes if you want to add or change something in the debug build.
Example:
 buildTypes {
        debug {
            println "Building DEBUG release."
            println "Run ProGuard == TRUE"
            runProguard true
        }
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            signingConfig signingConfigs.release
            zipAlign true
        }
    }

As you can see we've enabled Proguard in the debug release.

Run 'PROJECT_HOME\gradlew.bat tasks' for a complete list of different tasks to do or build or open the Gradle tab in IntelliJ IDEA.


I've marked signingReport as it'll display your key status. You can run it directly from IntelliJ IDEA or from your favorite shell.

As you can see we get a complete report of the key store. Wrong password or missing file etc.
When everything is working as it should. Just hit 'gradlew.bat install<yourTask>' to install the signed version of your app on the emulator or a physical device.

I'll update this post as i play more with Gradle!