Warning: Declaration of Bootstrap_Walker_Nav_Menu::start_lvl(&$output, $depth) should be compatible with Walker_Nav_Menu::start_lvl(&$output, $depth = 0, $args = Array) in /homepages/10/d412081534/htdocs/clickandbuilds/WordPress/wordpress/wp-content/themes/stanleywp/functions/function-extras.php on line 61
Gradle | buildpath.de

Different keys for Debug and Release Build

In lots of projects i had the problem, that i need different key for a live environment and the environment during the development. for example currently i use parse.com for my cloud backend. there is a live instance (with application_id and client_id) and a development instance. until now all the time i had to switch the instances by copy the right key in the string.xml file. in this post u can see my mechanism for that by using gradle.

the following code is for initialize the parse framework in my android project

Parse.initialize(this, getString(R.string.parse_application_id), getString(R.string.parse_client_key));

the keys for the application_id and the client_key are in the string.xml file. at the beginning they are empty. looks strange but later this will make sense ;)

<?xml version="1.0" encoding="utf-8"?>
<resources>

 <string name="parse_application_id"></string>
 <string name="parse_client_key"></string>

</resources>

the following code is my gradle.build file for my android application

apply plugin: 'android'

android {
 ...
 defaultConfig {
 ...
 }
 signingConfigs {
 ...
 }
 buildTypes {
 release {
 ...

 }
 debug {
 ...
 }
 }
}
dependencies {
 ...
}

android.applicationVariants.all { variant ->
   variant.mergeResources.dependsOn {
      overrideParseKey(variant)
   }
}

def overrideParseKey(def buildVariant) {
 def xmlStringsFileAsNode = new XmlParser().parse("app/src/main/res/values/strings.xml")

 xmlStringsFileAsNode.each {

    // debug
    if (buildVariant.name.equals("debug")) {
       if (it.attribute("name").equals("parse_application_id")) {
          it.setValue("DEBUG_PARSE_APPLICATION_ID")
       }
       if (it.attribute("name").equals("parse_client_key")) {
          it.setValue("DEBUG_PARSE_CLIENT_ID")
       }
    }

    // release
    if (buildVariant.name.equals("release")) {
       if (it.attribute("name").equals("parse_application_id")) {
          it.setValue("RELEASE_PARSE_APPLICATION_ID")
       }
       if (it.attribute("name").equals("parse_client_key")) {
          it.setValue("RELEASE_PARSE_CLIENT_ID")
       }
    }

    def fileWriter = new FileWriter("app/src/main/res/values/strings.xml")
    def printWriter = new PrintWriter(fileWriter)
    printWriter.print("""<?xml version="1.0" encoding="utf-8"?>\n""")
    def xmlPrinter = new XmlNodePrinter(printWriter)
    xmlPrinter.setPreserveWhitespace(true)
    xmlPrinter.print(xmlStringsFileAsNode)
 }
 createOverrideDirIfNecesarry()
}

def createOverrideDirIfNecesarry() {
   def file = new File("app/src/main/res/values/")
   if (!file.exists()) {
      file.mkdirs()
   }
}

so what happen in this gradle script? at first u can see 2 build types (release and debug). i build the release apk by using gradle assembleRelease with the signingConfigs. the debug type is used by me with gradle assembleDebug and the debug type is also used by the android studio by default!

if u rebuild your project (android studio will do this also automatically) –> build –> rebuild project, the gradle script will be executed. the script will replace the empty entries in the string.xml file withe the debug keys!!! is that cool? yes it is. and if you build the application with gradle assembleRelease the script will replace the entries with the release keys!