2017-06-22 49 views
0

的build.gradleAPK版本名称 - 詹金斯

compileSdkVersion 23 
buildToolsVersion "25.0.2" 
applicationVariants.all { variant -> 
    def flavor = variant.mergedFlavor 
    if (variant.buildType.isDebuggable()) { 
     flavor.versionName = "Beta Revision: ${svnRevisionDebug()}"; 
     flavor.versionCode = 1; 
    } else { 
     if (project.hasProperty('projVersion')) { 
      println "Assemble release with parameter " + project.projVersion; 
        flavor.versionName = ""+ project.projVersion; 
     } else { 
      flavor.versionName = '10.0.0' 
     } 
     flavor.versionCode = 1; 
    } 
} 

在詹金斯执行shell

./gradlew assembleRelease -PprojVersion=123

詹金斯输出控制台

+ ./gradlew assembleRelease -PprojVersion=123 
Incremental java compilation is an incubating feature. 
Unix runtime 
Assemble release with parameter 123 

输出

Unix runtime

从svnRevisionDebug()

def svnRevisionDebug() { 
    if (System.properties['os.name'].toLowerCase().contains('windows')) { 
     println "Windows runtime" 
     new ByteArrayOutputStream().withStream { os -> 
      def result = exec { 
       executable = 'svn' 
       args = ['info', '-r', 'HEAD'] 
       standardOutput = os 
      } 
      def outputAsString = os.toString() 
      def matchLastChangedRev = outputAsString =~ /Last Changed Rev:(\d+)/ 
      ext.svnRev = "${matchLastChangedRev[0][1]}".toInteger() 
     } 
     return svnRev 
    } else { 
     println "Unix runtime" 
     def p = ['/bin/bash', '-c', /svn info -r HEAD | grep '^Revision:' | sed -e 's\/^Revision: \/\/'/].execute() 
     p.waitFor() 
     return p.text.trim() 
    } 

} 

来了,但,当我从清单反编译的apk我看到

android:versionName="1.0" 

跆拳道? 可能是与创建apk文件时多次调用的applicationVariants.all一起使用的方法(您可以看到jenkins日志“Unix runtime”和“Assemble release with parameter”

哪种方法可以在versionName中注入参数?

回答

0

当传递一个属性作为参数,你应该能够与变量名本身来访问值。

尝试在gradle这个文件来改变project.projVersionprojVersion,应该工作。

含义那里最后生成线将

println "Assemble release with parameter " + projVersion; 
flavor.versionName = "" + projVersion; 

或者你可以尝试

flavor.versionName = projVersion.toString()