Tuesday, June 9, 2015

My build.gradle configuration

When inspecting projects of other people, I tend to peek into build.gradle file to look for various useful tweaks. I have gathered a few and now I want to share it with all of you. If you have some useful tweaks of yours not mentioned here, don't hesitate to share them in the comments section.
//keep your version divided into 4 parts, this way it's clearer to understand, than having only one number
def versionMajor = 0
def versionMinor = 1
def versionPatch = 0
def versionBuild = 0 

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    //standard gradle naming says almost nothing about your build; this way you can easily distinguish between different builds and versions
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.outputFile = new File(
                    output.outputFile.parent,
                    "$project.name-$variant.name-$variant.versionName-${variant.versionCode}.apk")
        }
    }

    //define your release config, so you can easily build and run release builds; this way you can avoid painful build->generate signed apk procedure
    signingConfigs {
        release {
            storeFile file(RELEASE_STORE_FILE)
            storePassword RELEASE_STORE_PASSWORD
            keyAlias RELEASE_KEY_ALIAS
            keyPassword RELEASE_KEY_PASSWORD
        }
    }

    defaultConfig {
        applicationId "cz.droidboy.whatever"
        minSdkVersion 15 //minimal api level of 15 is sufficient
        targetSdkVersion 22 //always target the latest sdk version

        //keep resources for listed configs only; this way you can reduce apk size by removing unnecessary resources from libraries
        resConfigs "cs", "sk", "en" //keep resources only for listed languages
        resConfigs "hdpi", "xhdpi", "xxhdpi", "xxxhdpi" //since latest phones have insanely high dpis, there's no need to keep resources for those lower than hdpi

        versionCode versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild
        versionName "${versionMajor}.${versionMinor}.${versionPatch}"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    buildTypes {

        release {
            minifyEnabled false // I don't use proguard
            signingConfig signingConfigs.release
        }

        //it's a good practice to keep debug and release builds distinguishable form each other; I also change debug build's icon to visually distinguish those 2; this way you can also have installed both versions simultaneously
        debug {
            applicationIdSuffix '.debug' 
        }
    }

    lintOptions {
        textReport true // if true, generate a text report of issues (false by default)
        textOutput 'stdout' // location to write the output; can be a file or 'stdout' (standard output)
    }
}

dependencies {}
Local gradle.properties file, which isn't under version control. Define your keystore credentials here.
RELEASE_STORE_FILE=path_to_the_file #use \ on windows, / on Linux/Mac
RELEASE_STORE_PASSWORD=password
RELEASE_KEY_ALIAS=key_alias
RELEASE_KEY_PASSWORD=key_password

0 comments:

Post a Comment