0

I have to create a two or three different build (Android build) using Gradle build system, each build pointing to different server and api key? At present these server url and api key is hard coded in java file. I could build debug and release build properly, but need to generate different build with set of different server url and api key?

Thanks in advance!

Raghu
  • 33
  • 6

2 Answers2

0

(If I understand) I would use an external executable that prepare a batch file for each build.

The .bat file calls gradle passing a variable (conaining the key) to it:

gradle assemble -Pkey=%key%

Now, inside the gradle script you can get the value of the variable:

def varKey = "$key"

and you can use it to sign the apk:

signingConfigs {
    release {
        storeFile = file('../key.keystroke')
        storePassword = varKey 
        keyAlias = "My key"         
        keyPassword = varKey 
    }
}

EDIT: Ok, the previous text is useful to sign the apk file with different keys.

To edit the server url using gradle, first you need to store the url and api key into a resource string and use from inside the java class instead of the hard coded strings:

<resources>
    <string name="server_url">you.server.com</string>
    <string name="api_key">your_api_key</string>
</resources>

now consider the .bat that calls gradle:

gradle assemble -Papi_key=%api_key% -Pserver_url=%server_url%

inside gradle, you get the variable content:

def varApiKey = "$api_key"
def varServerUrl = "$server_url"

Now you need to replace the strings in your resources. I never done it but (for example) you can look at:

How to replace strings resources with Android Gradle

Community
  • 1
  • 1
Seraphim's
  • 12,559
  • 20
  • 88
  • 129
0

I resolved my requirement by creating different build type and configuring different resource for that build type. For example my build.xml look like below:

buildTypes {

    release {
        signingConfig signingConfigs.release
        runProguard true
        proguardFile ('proguard-android-optimize-app.txt')
    }

    server1.initWith(buildTypes.debug)
    server1 {
        packageNameSuffix ".server1"
    }

}

 sourceSets {
    main {
        manifest.srcFile 'app/AndroidManifest.xml'
        java.srcDirs = ['app/src']
        resources.srcDirs = ['app/src']
        renderscript.srcDirs = ['app/src']
        res.srcDirs = ['app/res']
        assets.srcDirs = ['app/assets']
    }
    server1 {
        res.srcDirs = ['app/res/server1']
    }
    instrumentTest.setRoot('tests')
}

Thanks for the support!

Raghu
  • 33
  • 6