[英]Setting up development and production environment for android application in Android Studio
關於這個問題,我有 2 個問題。
如果在 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 幫助我了解了不同構建變體的設置。 因此,我將此與您分享。 希望有所幫助。
添加 inside inside app level build.gradle 文件
android { flavorDimensions "full" productFlavors { production { versionCode 17 versionName "1.1.0" dimension "full" } develop { applicationId "dev.kadepay.app" versionCode 17 versionName "1.1.0" dimension "full" } }
如果設置不同的 package 名稱 添加此行 applicationId "dev.kadepay.app"
並在同步構建后檢查構建變體
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.