好吧,让我们回到基础上来。首先在中创建属性
build.gradle
然后你创造
buildConfigField
使用自定义字段。
productFlavors {
play {
dimension "MyDimension"
Properties versionProps = new Properties()
versionProps.load(new FileInputStream(file('config.properties')))
def properties_versionCode = versionProps['VERSION_CODE'].toInteger()
def properties_versionName = versionProps['VERSION_NAME']
//...
buildConfigField "int", "MY_VERSION_CODE", "$properties_versionCode"
buildConfigField "String", "MY_VERSION_NAME", "\"$properties_versionName\""
}
}
重建项目后,字段将能够从
BuildConfig.{FIELD}
。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//...
Log.d(TAG, "Version Name: " + BuildConfig.MY_VERSION_NAME);
Log.d(TAG, "Version Code: " + BuildConfig.MY_VERSION_CODE);
//...
}
更多信息
here
.