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'
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/
Related
The configuration cache is enabled in the Gradle settings for the project, yet for this task it reuses the configuration cache anyways. Before I would always use the --no-configuration-cache flag since it would not edit the baseline file on consecutive runs. I updated Gradle and use notCompatibleWithConfigurationCache() in other tasks, yet only for this one it still reuses the configuration cache. How can I fix this? Is there another way I could read/write the file without having to use --no-configuration-cache?
This was the code I was using:
tasks.register("fixBaseline", Copy){
notCompatibleWithConfigurationCache()
List<String> fileList = []
// Read in file list
if (project.hasProperty("directory")) {
// Read in files from directory
String directoryPath = project.property(directoryProperty) as String
def fileNameFinder = new FileNameFinder()
def pathList = fileNameFinder.getFileNames("$rootDir$File.separator$directoryPath", "**/*.kt")
for (pathName in pathList) {
def f = file(pathName)
fileList.add(f.name)
}
}
// Remove lines from baseline
def baselineDir = "$rootDir/config/base"
def baselineFile = file("$rootDir/config/base/baseline.xml")
def outputFile = File.createTempFile("remove", ".tmp", baselineFile.getParentFile())
int linesRemoved = 0
project.logger.lifecycle("Clearing files from baseline...\n")
outputFile.withPrintWriter { outputWriter ->
baselineFile.eachLine { line, lineNumber ->
for (fileName in fileList){
if (line.contains(fileName)){
linesRemoved++
return
}
}
outputWriter.println(line)
}
}
// Rename temporary file to baseline.xml
from baselineDir
include outputFile.name
destinationDir file(baselineDir)
rename outputFile.name, baselineFile.name
project.logger.lifecycle("\nRemoved $linesRemoved lines")
project.logger.lifecycle('----------')
doLast {
project.delete(outputFile)
}
}
I've implemented dokka in top build.gradle
apply plugin: 'org.jetbrains.dokka-android'
dokka {
// These tasks will be used to determine source directories and classpath
// see https://github.com/Kotlin/dokka/blob/master/README.md
kotlinTasks {
defaultKotlinTasks() + [':core:compileDebugKotlin']
}
}
And in core module i have interface that want to be documented:
import com.f2prateek.rx.preferences2.RxSharedPreferences
/**
* Интерфейс для работы с [android.content.SharedPreferences] с помощью [RxSharedPreferences]
*
* #property prefs Свойство для доступа к [android.content.SharedPreferences] через [RxSharedPreferences]
* #see RxSharedPreferences
*/
interface FeaturePrefs {
val prefs: RxSharedPreferences
}
When i run dokka task i got warnings
Can't find node by signature `com.f2prateek.rx.preferences2.RxSharedPreferences`, referenced at my.package.FeaturePrefs (FeaturePrefs.kt:5)
Is there a way to use third party libraries in documentation? I'd like to configure it to reference to github source code (https://github.com/f2prateek/rx-preferences).
Already tried to configure it via "externalDocumentationLink", but no luck there, couldn't find any javadoc/package-list referenced to this lib.
If you click on android.content.SharedPreferences in documentation, you will be redirected to https://developer.android.com/reference/android/content/SharedPreferences.html, i want to achieve same behavior for rxprefs (redirect to github)
UPD: dokka configuration moved from root build.gradle to application's:
apply plugin: 'org.jetbrains.dokka-android'
dokka {
externalDocumentationLink {
url = new URL("http://reactivex.io/RxJava/javadoc/")
}
externalDocumentationLink {
url = new URL("http://jakewharton.github.io/timber/")
}
externalDocumentationLink {
url = new URL("https://dagger.dev/api/2.0/")
}
Set<ProjectDependency> deps =
project.configurations.collectMany {
it.allDependencies
}.findAll {
it instanceof ProjectDependency
}
sourceDirs = files(deps.collect {
p ->
def path = new File(p.getDependencyProject().projectDir, "/src/main/java")
return path
})
}
I've added some external documentation deps, but still getting a lot of warnings like
Can't find node by signature `javax.inject.Provider`
or
Can't find node by signature `androidx.fragment.app.Fragment`
Maybe it's impossible to have links on that kind of dependencies?
I'd like to have only my code to be documented, but have external links for framework's and third party's dependecies, is it real?
Android Studio 2.0 Preview 2, Gradle Wrapper 2.8, Mac OS X
-MainProjectWorkspace
-|-build.gradle
-|-settings.gradle
-|-gradle.properties
-|-gradle
-|-MyLibraryDependencies
-|-MyMainModule
--|-build.gradle
--|-build
--|-src
---|-androidTest
---|-main
----|-assets
----|-jniLibs
----|-libs
----|-java
-----|-com
----|-res
----|-AndroidManifest.xml
MyMainModule/build.gradle
//Not a single SourceSets configurations.
productFlavors {
flavor1 {
}
flavor2 {
}
}
buildTypes {
release {
}
debug {
}
}
A genius developer left System.out.println statements, instead of Log statements in several hundreds of Java source-files in 'src/main/java'. Ideally, we do not want Sysout statements getting bundled with either of the applicationVariants, specially flavor1Release and flavor2Release.
Before we make amends to those hundreds of Java source-files and eventually switch the Sysout statements to Log statements, we would need to turn them off urgently.
Two possible solutions -
Execute a simple script that will remove all the Sysout statements in the Java source-files in 'src/main/java'. But about that, variants flavor1Debug and flavor2Debug need the Loggers displaying what's going on in the App.
Execute a simple Gradle task at build-time, copy Java source-files from 'src/main/java' to 'src/release/java', ensuring Sysout statements are omitted.
Solution 2 appears to be quickest, easiest, elegant. Particularly when the Gradle Custom-task is executed independently. Except for that, Gradle-sync in Android-Studio seems to be copying everything from 'src/main' to 'src/release' including assets, jniLibs, libs, res and even the AndroidManifest.xml. Fortunately, the Java source-files are ripped-off the Sysout statements in 'src/release', which is the expected result, and ideally, only 'src/release/java' should remain without any other folders.
task prepareRelease(type: Task) {
Pattern sysoutRegex = Pattern.compile(<Sysout-Pattern>)
try {
File releaseFolder = file("src/release")
File mainFolder = file("src/main")
ByteArrayOutputStream output = new ByteArrayOutputStream()
exec {
commandLine = "sh"
args = ["-c", "find ${mainFolder.canonicalPath} -name '*' -type f -print ",
"| xargs egrep -l '${sysoutRegex.pattern()}'"]
standardOutput = output
}
def fileList = output.toString().split(System.getProperty("line.separator"))
fileList.each { line ->
File srcFile = file(line)
File destFile = file(srcFile.canonicalPath.replace("main", "release"))
def content = srcFile.readLines()
if (!destFile.exists()) {
destFile.parentFile.mkdirs()
destFile.createNewFile()
destFile.writable = true
}
destFile.withWriter { out ->
content.eachWithIndex { contentLine, index ->
if (!sysoutRegex.matcher(contentLine).find()) {
out.println contentLine
}
}
}
} catch (Exception fail) {
throw fail
}
}
There is nothing in the custom Gradle-task that may cause this error of making "src/release" an exact copy of "src/main", which was not intended to be.
Any pointers to prevent this default copy of "src/main" to "src/release" will be greatly appreciated.
Based off RaGe's comment - "How about using *.java as your find pattern instead of * ?"
Kudos. I was not supposed to break the "find | xargs egrep " before the '|' into two separate args in the args[] in the exec task. Indeed, a Genius!!!
I'm integrating New Relic in my project (with Android Studio & Gradle) which has 2 variants. Each variant has its own generated token, which I store in each variant's string.xml file.
In the New Relic documentation, it states the following:
In your project’s root directory (projectname/app), add a newrelic.properties file with the following line:
com.newrelic.application_token=generated_token
The problem is, if I do this, how can make the correct token appear for the correct variant? If this file must appear in the project root, I can't create one per variant, and so I'm forced to use the same token for both variants, which doesn't work for my requirements.
Any insight would be appreciated.
Okay, so after contacting the support team at New Relic, there is apparently no direct solution for this as of today, although they said they've opened a feature request, and so this problem might be solved soon.
From what I managed to understand, the reason this file is needed is so that the New Relic system can display an un-obfuscated error log when an exception occurs on a production version which has been obfuscated with ProGuard.
The New Relic system, with the help of this file, will upload the ProGuard mapping.txt file to the New Relic servers and associate it with your app according to the specified token. With this, New Relic can un-obfuscate stack traces and display a descriptive stack trace with actual class & method names, rather a, b, c, etc.
As a workaround, I was told that I can forego this file all together, if I upload the mapping file manually.
The mapping file can be found at:
build/outputs/proguard/release/mapping.txt
In order to manually upload the file, perform the following via command line:
curl -v -F proguard=#"<path_to_mapping.txt>" -H "X-APP-LICENSE-KEY:<APPLICATION_TOKEN>" https://mobile-symbol-upload.newrelic.com/symbol
This must be done for each variant which is being obfuscated with ProGuard (classically, release builds).
Source
Hope this helps someone else.
I solved creating some Gradle tasks. Please, take a look at https://discuss.newrelic.com/t/how-to-set-up-newrelic-properties-file-for-project-with-multiple-build-variants/46176/5
I following a code that worked pretty well for me.
Add the New Relic application token on a string resource file. i.e.: api.xml.
Create a new Gradle file. i.e: newrelic-util.gradle.
Add the following content on the newly created Gradle file:
apply plugin: 'com.android.application'
android.applicationVariants.all { variant ->
//<editor-fold desc="Setup New Relic property file">
def variantName = variant.name.capitalize()
def newRelicTasksGroup = "newrelic"
def projectDirPath = project.getProjectDir().absolutePath
def newRelicPropertyFileName = "newrelic.properties"
def newRelicPropertyFilePath = "${projectDirPath}/${newRelicPropertyFileName}"
// Cleanup task for New Relic property file creation process.
def deleteNewRelicPropertyFile = "deleteNewRelicPropertyFile"
def taskDeleteNewRelicPropertyFile = project.tasks.findByName(deleteNewRelicPropertyFile)
if (!taskDeleteNewRelicPropertyFile) {
taskDeleteNewRelicPropertyFile = tasks.create(name: deleteNewRelicPropertyFile) {
group = newRelicTasksGroup
description = "Delete the newrelic.properties file on project dir."
doLast {
new File("${newRelicPropertyFilePath}").with {
if (exists()) {
logger.lifecycle("Deleting file ${absolutePath}.")
delete()
} else {
logger.lifecycle("Nothing to do. File ${absolutePath} not found.")
}
}
}
}
}
/*
* Fix for warning message reported by task "newRelicMapUploadVariantName"
* Message:
* [newrelic] newrelic.properties was not found! Mapping file for variant [variantName] not uploaded.
* New Relic discussion:
* https://discuss.newrelic.com/t/how-to-set-up-newrelic-properties-file-for-project-with-multiple-build-variants/46176
*/
def requiredTaskName = "assemble${variantName}"
def taskAssembleByVariant = project.tasks.findByName(requiredTaskName)
def createNewRelicPropertyFileVariantName = "createNewRelicPropertyFile${variantName}"
// 0. Searching task candidate to be dependent.
if (taskAssembleByVariant) {
logger.debug("Candidate task to be dependent found: ${taskAssembleByVariant.name}")
// 1. Task creation
def taskCreateNewRelicPropertyFile = tasks.create(name: createNewRelicPropertyFileVariantName) {
group = newRelicTasksGroup
description = "Generate the newrelic.properties file on project dir.\nA key/value propety " +
"will be written in file to be consumed by newRelicMapUploadVariantName task."
logger.debug("Creating task: ${name}")
doLast {
def newRelicPropertyKey = "com.newrelic.application_token"
def newRelicStringResourceKey = "new_relic_key"
def targetResourceFileName = "api.xml"
def variantXmlResourceFilePath = "${projectDirPath}/src/${variant.name}/res/values/${targetResourceFileName}"
def mainXmlResourceFilePath = "${projectDirPath}/src/main/res/values/${targetResourceFileName}"
def xmlResourceFilesPaths = [variantXmlResourceFilePath, mainXmlResourceFilePath]
xmlResourceFilesPaths.any { xmlResourceFilePath ->
// 1.1. Searching xml resource file.
def xmlResourceFile = new File(xmlResourceFilePath)
if (xmlResourceFile.exists()) {
logger.lifecycle("Reading property from xml resource file: ${xmlResourceFilePath}.")
// 1.2. Searching for string name new_relic_key api.xml resource file.
def nodeResources = new XmlParser().parse(xmlResourceFile)
def nodeString = nodeResources.find {
Node nodeString -> nodeString.'#name'.toString() == newRelicStringResourceKey
}
// 1.3. Checking if string name new_relic_key was found.
if (nodeString != null) {
def newRelicApplicationToken = "${nodeString.value()[0]}"
logger.lifecycle("name:${nodeString.'#name'.toString()};" +
"value:${newRelicApplicationToken}")
// 1.4 Checking the content of newRelicApplicationToken
if (newRelicApplicationToken == 'null' || newRelicApplicationToken.allWhitespace) {
logger.warn("Invalid value for key ${newRelicStringResourceKey}. " +
"Please, consider configuring a value for key ${newRelicStringResourceKey}" +
" on ${xmlResourceFile.name} resource file for buildVariant: ${variantName}. " +
"The ${newRelicPropertyFileName} will be not created.")
return true // break the loop
}
// 1.5. File creation.
File fileProperties = new File(newRelicPropertyFilePath)
fileProperties.createNewFile()
logger.lifecycle("File ${fileProperties.absolutePath} created.")
// 1.6. Writing content on properties file.
def fileComments = "File generated dynamically by gradle task ${createNewRelicPropertyFileVariantName}.\n" +
"Don't change it manually.\n" +
"Don't track it on VCS."
new Properties().with {
load(fileProperties.newDataInputStream())
setProperty(newRelicPropertyKey, newRelicApplicationToken.toString())
store(fileProperties.newWriter(), fileComments)
}
logger.lifecycle("Properties saved on file ${fileProperties.absolutePath}.")
return true // break the loop
} else {
logger.warn("The key ${newRelicStringResourceKey} was not found on ${xmlResourceFile.absolutePath}.\n" +
"Please, consider configuring a key/value on ${xmlResourceFile.name} resource file for buildVariant: ${variantName}.")
return // continue to next xmlResourceFilePath
}
} else {
logger.error("Resource file not found: ${xmlResourceFile.absolutePath}")
return // continue next xmlResourceFilePath
}
}
}
}
// 2. Task dependency setup
// To check the task dependencies, use:
// logger.lifecycle("Task ${name} now depends on tasks:")
// dependsOn.forEach { dep -> logger.lifecycle("\tTask: ${dep}") }
tasks['clean'].dependsOn taskDeleteNewRelicPropertyFile
taskCreateNewRelicPropertyFile.dependsOn taskDeleteNewRelicPropertyFile
taskAssembleByVariant.dependsOn taskCreateNewRelicPropertyFile
} else {
logger.error("Required task ${requiredTaskName} was not found. " +
"The task ${createNewRelicPropertyFileVariantName} will be not created.")
}
//</editor-fold>
}
On app/build.gradle file, apply the Gradle file.
apply from: './newrelic-util.gradle'
That’s it. I created a file named newrelic-util.gradle on project app dir. If you execute the task assembleAnyVariantName, the task createNewRelicPropertyFileAnyVarianteName will be performed first. Tip: don’t track the generated file newrelic.properties file. Ignore it on your VCS.
Additionally, the task deleteNewRelicPropertyFile will be performed right before the tasks ‘clean’ and ‘createNewRelicPropertyFileAnyVarianteName’ in order to avoid a file with a wrong New Relic application token.
I have the following setup in my build.gradle file:
// Task designed to bump version numbers. This should be the first task run
// after a new release branch is created.
task bumpVersion(description: 'Bumps the version number of the current Android release. Should be used as a standalone task, and should only be the first task called after creating a release branch.', group: 'Management') << {
Properties props = new Properties();
File propsFile = new File('gradle.properties');
props.load(propsFile.newDataInputStream());
def currentVersionCode = props.getProperty("CORE_VERSION_CODE") as int;
def currentVersionName = props.getProperty("CORE_VERSION_NAME") as String;
def intPortionsOfVersionName = currentVersionName.tokenize('.').toArray();
def leastSignificantPortion = intPortionsOfVersionName[intPortionsOfVersionName.length - 1] as int;
def newVersionCode = currentVersionCode + 1;
def newVersionName = "";
if (!project.hasProperty('newVersion')) {
leastSignificantPortion = leastSignificantPortion + 1;
intPortionsOfVersionName[intPortionsOfVersionName.length - 1] = leastSignificantPortion;
newVersionName = intPortionsOfVersionName.collect{ it }.join(".");
} else {
newVersionName = project.getProperty('newVersion');
}
props.setProperty("CORE_VERSION_NAME", newVersionName as String);
props.setProperty("CORE_VERSION_CODE", newVersionCode as String);
props.store(propsFile.newWriter(), null);
}
Under the line newVersionName = project.getProperty('newVersion') I try to acquire the property called "newVersion", if it exists, and bump the least significant digit if it's not available.
This works fine, but what I want to do is add a way to specify this option in the documentation (i.e. gradle help --task bumpVersion). For instance, if I run gradle help --task help, it gives me:
:help
Detailed task information for help
Path
:help
Type
Help (org.gradle.configuration.Help)
Options
--task The task, detailed help is requested for.
Description
Displays a help message
Notice how '--task' is under the Options section. I'm wondering how to do this with my own code.
This can be done using the #Option annotation.
#Option(option = "version", description = "Version number to use")
public void setVersion(String version) { ... }
Note: This is an internal API so it may change.
Edit: May have forgotten to mention you will have to implement your task as a custom task class to leverage this capability.