android {
compileSdkVersion 28
//开发
def ENVIRONMENT_DEV = "\"https://www.baidu.com/\"";
//生产
def ENVIRONMENT_PRO = "\"https://www.1688.com/\"";
//测试
def ENVIRONMENT_TEST = "\"https://www.amap.com/\"";
//正式
def ENVIRONMENT_OFFICE = "\"https://www.jd.com/\"";
defaultConfig {
applicationId "com.example.myapplication2"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
//Build Type + Product Flavor = Build Variant
flavorDimensions "default"
productFlavors {
//开发环境
dev {
dimension "default"
//在BuildConfig类中生成
buildConfigField "String", "BASE_URL", "${ENVIRONMENT_DEV}"
//供manifest使用
manifestPlaceholders = [app_name: "@string/app_name_dev"]
}
//生产环境
pro {
dimension "default"
//在BuildConfig类中生成
buildConfigField "String", "BASE_URL", "${ENVIRONMENT_PRO}"
//供manifest使用
manifestPlaceholders = [app_name: "@string/app_name_pro"]
}
//测试环境
tst {
dimension "default"
//在BuildConfig类中生成
buildConfigField "String", "BASE_URL", "${ENVIRONMENT_TEST}"
//供manifest使用
manifestPlaceholders = [app_name: "@string/app_name_test"]
}
//正式环境
office {
dimension "default"
//在BuildConfig类中生成
buildConfigField "String", "BASE_URL", "${ENVIRONMENT_OFFICE}"
//供manifest使用
manifestPlaceholders = [app_name: "@string/app_name_office"]
}
}
}
string.xml : 要删除掉app_name
<resources>
<string name="app_name_pro">My pro</string>
<string name="app_name_dev">My dev</string>
<string name="app_name_test">My test</string>
<string name="app_name_office">My office</string>
</resources>
manifest.xml的label标签引入app_name
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="${app_name}"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity可引用生成的BASE_URL :
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv.text = BuildConfig.BASE_URL
}
}