[英]Gradle Build - Execution failed for task ':sonarqube' can't be indexed twice
[英]Can't make gradle task pass value in Java during build time
所以我有一個任務讀取文件並將其值傳遞到 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 中傳遞了嗎? 我錯過了什么?
幾天前找到了答案,為了使它起作用,我必須像這樣定義我的任務:
def testVariableInsertion = {
File file = file('.gitignore')
println domainName
domainName = file.text;
println domainName;
project.logger.info('task runned NOW!!!!!!!!!!!!!')
}
並在下面調用它
testVariableInsertion()
我也在“ext”括號內有我的變量 domainName
ext {
domainname = "defaultValue"
}
然后在 app 模塊的 defaultConfig 中:
buildConfigField "String", "DOMAIN_NAME", rootProject.ext.domainName
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.