[英]External reference/variable in build.gradle file
Is it possible to use some external reference or variable in build.gradle files? 是否可以在build.gradle文件中使用某些外部引用或变量?
I have several build.gradle files in my app source files, including the one for module app , module base , module player , etc. (it depends on the structure of your code and the names of your packages). 我的应用程序源文件中有几个build.gradle文件,其中包括用于模块app , 模块库 , 模块播放器等的文件(这取决于代码的结构和包的名称)。
Inside of each of these files is the following or similar structure: 在每个文件的内部都有以下或类似的结构:
defaultConfig {
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0.001"
}
Is there any way I can code this the way that I don't have to change these values in every file? 有什么方法可以不必在每个文件中更改这些值的方式进行编码吗? Is it possible to use some external reference or variable and that way I can edit my versionCode , versionName , etc. just on one place?
是否可以使用一些外部引用或变量,这样我就可以只在一个地方编辑versionCode , versionName等?
In your Project gradle 在您的项目gradle中
ext {
minSdkVersion = 14
targetSdkVersion = 26
compileSdkVersion = 26
buildToolsVersion = '26.0.2'
// App dependencies
supportLibraryVersion = '26.1.0'
mockitoVersion = '1.10.19'
roomVersion = "1.0.0"
}
In your App gradle 在您的应用程式中
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
.
.
}
}
dependencies {
// App's dependencies, including test
compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
}
You can define variables in top level build.gradle
file and then reference these variables in each module's build.gradle
- that way youll be changing them only once in one file. 您可以在顶级
build.gradle
文件中定义变量,然后在每个模块的build.gradle
引用这些变量-这样,您将只在一个文件中更改一次。
to give you an example,this is top level file https://github.com/Ejstn/android-starter/blob/master/build.gradle and this one is module level file: https://github.com/Ejstn/android-starter/blob/master/app/build.gradle 举一个例子,这是顶级文件https://github.com/Ejstn/android-starter/blob/master/build.gradle ,这是模块级文件: https : //github.com/Ejstn/机器人起动/斑点/主/应用程序/的build.gradle
You can also declare whole dependency as variable like in this google's app: https://github.com/google/santa-tracker-android/blob/master/build.gradle 您还可以像在此Google应用程序中一样将整个依赖项声明为变量: https : //github.com/google/santa-tracker-android/blob/master/build.gradle
Go to File/Project structure/app/flavors then you can get versionCode, versionName, etc then change them what you want and it effects all of your Files. 转到File / Project结构/ app / flavors,然后可以获取versionCode,versionName等,然后将其更改为所需内容,这会影响所有文件。
Yes it is. 是的。
In your project-level Gradle config (the one in the root of your project, outside any module folders), you can define variables under the buildscript
block: 在项目级别的Gradle配置中(项目根目录中的那个模块,位于任何模块文件夹之外),您可以在
buildscript
块下定义变量:
ext.thisVersionCode = 1
ext.thisVersionName = "1.0.001"
Then you should be able to reference them from your module-level configs: 然后,您应该能够从模块级配置中引用它们:
defaultConfig {
versionCode = rootProject.ext.thisVersionCode
versionName = rootProject.ext.thisVersionName
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.