[英]Android project - can I override the signingConfigs set in build.gradle using an init.gradle init script?
在CI服務器中,我想擺脫開發人員在Android項目的build.gradle文件中設置的signingConfigs:
的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
}
}
}
然后,我想用放置在CI服務器中的init.gradle初始化腳本中編寫的signingConfig替換它們。 我想使用此處顯示的相同技術(Gradle Init腳本插件)替換存儲庫,但是我無法引用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'
}
}
}
}
}
}
當我執行命令gradle -I init.gradle clean assembleRelease
,出現以下錯誤:
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
我應該如何引用signingConfigs? 可行嗎 這是滿足我需求的有效方法嗎? 先謝謝您的幫助!
為了解決這個問題,我不得不考慮到Gradle構建生命周期發生在三個階段:初始化,配置和執行。 重寫signingConfig
分配給釋放buildType
必須出現在配置階段結束時,生成后,即該評估了所有項目(S)性質和任務,他們仍然可以進行修改。
Gradle提供了項目鈎子project.afterEvaluate()
,它允許在配置階段結束時執行代碼塊,其中所有項目的屬性和任務均已設置,並且在執行階段之前仍可進行修改開始。
在初始化腳本中可以方便地使用此掛鈎,例如:
init.gradle
// Enter the scope of the root project
rootProject{
println ""
println "Project name: ${project.name}"
println ""
// Apply the subsequent code after the configuration phase has finished
afterEvaluate{
//Check if it exixts the properties file for the signing configuration specified in gradle.properties by the property helloWorldAppSigningProperties.
//Then read the signing configuration properties
//If something goes wrong an exception is thrown and the build execution is terminated
if (new File(helloWorldAppSigningProperties).exists()){
println ""
println "Found properties file: ${helloWorldAppSigningProperties}!"
def signingProps = new Properties()
file(helloWorldAppSigningProperties).withInputStream{signingProps.load(it)}
ext{
ka = signingProps.getProperty("keyAlias")
kp = signingProps.getProperty("keyPassword")
sf = signingProps.getProperty("storeFile")
sp = signingProps.getProperty("storePassword")
}
if (ka == null){
throw new GradleException("Property keyAlias not found in file: ${helloWorldAppSigningProperties}")
} else if (kp == null) {
throw new GradleException("Property keyPassword not found in file: ${helloWorldAppSigningProperties}")
} else if (sf == null) {
throw new GradleException("Property storeFile not found in file: ${helloWorldAppSigningProperties}")
} else if (sp == null) {
throw new GradleException("Property storePassword not found in file: ${helloWorldAppSigningProperties}")
}
} else {
throw new GradleException("Properties file: ${helloWorldAppSigningProperties} not found!")
}
//Add a signing configuration named "helloWorldApp_release" to the android build
//Signing configuration properties that were loaded from an external properties file are here assigned
println ""
println "Adding new signingConfig helloWorldApp_release"
android.signingConfigs{
helloWorldApp_release{
keyAlias ka
keyPassword kp
storeFile file(sf)
storePassword sp
}
}
//Display the list of the available signigConfigs
println ""
println "Available signingConfigs:"
android.signingConfigs.all { sc ->
println "------------------"
println sc.name
}
println "------------------"
//Display the list of the available buildTypes
println ""
println "Available buildTypes:"
android.buildTypes.all { bt ->
println "------------------"
println bt.name
}
println "------------------"
println ""
println "SigningConfig assigned to the release buildType BEFORE overriding: ${android.buildTypes.release.signingConfig.name}"
//Set the helloWorldApp_release signingConfig to the release buildType
android.buildTypes.release.signingConfig android.signingConfigs.helloWorldApp_release
println ""
println "SigningConfig assigned to the release buildType AFTER overriding: ${android.buildTypes.release.signingConfig.name}"
println ""
}
}
我將所需的signingConfig屬性外部化到一個文件,該文件在gradle.properties
文件中引用了該文件的位置:
gradle.properties
org.gradle.jvmargs=-Xmx1536M
....
....
....
//Signing config properties files
helloWorldAppSigningProperties=C:\\Users\\it056548\\.signing\\HelloWorldApp.properties
它導致成功的android構建成功執行了gradle -I init.gradle clean assembleRelease
命令, gradle -I init.gradle clean assembleRelease
滿足了我的需求:
Project name: native
NDK is missing a "platforms" directory.
If you are using NDK, verify the ndk.dir is set to a valid NDK directory. It is currently set to C:\android-sdks\ndk-bundle.
If you are not using NDK, unset the NDK variable from ANDROID_NDK_HOME or local.properties to remove this warning.
Found properties file: C:\Users\it056548\.signing\HelloWorldApp.properties!
Adding new signingConfig helloWorldApp_release
Available signingConfigs:
------------------
debug
------------------
helloWorldApp_release
------------------
releaseConfig
------------------
Available buildTypes:
------------------
debug
------------------
release
------------------
SigningConfig assigned to the release buildType BEFORE overriding: releaseConfig
SigningConfig assigned to the release buildType AFTER overriding: helloWorldApp_release
:clean
:preBuild UP-TO-DATE
:preReleaseBuild UP-TO-DATE
:checkReleaseManifest
:prepareReleaseDependencies
:compileReleaseAidl
:compileReleaseRenderscript
:generateReleaseBuildConfig
:generateReleaseResValues
:generateReleaseResources
:mergeReleaseResources
:processReleaseManifest
:processReleaseResources
:generateReleaseSources
:incrementalReleaseJavaCompilationSafeguard
:javaPreCompileRelease
:compileReleaseJavaWithJavac
:compileReleaseJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
:compileReleaseNdk NO-SOURCE
:compileReleaseSources
:lintVitalRelease
:mergeReleaseShaders
:compileReleaseShaders
:generateReleaseAssets
:mergeReleaseAssets
:transformClassesWithDexForRelease
:mergeReleaseJniLibFolders
:transformNativeLibsWithMergeJniLibsForRelease
:processReleaseJavaRes NO-SOURCE
:transformResourcesWithMergeJavaResForRelease
:validateSigningRelease
:packageRelease
:assembleRelease
BUILD SUCCESSFUL
Total time: 8.692 secs
希望這會有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.