是的,这在您的 Android 应用程序中也是可能的。您只需修改您的build.gradle 文件,即可根据您的开发、测试或生产环境管理您的BuildConfig。
这是来自我的一个项目的示例 build.gradle 文件。
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
def keystorePropertiesFile = rootProject.file("../Path_To_KeyStore/keystore.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
def appPropertiesFile = rootProject.file("app-settings.properties")
def appProperties = new Properties()
appProperties.load(new FileInputStream(appPropertiesFile))
android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
signingConfigs {
MyAppSigningConfig {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}
}
defaultConfig {
applicationId "com.example.myapp"
minSdkVersion 21
targetSdkVersion 27
versionCode appProperties['app.version.code'] as int
versionName appProperties['app.version.name']
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
def buildVariant = getBuildVariant()
def environmentPath
if ((buildVariant == "Release")) {
environmentPath = appProperties["env.path.live"]
} else if ((buildVariant == "Debug")) {
environmentPath = appProperties["env.path.test"]
} else {
environmentPath = appProperties["env.path.live"]
}
def envPropertiesFile = rootProject.file(environmentPath)
def envProperties = new Properties()
envProperties.load(new FileInputStream(envPropertiesFile))
println("buildVariant = $buildVariant")
for (String key : envProperties.keySet()) {
buildConfigField "String", key.replaceAll("\\.", "_").toUpperCase(), envProperties[key]
}
}
buildTypes {
debug {
applicationIdSuffix ".debug"
manifestPlaceholders = [appName: "@string/app_name_debug_test"]
}
release {
manifestPlaceholders = [appName: "@string/app_name"]
signingConfig signingConfigs.MyAppSigningConfig
minifyEnabled false
multiDexEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
def getBuildVariant() {
for (TaskExecutionRequest t : gradle.getStartParameter().getTaskRequests()) {
for (String command : t.args) {
if (command.matches(":app:generate(.*)Sources")) {
return command.replaceAll(":app:generate(.*)Sources", "\$1")
} else if (command.matches(":app:assemble(.*)")) {
return command.replaceAll(":app:assemble(.*)", "\$1")
}
}
}
return "Release"
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.android.support:recyclerview-v7:27.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
我在这里有两种不同的构建变体。一个用于发布,另一个用于调试。我在应用程序目录中有三个属性文件。这些如下。
app-settings.properties 文件如下所示。
app.version.code=1
app.version.name=0.0.1
env.path.live=live-env.properties
env.path.test=test-env.properties
test-env.properties 看起来像
base.url.auth="http://localhost:8888/auth/"
base.url.communication="http://localhost:8000/communication/"
base.url.site="http://localhost:8000/"
api.key.auth="demo_key"
live-env.properties 就像
base.url.auth="http://auth.yourapp.com/auth/"
base.url.communication="http://yourapp.com/communication/"
base.url.site="http://yourapp.com/"
api.key.auth="live_key1223ssHHddSSYYY"
因此,一旦设置了build.gradle 和应用程序属性,您需要与 gradle 同步以生成BuildConfig.java 文件。您将看到该文件是使用从您的属性文件中找到的值自动生成的。
您可以从代码中的任何位置访问环境变量,如下所示。
const val BASE_URL = BuildConfig.BASE_URL_SITE
const val BASE_URL_AUTH = BuildConfig.BASE_URL_AUTH
从 Android Studio 中构建变体的左侧菜单中获取所需的应用构建。
我的一位名叫 Sajid Shahriar 的同事帮助我了解了不同构建变体的设置。因此,我与您分享这个。希望对您有所帮助。