所以我有一个任务,它读取文件并将其值传递到 BuildConfig 类中传递的属性中,以便可以通过 Android Studio 中的 Java 类访问它。当我单独运行任务时,它会打印出从文件中获取的读取值。但是,当我按“运行”运行我的应用程序时,该属性的值在 BuildConfig 中保持默认值,就像任务从未运行一样。
在项目的 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 中传递了吗?我错过了什么?