Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding release keys in the experimental Gradle plugin for Android

Hey I am having some issues adding a signing my release build variant. Currently I am using the experimental gradle 2.5 with the new android gradle plugin version 0.1.0.

build.gradle:

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 15
        buildToolsVersion = "22.0.1"

        defaultConfig.with {
            applicationId = "com.testcom.test"
            minSdkVersion.apiLevel = 14
            targetSdkVersion.apiLevel = 14
            versionCode = 1
            versionName = "1.0"
        }

        compileOptions.with {
            sourceCompatibility JavaVersion.VERSION_1_6
            targetCompatibility JavaVersion.VERSION_1_6
        }
    }

    android.ndk {
        ...
    }

    android.signingConfigs {
        signed {
            keyAlias = "meow"
            keyPassword = "**"
            storeFile = file("meow-key.keystore")
            storePassword = "**"
        }
    }

    android.buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles += file('proguard-rules.pro')
            signingConfig  = signingConfigs.signed
        }
        debug {
            isDebuggable = true
            isJniDebuggable = true
        }
    }

    // You can modify the NDK configuration for each variant.
    components.android {
        binaries.afterEach { binary ->
            binary.mergedNdkConfig.cppFlags.add(
                    "-DVARIANT=\"" + binary.name + "\"")
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

My root build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle-experimental:0.1.0'
    }
}

allprojects {

    repositories {
        jcenter()
    }
}

The error that I am getting when running ./gradlew assembleRelease is:

> Exception thrown while executing model rule: model.android.buildTypes > named(release)
   > Attempt to read a write only view of model of type 'java.lang.Object' given to rule 'model.android.buildTypes'

Has anyone had a similar issue with the experimental gradle plugin? Help would be greatly appreciated. :)

like image 294
canuc Avatar asked Jan 07 '23 17:01

canuc


2 Answers

This workaround works for me and does not require -Dorg.gradle.model.dsl=true

model {
    def signConf

    android.buildTypes {
        release {
            signingConfig = signConf
        }
    }

    android.signingConfigs {
        create("signed") {
            keyAlias = "meow"
            keyPassword = "**"
            storeFile = file("meow-key.keystore")
            storePassword = "**"
            storeType = "jks"

            signConf = it
        }
    }
}

However, it only works if you only have one signingConfig.

like image 140
Carlos Hoyos Avatar answered Jan 11 '23 01:01

Carlos Hoyos


You should be able to add the release keys with a script like this:

model {
    android.buildTypes {
        release {
            signingConfig = $("android.signingConfigs.signed")
        }
    }

    android.signingConfigs {
        create("signed") {
            keyAlias = "meow"
            keyPassword = "**"
            storeFile = file("meow-key.keystore")
            storePassword = "**"
            storeType = "jks"
        }
    }
}

Currently it seems to be a bug in the plugin.
You need to specify -Dorg.gradle.model.dsl=true when you run your gradle command.

Also you should have an issue with proguard. In this case you can use new File("path/to/proguard-rules.pro") instead of file('proguard-rules.pro')

like image 41
Gabriele Mariotti Avatar answered Jan 11 '23 01:01

Gabriele Mariotti