In Android Studio and using Gradle how do I setup a DEBUG/RELEASE variable. For example when I run my app in DEBUG want my server to be:
SERVER = "http://www.mytestserver.com";
When I run my app in RELEASE I want my server URL to be:
SERVER = "http://www.myproductionserver.com";
How can I do that?
Regards
Start with this link: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Types
Among other things, this section means that a module in an Android Studio project can use separate "debug" and "release" directories of source-code/resources/etc.
So, for example, in a project created by Android Studio (not imported from Eclipse), you can use these files without any additional configuration in "build.gradle":
In the debug directory's strings.xml file, you can define a new string resource such as:
<string name="server_uri">http://www.mytestserver.com</string>
And in the release directory's strings.xml file, you can define the same string resource, but with a different value:
<string name="server_uri">http://www.myproductionserver.com</string>
And these resources are added automatically during the Gradle build to your other string values in the main directory, depending on which build type is used - with no need for any explicit configuration by you.
Strings could be added with Gradle only in app/build.gradle
You can store here: Server Urls, Google Map Api Key, etc.
android {
//...
buildTypes {
release {
resValue "string", "server_uri", "http ...1"
resValue "string", "google_maps_key", "your key"
shrinkResources true
debuggable false
}
releaseStaging {
resValue "string", "server_uri", "http ...2"
resValue "string", "google_maps_key", "your key2"
shrinkResources true
debuggable false
}
debug {
resValue "string", "server_uri", "http ...3"
resValue "string", "google_maps_key", "your key 3"
shrinkResources true
debuggable true
}
}
use:
in code as simple string:
getString(R.string.server_uri);
in manifest:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With