I want to automate the parsing and saving of a json object to asset or raw directory during gradle build. I have a java Json parsing class and I know how to run it from gradle. What i do not know is how to save the results or that class to either of the above folders. Below is an example of how i will be running the script. Is what i am trying to do possible in its current state??
package com.mrhaki.java;
public class SimpleParser {
public static void main(String[] args) {
//parse content
}
}
Gradle Build
apply plugin: 'java'
task(runSimpleParser, dependsOn: 'classes', type: JavaExec) {
main = 'com.mrhaki.java.SimpleParser'
classpath = sourceSets.main.runtimeClasspath
args 'mrhaki'
systemProperty 'simpleparser.message', 'Hello '
}
defaultTasks 'runSimpleParser'
In Eclipse, select File > Export. In the window that appears, open Android and select Generate Gradle build files. Select the project you want to export for Android Studio and click Finish. Your selected project remains in the same location but now contains a build.
The top-level build.gradle file, located in the root project directory, defines dependencies that apply to all modules in your project. By default, the top-level build file uses the plugins block to define the Gradle dependencies that are common to all modules in the project.
It is popular for its ability to build automation in languages like Java, Scala, Android, C/C++, and Groovy. The tool supports groovy based Domain Specific Language over XML. Gradle provides building, testing, and deploying software on several platforms. The tool is popular for building any software and large projects.
I thinks that there's no need to use external JSON parser. Instead use JsonSlurper
. Works really well. In the task above, create a file, write the parsed content there and save it in the declared folder. That's all. What exactly You don't know?
It will be similar to:
task json() << {
def f1 = new File('path/to/file1')
def f2 = new File('path/to/file2')
f1.text //= set content here
f2.text //= set content here
}
That's all as far as I understood.
Just for a slightly more detailed answer, I had to do something similar recently, iterating over a simple json file and generating a strings.xml pre-build. The relevant bit from build.gradle:
import groovy.json.JsonSlurper
task generateStrings {
def inputFile = new File("app/src/main/assets/localized_strings.json")
def json = new JsonSlurper().parseText(inputFile.text)
def sw = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(sw)
//add json values to the xml builder
xml.resources() {
json.each {
k, v ->
string(name: "${k}", "${v}")
}
}
def stringsFile = new File("app/src/main/res/values/strings.xml")
stringsFile.write(sw.toString())
}
gradle.projectsEvaluated {
preBuild.dependsOn('generateStrings')
}
http://www.veltema.jp/2014/08/27/generating-strings.xml-from-JSON-at-Build-in-Android-Studio/
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