从摇篮在构建时设置静态变量 [英] Set static variable from Gradle on build-time

查看:163
本文介绍了从摇篮在构建时设置静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android项目,在git的管理。
Git有2个分支(以及更多......但重要的是那些二),分支A和分支-b中。
是否有可能建立gradle这个以确定哪个分支项目从建成并成立了公共静态字符串在项目的类值取决于分支之一吗?

I've got an Android project, managed under git. Git has 2 branches (well more.. but important are those two), branch-a & branch-b. Is it possible to set up gradle to determine which branch was the project built from and set up the public static String in one of the project's classes to value depending on the branch?

感谢。

推荐答案

是的,你可能有两个分支的(你可以看到属性 gradle.properties 文件这里),containig一些特性,例如 buildBranchName 。但分配给该属性的值将在每个BRACH不同。例如,在brach1你在同一目录下的build.gradle脚本gradle.properties文件,它有一个

Yes, you may have a gradle.properties file in both your branches (you can read about properties here), containig some property, for example buildBranchName. But the value assigned to this property will differ in every brach. For example, in brach1 you have a gradle.properties file in the same directory as your build.gradle script and it has a

buildBranchName=branch1

属性里面。与此同时,gradle.properties在BRANCH2文件包含:

property inside. At the same time, gradle.properties file in branch2 contains:

buildBranchName=branch2

然后,因为你有一个Android项目,你可以使用 BuildConfig 类生成选项给这个变量传递到你的应用程序的来源。你可以阅读一下这里 ,在加强您BuildConfig一节。所有你需要的,是一些属性添加到这个自动生成的类为你打造的类型,就像:

Then, since you have an android project, you can use a BuildConfig class generation option to pass this variable into you application sources. You can read about it here, in "Enhancing your BuildConfig" section. All you need, is to add some property to this autogenerated class for your build types, just like:

android {
    ...
    buildTypes {
        debug {
            buildConfigField "String", "BUILD_BRANCH", buildBranchName
        }
        ...
    }    
}

然后,在构建过程中, BuildConfig 将产生最终的类它将有一个 BUILD_BRANCH 字段,你可以为这个类的一个简单的静态字段中使用。

Then, during the build, the final class BuildConfig will be generated and it will have a BUILD_BRANCH field, which you may use as a simple static field of this class.

此外,如果一些具有非机器人工程,有一个插件时,它允许所有Java项目这样做。

Furthermore, if some has a non-android project, there is a plugin, which allows to do the same for all java-projects.

这篇关于从摇篮在构建时设置静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆