简体   繁体   English

通过Jenkins在Android Gradle中设置构建属性

[英]Setting build properties in Android Gradle via Jenkins

How do you tell Jenkins to set a property inside gradle.properties in an Android project? 你如何告诉Jenkins在Android项目的gradle.properties中设置属性? I've figured out how to get the properties from Gradle into Jenkins (by using the EnvInject plugin and simply entering gradle.properties into the property file field) but I also want the build number that is managed by Jenkins to be injected into the build artifact. 我已经想出如何从Gradle获取属性到Jenkins(通过使用EnvInject插件并简单地将gradle.properties输入到属性文件字段中)但我还希望将由Jenkins管理的内部版本号注入到构建中神器。

I also want to set the filename of the resulting artifact which, I think, needs to be injected by using archivesBaseName . 我还想设置生成的工件的文件名,我认为需要使用archivesBaseName注入。 But that property isn't part of gradle.properties so I wonder how do I access it? 但是该属性不是gradle.properties一部分,所以我想知道如何访问它?

So far I've only found solutions that change the build.gradle file (or other gradle scripts) in the Android project itself. 到目前为止,我只找到了改变Android项目本身的build.gradle文件(或其他gradle脚本)的解决方案。 But that's not what I want to do because that would make the Android code base rely on Jenkins. 但这不是我想要做的,因为这会使Android代码库依赖于Jenkins。

Instead I want Jenkins to provide the build number and artifact file name to the Android project before it compiles the code. 相反,我希望Jenkins在编译代码之前为Android项目提供构建号和工件文件名。

The server runs on a Mac. 服务器在Mac上运行。 Does anyone have a solution for this? 有人有解决方案吗? Any shell/Groovy script that does the job would be welcome. 任何完成这项工作的shell / Groovy脚本都是受欢迎的。

We are just wrinting a file version.properties, which is generated by jenkins "echo "VERSION_CODE=${BUILD_NUBMER}" > version.properies" script. 我们只是在写一个文件version.properties,它是由jenkins“echo”VERSION_CODE = $ {BUILD_NUBMER}“> version.properies”脚本生成的。

Then gradle script imports it as following: 然后gradle脚本将其导入如下:

// Version code is loaded from a properties file
        def versionPropsFile = file('version.properties')
        if (versionPropsFile.canRead()) {
            def Properties versionProps = new Properties()
            versionProps.load(new FileInputStream(versionPropsFile))

            def code = versionProps['VERSION_CODE'].toInteger()

            versionCode code
        }
        else {
            throw new GradleException("Could not read version.properties!")
        }

        buildConfigField "String", " some _VERSION", "\"${getVersionString()}\""
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM