Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android studio gradle server configuration

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

like image 427
user2475442 Avatar asked Jun 11 '13 16:06

user2475442


2 Answers

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":

  • <Module>/src/main/res/values/strings.xml (created by the IDE by default)
  • <Module>/src/debug/res/values/strings.xml (created manually by you)
  • <Module>/src/release/res/values/strings.xml (created manually by you)

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.

like image 107
dab Avatar answered Oct 11 '22 04:10

dab


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" />
like image 25
Oleksandr B Avatar answered Oct 11 '22 04:10

Oleksandr B