2017-08-05 82 views
0

在CI服务器,我想摆脱开发商已经在文件的build.gradle设置一个Android项目signingConfigs的:Android项目 - 我可以使用init.gradle初始化脚本覆盖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初始化脚本中写入的signedConfig替换它们。我想用here(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?这可行吗?这是解决我需求的有效方法吗?先谢谢您的帮助!

回答

0

为了解决这个问题,我不得不考虑Gradle构建生命周期分三个阶段进行:初始化,配置和执行。分配给发行版buildTypesigningConfig必须在配置阶段结束时发生,这意味着构建已评估完所有项目属性和任务后仍可用于修改。

Gradle提供了项目挂钩,它允许在配置阶段结束时执行一段代码,其中所有项目属性和任务已经设置,并且在执行阶段之前它们仍然可以修改开始。

使用这种钩来在方便的在初始化脚本,如下列:

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 

执行命令gradle -I init.gradle clean assembleRelease时,它导致了成功的Android构建的是解决了我的需求:

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 

希望这可以帮助!

相关问题