'Setting up development and production environment for android application in Android Studio
I have 2 questions regarding this issue.
if in laravel/web we have
.envfile to set environment to "development" or production and automatically connect to different database. how about in android/kotlin/android studio?and how to make my application request to my localhost (127.0.2.1) on PC if it's in "development" environment and request to real url API if it's in "production" environment. FYI, I dont use emulator. I use my phone to test my application.
Solution 1:[1]
Yes this is possible in your Android application as well. You just have to modify your build.gradle file to manage your BuildConfig based on your dev, test or production environment.
Here's a sample build.gradle file from one of my project.
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'
}
I have two different build variants here. One is for release and the other is for debug. And I have three properties file in the application directory. These are as follows.
The app-settings.properties file looks like this.
app.version.code=1
app.version.name=0.0.1
env.path.live=live-env.properties
env.path.test=test-env.properties
The test-env.properties looks like
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"
And the live-env.properties is like
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"
So once the build.gradle and the application properties are setup, you need to sync with gradle to generate the BuildConfig.java file. You will see the file is generated automatically with the values found from your properties file.
From anywhere in your code, you might access the environment variables like the following.
const val BASE_URL = BuildConfig.BASE_URL_SITE
const val BASE_URL_AUTH = BuildConfig.BASE_URL_AUTH
Get the desired application build from the left side menu of build variants in Android Studio.
One of my colleague named Sajid Shahriar helped me to understand the setup for different build variants. Hence, I am sharing this with you. Hope that helps.
Solution 2:[2]
Add inside inside app level build.gradle file
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" } }
if set up different package name Add this line applicationId "dev.kadepay.app"
and after sync build check build variants
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Reaz Murshed |
| Solution 2 | Sumit Ojha |

