Global Config signature for multi-project build

I use the project structure as follows:

root |--build.gradle |--settings.gradle | |--child1 | |--build.gradle | |--child2 |--build.gradle 

In both child projects, build.gradle contains the following:

 apply plugin: 'android' ... android { ... signingConfigs { release { storeFile file("release.keystore") storePassword "pass" keyAlias "alias" keyPassword "pass" } } buildTypes { release { signingConfig signingConfigs.release } } ... } 

How can I move the signatureConfigs component to the root project to reduce code duplication?

+6
source share
3 answers

Given that all subprojects are Android projects, you can move the code to the subprojects configuration block defined in the root level of the build script.

 subprojects { apply plugin: 'android' android { ... } } 

If you need, you can also filter the list of projects based on certain criteria and apply this configuration to them.

+1
source

I tried this code and it works. Keep in mind that do not use signConfigs in an android library project.

 subprojects { afterEvaluate { def isAndroidProject = project.plugins.hasPlugin('android') def isLibraryProject = project.plugins.hasPlugin('android-library') if (isAndroidProject || isLibraryProject) { android { compileSdkVersion 19 buildToolsVersion 19.0.1 compileOptions { sourceCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7 } if (isAndroidProject) { signingConfigs { release { storeFile file("${rootDir}/platform.keystore") storePassword "android" keyAlias "platform" keyPassword "android" } } buildTypes { release { signingConfig signingConfigs.release minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), "${project.projectDir}/proguard-project.txt" } } android.applicationVariants.all { variant -> variant.outputs.each { output -> def buildType = variant.buildType.name if (buildType.equals('release')) { def fileName = "${project.name}.apk"; output.outputFile = new File(output.outputFile.parent, fileName) output.assemble.doLast { copy { from output.outputFile.getAbsolutePath() into "../apks/" } } } } } } } } } 
+1
source

I understand this question is old, but for those who have the same problem, I finally found this configuration that works for us:

In the root directory of build.gradle :

 ext { signingConfigs = { release { storeFile rootProject.file('keystores/release.keystore') storePassword 'pass' keyAlias 'alias' keyPassword 'pass' } // Optionally other configs like 'debug' here... } // Other ext properties here... } 

Then, in separate build.gradle modules build.gradle files (except library modules):

 android { // ... signingConfigs rootProject.ext.signingConfigs } buildTypes { release { signingConfig signingConfigs.release // ... } } 

Thus, at least you do not need to duplicate the password / store name everywhere. It also makes it easier to switch to using environment variables for passwords, which are good practice.

-1
source

All Articles