Android project - can I override the signingConfigs set in build.gradle using an init.gradle init script?

Viewed 1645

In a CI server I would like to get rid of the signingConfigs the developers have set in the file build.gradle for an Android project:

build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
    }
}
apply plugin: 'com.android.application'

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

android {
    signingConfigs {
        releaseConfig {
            keyAlias 'fake_key_alias'
            keyPassword 'fake_key_pass'
            storeFile file('C:/fake')
            storePassword 'fake_store_pass'
        }
    } 

    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        minSdkVersion 20
        targetSdkVersion 25
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
    buildTypes {
        release {
            zipAlignEnabled true
            signingConfig signingConfigs.releaseConfig
        }
    }
}

Then, I would like to replace them with a signingConfig written in an init.gradle init script placed in the CI server. I would like to use the same technique showed here (Gradle Init script plugins) to replace the repositories but I'm not able to reference the signingConfigs.

init.gradle

apply plugin:EnterpriseSigningConfigPlugin

class EnterpriseSigningConfigPlugin implements Plugin<Gradle> {

    void apply(Gradle gradle) {

        gradle.allprojects{ project ->
            project.android.signingConfigs {

                // Remove all signingConfigs
                all {SigningConfig cfg ->
                        remove cfg
                }

                // add the signingConfig
                signingConfigs {
                        releaseConfig {
                            keyAlias 'CI_key_alias'
                            keyPassword 'CI_key_pass'
                            storeFile file('/CI_storeFile')
                            storePassword 'CI_store_pass'
                        }

                }
            }
        }
    }
}

When I execute the command gradle -I init.gradle clean assembleRelease, I get the following error:

FAILURE: Build failed with an exception.

* Where:
Initialization script 'C:\Users\it056548\.init\init.gradle' line: 11

* What went wrong:
Could not compile initialization script 'C:\Users\it056548\.init\init.gradle'.
> startup failed:
  initialization script 'C:\Users\it056548\.init\init.gradle': 11: unable to resolve class SigningConfig
   @ line 11, column 21.
                     all {SigningConfig cfg ->
                         ^

  1 error


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.721 secs

How should I reference the signingConfigs? Is it feasible? Is this a valid approach to address my need? Thanks in advance for the help!

1 Answers
Related