今天用buildConfigField设置编译时常量遇到了问题,访问不到BuildConfig类,import导包也找不到类,具体设置如下:
defaultConfig {
applicationId 'com.sample.abc'
minSdk 28
targetSdk 33
versionCode getVerInt()//1
versionName getVerName()//"1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField "String", "Build_ENV", "\"abc\""
buildConfigField "String", "Build_VEHICLE_TYPE", "\"type1\""
}
原因是buildConfigField脚本写在了testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"脚本之后;
写在testInstrumentationRunner语句之前就可以解决了,代码如下:
defaultConfig {
applicationId 'com.sample.abc'
minSdk 28
targetSdk 33
versionCode getVerInt()//1
versionName getVerName()//"1.0"
buildConfigField "String", "Build_ENV", "\"abc\""
buildConfigField "String", "Build_VEHICLE_TYPE", "\"type1\""
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}