关于这个问题我有两个问题。
如果在 laravel/web 中,我们有
.env
文件将环境设置为“开发”或生产并自动连接到不同的数据库。在 android/kotlin/android studio 中怎么样?以及如何在 PC 上向我的本地主机 (127.0.2.1) 发出应用程序请求(如果处于“开发”环境),以及如何请求真实的 url API(如果处于“生产”环境)。仅供参考,我不使用模拟器。我用手机来测试我的应用程序。
是的,这在您的 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 帮助我了解了不同构建变体的设置。因此,我与您分享这一点。希望有帮助。
在应用程序级别的 build.gradle 文件中添加
android {
flavorDimensions "full"
productFlavors {
production {
versionCode 17
versionName "1.1.0"
dimension "full"
}
develop {
applicationId "com.packagename.app"
versionCode 17
versionName "1.1.0"
dimension "full"
}
}
如果设置不同的包名 添加这一行 applicationId“com.packagename.app”
并在同步构建后检查构建变体