How to tell gradlew to export lint results file to projectDir? - android

I'm building an Android application using Jenkins pipeline and Gradle.
I'd like to run Lint tests on the code and so the command which is used for that matter is:
./gradlew lintStagingDebug
For some reason this Gradle task is not creating the report.
I know that if I'd run lint like so:
lint <project_dir> --xml xml_dest_path
It would create the report.
But since through out the whole pipeline I'm using gradlew to run the relevant tasks (clean, lint, compile, unittest, assemble) I'd like to use gradlew for this task as well but I'm not sure how to tell Gradle to export the lint report.
Another thing I've tried is to edit the project/build.gradle file and change this section:
lintOptions {
abortOnError false
}
to (according to Lint official documentation)
lintOptions {
abortOnError false
xmlOutput projectDir/lint-results.xml
}
But then I get an error:
A problem occurred evaluating project ':App01'.
> No signature of method: java.io.File.div() is applicable for argument types: (com.android.build.gradle.tasks.Lint_Decorated) values: [task ':App01:lint']
Anyone knows how it can be done?

You can provide the XML report output file in Gradle in the following way:
lintOptions {
xmlOutput file("lint-results.xml")
}
The xmlOutput method requires a file type argument. I have used the relative path in the argument. You can also use it with projectDir.
lintOptions {
xmlOutput file("$projectDir/lint-results.xml")
}

Here's how I solved it.
stage('Lint run') {
sh """
export HOME=$GRADLE_USER_HOME
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"
OUTPUTFILE="lint-results.txt"
touch \$OUTPUTFILE
./gradlew lint${BUILDFLAV}${BUILDTYPE} -x lint 2>&1 | tee \$OUTPUTFILE
"""
})
Explanation:
In order to keep using gradlew to run lint test on the code, I've defined an output file and added at the end of the gradlew command redirection of stdOut and stdErr to this output file using the Linux command "tee".

Related

Add variable in gradle command

I have to following gradle code:
testOptions {
unitTests.returnDefaultValues = true
unitTests.all {
// All the usual Gradle options.
jvmArgs '-XX:MaxPermSize=1024m'
}
execution 'ANDROID_TEST_ORCHESTRATOR'
}
And I am using Android test orchestrator, but I makes my UI tests take a lot of time to run. So I would like to be able to request for orchestrator in the command line like:
./gradlew androidConnectedCheck --orchestrator
So I can controll when to use orchestator. Is it possible to do that with gradle?
Edit:
I am not interested in using a gradle variable in my code (in Java or Kotlin) I would like to create a conditional test configuration accordingly with my command line
./gradlew androidConnectedCheck -Porchestrator
And in gradle, you can do:
def orchestrator = project.hasProperty('orchestrator')
orchestrator will be true if it was passed as an arg. This uses the project-prop built into gradle command line.

Add lint to CI and mark a build fail when lint produces errors for Android Studio projects

I need the Gradle build to be marked as a failure and it should be stopped automatically if running lint gives me an error. I made the changes in code as required but it did not result in any change.
Code:
lintOptions {
// set to true to turn off analysis progress reporting by lint
quiet true
// if true, stop the gradle build if errors are found
abortOnError true
// if true, only report errors
ignoreWarnings false
}
Also apart from it, I need to add lint to CI. The CI software I use is Jenkins. SO I need to configure their android linting plugin of Jenkins such that the build is stopped and marked Failure if Lint gives an error.
I am very new to lint and CI, so please provide a detailed answer.
You don't need the Lint Jenkins plugin. Just put something this in your JenkinsFile.
try {
sh './gradlew lint'
} finally {
step([$class: 'ArtifactArchiver', artifacts: 'app/build/reports/staticAnalysis/lint/', fingerprint: true])
}
ArtifactArchiver is just to collect artifacts after lint check.

Gradle build: Execution failed for task ':app:lint'

I'm using Android Studio 2.0 and I was trying to running my program when something strange happened. I ran the build command of the gradle and I got this error:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:lint'.
> Lint found errors in the project; aborting build.
Fix the issues identified by lint, or add the following to your build script to proceed with errors:
...
android {
lintOptions {
abortOnError false
}
}
...
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 4.197 secs
Lint found errors in the project; aborting build.
Fix the issues identified by lint, or add the following to your build script to proceed with errors:
...
android {
lintOptions {
abortOnError false
}
}
...
10:41:28: External task execution finished 'build'.
And so... What the hell is that? I'm supposed to do to solve this problem adding the code to the gradle.build, but the question is: why I got this error message?
Please save me guys!
Add this in your app/build.gradle file
android {
//...
lintOptions {
abortOnError false
}
}
Switch to project view
Then, project/app/build/reports/lint-results
Now, you will find the lint-result files in two formats - 1) xml and 2) html.
You can view these files as is. However, I find viewing the lint results in a browser to be easy on the eyes. Just right-click on the html file and choose view in browser.
It opens up like the image below in my Chrome browser.
You have some lint issues while you are bulding the app module.
You can find all issues found in the report generated in Project\module\build\outputs.
Here you will find the html file and the xml file with the lint report.
Using this script in the app\build.gradle
android {
lintOptions {
abortOnError false
}
}
you can disable the block but it is not the best practice.
You should analyze the lint report to solve each point.
I think it's better to find the errors rather than ignoring them.
try this:
In Gradle Console find "Wrote HTML report to file", open the indicated HTML file and you will find a lint report
Go to your errors and fix them
Your build.gradle(Module: app) should include
android {
lintOptions {
abortOnError false
}
}
If the abortOnError false does not work, the Flutter version 2.5.1 recommend using the following arguments in the lintOptions:
android{
...
lintOptions {
checkReleaseBuilds false
}
}
Run gradle build -i instead of just gradle build. There will be lots more output than usual. Some of it will look like this:
> Task :project-name:lint FAILED
Putting task artifact state for task ':project-name:lint' into context took 0.0 secs.
Up-to-date check for task ':project-name:lint' took 0.0 secs. It is not up-to-date because:
Task has not declared any outputs.
Ran lint on variant release: 333 issues found
Ran lint on variant debug: 333 issues found
Wrote HTML report to file:///some/path/lint-results.html
Wrote XML report to file:///some/pahth/lint-results.xml
:project-name:lint (Thread[Task worker for ':',5,main]) completed. Took 1.756 secs.
Check out /some/path/lint-results.html to see why lint failed. Once those errors are fixed, your build will complete smoothly.
I got the error Execution failed for task ':app:lintVital[myBuildVariant]' and a nullpointer error. It happened after switching branches and the thing that helped for me was to do a Build -> Clean Project
Only add this code in build gradle:
android {
lintOptions {
abortOnError false
}
}
That should be enough.
If you face this issue do not add a linter disabler to your gradle,
This is an access error issue,I faced that on react-native after using sudo command for a few command,
Just reset your code access and then release again
For example the mac
sudo chmod -R 777 <project-root-folder-name>
If it remains use
preBuild.doFirst {
ant.replaceregexp(
match:'Bad gradle',
replace:'Correct one',
flags:'g',
byline:true
) {
fileset(
dir: 'Where to search',
includes: '*.java'
)
}
}
So you can fix error before release if exists in that library
The new lint way is like so
android {
lint {
isAbortOnError = false
}
}

Access project related data from Gradle command

I need project related data like project name,app version and its main module from gradle based android project. I have tried various tasks like project,properties but none of it giving me specific information i need.
Is there a way to find version code,app name and main android module using gradle in command line?
Using "BuildConfig" global variable you will get
boolean DEBUG
String APPLICATION_ID
String BUILD_TYPE
String FLAVOR
int VERSION_CODE
String VERSION_NAME
eg :- BuildConfig.APPLICATION_ID
and if you defined any global data in gradle like
debug {
buildConfigField "String", "BASE_URL", '"http://172.16.1.175:8080/api/"'
debuggable true
}
you will get this details also
BuildConfig.BASE_URL
You can probably write your own custom gradle task for doing that. Add this code snippet in your app build.gradle, where you define your android plugin and run it from console. You can format output like you need it and use other data from build script.
task hello<<{
println("versionCode = ${android.defaultConfig.versionCode}")
println("applicationId = ${android.defaultConfig.applicationId}")
println("minSDK = ${android.defaultConfig.minSdkVersion}")
}
you can use resValue for that to get value
Gradle
defaultConfig {
//other config
resValue "String","versionCode","1"
}
your class
context.getString(R.string.versionCode);
I don't know if it suits, you can create one common init gradle file, which you run from command line, so it is not a source code manipulation, where you print out all necessary data. But gradle output is dirty.
This is snippet of init.gradle which is in /Users/username
allprojects{
afterEvaluate({//listen for project evaluation
println(project.name)//it is supposed to be 2 projects "ProjName" and "app"
if(project.name.equalsIgnoreCase("app")){//or any other condtion to check if it is inner android project
project.task("getVersion",{
println("versionCode = ${android.defaultConfig.versionCode}")
})
}
});
}
you start this script like ./gradlew --I /Users/username/init.gradle
This is what I have as an output
music
app
versionCode = 1
:help
Welcome to Gradle 2.4.
To run a build, run gradlew <task> ...
To see a list of available tasks, run gradlew tasks
To see a list of command-line options, run gradlew --help
To see more detail about a task, run gradlew help --task <task>
BUILD SUCCESSFUL
Total time: 6.929 secs
This build could be faster, please consider using the Gradle Daemon: http://gradle.org/docs/2.4/userguide/gradle_daemon.html
So this is what could be done, another available option is to parse build.gradle file or manifest.xml in bash, or write own console utility that will do it with a cleaner output.
I hope I helped.

Suppress successful lint output in Gradle

Is there a switch to turn off the lint output like this:
:app:lint
Ran lint on variant release: 0 issues found
Ran lint on variant debug: 0 issues found
Wrote HTML report to file:/.../project/app/build/outputs/lint-results.html
Wrote XML report to .../project/app/build/outputs/lint-results.xml
It should be pretty obvious from a
:app:lint
:app:nextTaskAfterLint
output that:
Lint ran
No issues found
No-one wants to look at an almost empty html page saying "Congratulations!"
I tried these options:
android {
lintOptions {
quiet true // no effect at all
textOutput file('lint.x') // no output to given file, and still outputs to stdout
}
}
It would be nice to have the above output if the issues found is > 0, but it's not a big problem since I can stop the build by warningsAsErrors true and abortOnError true if any issues found.
According to doc, you can exclude a task with a command line:
http://www.gradle.org/docs/current/userguide/userguide_single.html#sec%3aexcluding_tasks_from_the_command_line
So you can use this command to skip lint:
gradlew build -x lint
To avoid the log output, you can check the Lint task here:
https://android.googlesource.com/platform/tools/base/+/gradle_0.13.3/build-system/gradle/src/main/groovy/com/android/build/gradle/tasks/Lint.groovy
You can find at line 90:
if (!mFatalOnly) {
println "Ran lint on variant " + variant.getName() + ": " + warnings.size() +
" issues found"
}
Hovewer I didn't find how to set this variable-
Here you can find all options available today with the gradle plugin 0.13.3:
https://android.googlesource.com/platform/tools/base/+/gradle_0.13.3/build-system/builder-model/src/main/java/com/android/builder/model/LintOptions.java

Categories

Resources