在构建期间无法在 Java 中创建 gradle 任务传递值
Can't make gradle task pass value in Java during build time
所以我有一个任务读取文件并将其值传递到 属性 中,该 属性 在 BuildConfig class 中传递,以便可以通过 Java classes 在 Android Studio 中。当我 运行 单独执行任务时,它会打印出从文件中读取的值。但是,当我按 "Run" 到 运行 我的应用程序时,那个 属性 的值在 BuildConfig 中保持默认值,就像任务永远不会 运行.
INSIDE build.gradle 项目:
task testVariableInsertion() {
doLast {
File file = file('.gitignore')
println domainName
domainName = file.text;
println domainName;
project.logger.info('task runned NOW!!!!!!!!!!!!!')
}
}
模块 'app' 的 build.gradle 内部:
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.testingproject"
minSdkVersion 24
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
buildConfigField "String", "DOMAIN_NAME", "${domainName}"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
期望 gradle 会自动 运行 任务,这似乎是这种情况,因为如果我在构建后键入 "gradlew --info" 就会收到日志消息。是因为它读取文件并且在读取完成时值已经传递到 BuildConfig 中了吗?我错过了什么?
几天前找到了答案,为了让它工作,我必须这样定义我的任务:
def testVariableInsertion = {
File file = file('.gitignore')
println domainName
domainName = file.text;
println domainName;
project.logger.info('task runned NOW!!!!!!!!!!!!!')
}
并在下面用
调用它
testVariableInsertion()
我在 'ext' 括号内也有我的变量域名
ext {
domainname = "defaultValue"
}
然后在app模块的defaultConfig中:
buildConfigField "String", "DOMAIN_NAME", rootProject.ext.domainName
所以我有一个任务读取文件并将其值传递到 属性 中,该 属性 在 BuildConfig class 中传递,以便可以通过 Java classes 在 Android Studio 中。当我 运行 单独执行任务时,它会打印出从文件中读取的值。但是,当我按 "Run" 到 运行 我的应用程序时,那个 属性 的值在 BuildConfig 中保持默认值,就像任务永远不会 运行.
INSIDE build.gradle 项目:
task testVariableInsertion() {
doLast {
File file = file('.gitignore')
println domainName
domainName = file.text;
println domainName;
project.logger.info('task runned NOW!!!!!!!!!!!!!')
}
}
模块 'app' 的 build.gradle 内部:
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.testingproject"
minSdkVersion 24
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
buildConfigField "String", "DOMAIN_NAME", "${domainName}"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
期望 gradle 会自动 运行 任务,这似乎是这种情况,因为如果我在构建后键入 "gradlew --info" 就会收到日志消息。是因为它读取文件并且在读取完成时值已经传递到 BuildConfig 中了吗?我错过了什么?
几天前找到了答案,为了让它工作,我必须这样定义我的任务:
def testVariableInsertion = {
File file = file('.gitignore')
println domainName
domainName = file.text;
println domainName;
project.logger.info('task runned NOW!!!!!!!!!!!!!')
}
并在下面用
调用它testVariableInsertion()
我在 'ext' 括号内也有我的变量域名
ext {
domainname = "defaultValue"
}
然后在app模块的defaultConfig中:
buildConfigField "String", "DOMAIN_NAME", rootProject.ext.domainName