(Android)Read API key from config.xml file
MODIFY
src/android/frameworks/apikey.gradle
src/android/frameworks/apikey.gradle
android {
- def searchPath = 'pgm.properties'
- def finalBuildPropertiesFile = null;
+ def searchPath = 'app/src/main/res/xml/config.xml'
+ def finalConfigXmlFile = null;
for (int i = 0; i < 8; i++) {
- def buildPropertiesFile = file(searchPath)
- println(buildPropertiesFile.absolutePath)
- if(!buildPropertiesFile.canRead()) {
+ def configXmlFile = file(searchPath)
+ println(configXmlFile.absolutePath)
+ if(!configXmlFile.canRead()) {
searchPath = '../' + searchPath;
} else {
- finalBuildPropertiesFile = buildPropertiesFile;
+ finalConfigXmlFile = configXmlFile;
break;
}
}
- if (finalBuildPropertiesFile == null) {
- throw new GradleException("Could not read pgm.properties file.")
+ if (finalConfigXmlFile == null) {
+ throw new GradleException("Could not read config.xml file.")
}
- def props = new Properties()
- finalBuildPropertiesFile.withInputStream { props.load(it) }
+ // parse xml file
+ def widget = new XmlParser().parse(finalConfigXmlFile)
+ if (widget.preference == null) {
+ throw new GradleException("widget is null.")
+ }
+ Properties props = new Properties()
+ println(widget.preference[0].attributes().name);
+ def found = 0
+ widget.preference.each { pref ->
+ def name = pref.attributes().name
+ def value = pref.attributes().value
+ if (name == "GOOGLE_MAPS_ANDROID_API_KEY_DEBUG" ||
+ name == "GOOGLE_MAPS_ANDROID_API_KEY_RELEASE") {
+ //println("name = " + name + ", value = " + value);
+ props.setProperty(name, value);
+ found = 1
+ }
+ }
+
+ if (found == 0) {
+ throw new GradleException("You need to declare GOOGLE_MAPS_ANDROID_API_KEY_(DEBUG/RELEASE) in config.xml.")
+ }
buildTypes {
debug {
- resValue "string", "GOOGLE_MAPS_ANDROID_API_KEY", props.getProperty("API_KEY_FOR_ANDROID_DEBUG")
+ resValue "string", "GOOGLE_MAPS_ANDROID_API_KEY", props.getProperty("GOOGLE_MAPS_ANDROID_API_KEY_DEBUG")
}
release {
- resValue "string", "GOOGLE_MAPS_ANDROID_API_KEY", props.getProperty("API_KEY_FOR_ANDROID_RELEASE")
+ resValue "string", "GOOGLE_MAPS_ANDROID_API_KEY", props.getProperty("GOOGLE_MAPS_ANDROID_API_KEY_RELEASE")
}
}

(Android)Read API key from config.xml file