gradle Lint plugin defined task for include or exclude custom files - gradle

I am using gradle lint plugin in my Android project on CI Server. But sometimes I want to run android lint only on custom files, just like gradle Copy Task include and exclude to defined task scope in my gradle task.
In Android Studio, inspection can defined these behavior. But in my case, it is gradle task run on CI Server without UI interaction.

I don't think the Gradle Plugin is capeable of that. However you can also run lint on it's own.
lint [flags] <project directory>
You can get all of the options with:
lint --help
The executable is located in $ANDROID_HOME/tools/
More information here.

Related

Trigger ./gradlew test when files have changed

I am building a Spring Boot REST API application and using Kotlin as the language.
For the development I am using IntellJ as the IDE.
The project structure looks as follows:
Is there a way to watch changes in the folder src to trigger the gradle task ./gradlew test after files have been changed?
You can use Gradle's support for continuous builds:
./gradlew test --continuous
This will cause Gradle to watch the filesystem for changes and execute the test task and any tasks upon which it depends whenever a change is detected.
You can right click the gradle task and set it to be done e.g. after each build.

Is it necessary to install Groovy for Gradle

I'm new to Gradle. I see that Gradle lib already has a file 'groovy-all-2.4.12.jar' in lib folder and I don't seem to have any issues with tasks and or dependencies. Still, is it necessary in any scenario to install Groovy on my system on top of it?
Reason why I ask is that, when I do 'gradle -v' in command prompt, I see few warnings. Please see attached screenshot.
With gradle it is strongly recommended to use the Gradle wrapper committed into the project you are building instead of a system-wide gradle distribution (that is gradlew and not gralde). This guarantees the matching version of Gradle your project has been tested with.
With the Gradle wrapper you do not need to care about any dependencies that Grade itself needs, such as groovy and you really do not need to install anything of Gradle at all as the wrapper in your project will download all it needs on the first run.
The minimum setup for the Gradle wrapper is:
/gradlew - unix shell script
/gradlew.bat - windows batch script
/gradle/wrapper/gradle-wrapper.properties -- the properties file defining the version
/gradle/wrapper/gradle-wrapper.jar -- the minimal jar (50Kb) that takes care about the rest
The above files must be committed into your project and this is what 99% of all gradle projects do. You will find further details here https://docs.gradle.org/current/userguide/gradle_wrapper.html

Error: DefaultOperationDescriptor#2b878eea already available in Android Studio

Failed to complete Gradle Execution
When I try to Sync Gradle with Project Files, the error mentioned below appears
Android Studio version that I am using is 3.0.1, Gradle Build 4.1-all.zip
FYI, I have already tried ->Build->Clean Project and Invalidate Cache & Restart and one more thing is that there is no error in my code.
Message Error:
Information:Gradle tasks [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar]
Error:Failed to complete Gradle execution.
Cause:
Operation org.gradle.tooling.internal.provider.events.DefaultOperationDescriptor#86028ba already available.
BUILD SUCCESSFUL in 8s
Information:1 error
Information:0 warnings
But When I try to Run on my Emulator
The error below appears
Message Error At Run Time
Information:Gradle tasks [:app:assembleDebug]
E:\Android Game App\app\build.gradle
Error:(1, 1) A problem occurred evaluating project ':Android Game App:app'.
Failed to apply plugin [id 'com.android.application']
Due to a limitation of Gradle new variant-aware dependency management, loading the Android Gradle plugin in different class loaders leads to a build error.
This can occur when the buildscript classpaths that contain the Android Gradle plugin in sub-projects, or included projects in the case of composite builds, are set differently.
To resolve this issue, add the Android Gradle plugin to only the buildscript classpath of the top-level build.gradle file.
In the case of composite builds, also make sure the build script classpaths that contain the Android Gradle plugin are identical across the main and included projects.
If you are using a version of Gradle that has fixed the issue, you can disable this check by setting android.enableBuildScriptClasspathCheck=false in the gradle.properties file.
To learn more about this issue, go to https://d.android.com/r/tools/buildscript-classpath-check.html.
Information:BUILD FAILED in 16s
Information:1 error
Information:0 warnings
Information:See complete output in console
Nothing found on Google! Any help would be highly encouraged???
The key to the problem is this line. Maybe due to a project.all in root build.gradle or you are using composite builds.
This can occur when the build script classpaths that contain the Android Gradle plugin in sub-projects,
or included projects in the case of composite builds, are set differently.
that mean in all yours build.gradle you have more than one this line
buildscript {
dependencies {
classpath "com.android.tools.build:gradle:x.y.z"
}
}
what this means is that with the introduction of Android Gradle Plugin 3.Y.Z and the new way of handling dependencies,
if you mix in the same project 2 projects with different plugin version (one with a 2.3 and other with 3.0.1) you will get dragons while compiling.
And this error it's a way to force developer to check it and opt-out once detected and solved.
how to solve it, first ensure you don't use a android gradle plugin below 3.0.1 and use the new dependencies configurations, and add this property on every gradle.properties you have.
android.enableBuildScriptClasspathCheck=false
with that you can now compile
one example could be found at realm sample repo they use a allprojects block that include android gradle plugin on every module. and solve it adding previus gradle property at root gradle.properties

Using Gradle to find dependency tree

Is it possible to use Gradle to produce a tree of what depends on what?
I have a project and would like to find out all the dependencies so I may be able to prune it a little with forward declarations etc.
Without modules:
gradle dependencies
For Android:
gradle app:dependencies
Using gradle wrapper:
./gradlew app:dependencies
Note: Replace app with the project module name.
Additionally, if you want to check if something is compile vs. testCompile vs androidTestCompile dependency as well as what is pulling it in:
./gradlew :app:dependencyInsight --configuration compile --dependency <name>
./gradlew :app:dependencyInsight --configuration testCompile --dependency <name>
./gradlew :app:dependencyInsight --configuration androidTestCompile --dependency <name>
You can render the dependency tree with the command gradle dependencies. For more information check the section Listing dependencies in a project in the online user guide.
If you find it hard to navigate console output of gradle dependencies, you can add the Project reports plugin:
apply plugin: 'project-report'
And generate a HTML report using:
$ ./gradlew htmlDependencyReport
Report can normally be found in build/reports/project/dependencies/index.html
It looks like this:
In Android Studio (at least since v2.3.3) you can run the command directly from the UI:
Click on the Gradle tab and then double click on :yourmodule -> Tasks -> android -> androidDependencies
The tree will be displayed in the Gradle Console tab
Often the complete testImplementation, implementation, and androidTestImplementation dependency graph is too much to examine together. If you merely want the implementation dependency graph you can use:
./gradlew app:dependencies --configuration implementation
Source: Listing dependencies in a project
Note: compile has been deprecated in more recent versions of Gradle and in more recent versions you are advised to shift all of your compile dependencies to implementation. Please see this answer here
If you want to visualize your dependencies in a graph you can use gradle-dependency-graph-generator plugin.
Generally the output of this plugin can be found in build/reports/dependency-graph directory and it contains three files (.dot|.png|.svg) if you are using the 0.5.0 version of the plugin.
Example of dependences graph in a real app (Chess Clock):
For me, it was simply one command
in build.gradle add plugin
apply plugin: 'project-report'
and then go to cmd and run following command
./gradlew htmlDependencyReport
This gives me an HTML report WOW Html report 💕
Or if you want the report in a text file, to make search easy use following command
gradlew dependencyReport
That's all my lord.
Things have moved forward in Gradle so I believe this question merits another answer.
Since Gradle 4.3, "build scans" were introduced. All relevant info is available in the Gradle docs (1, 2). For me, this seems to now be the easiest way to check your dependencies (and generally your build) in a clear, organized way.
They are very easy to create, just execute:
gradle build --scan
(or ./gradlew build --scan if you use a wrapper)
This produces a randomly generated link where you can see your scan. When opening that link, you enter your email and gain full control of the link: eg. share it or delete it. It has got a lot of info about your build, not just dependencies. You can see your dependencies, their hierarchies, the repository used to obtain them but also a lot of other stuff about your build, namely, its performance (which is of interest in big complex builds), your tests, even your console output and your system configuration, which JDK and JVM was used, max heap size etc.
This is a printscreen from a mock project:
A build scan is a shareable record of a build that provides insights into what happened and why. You can create a build scan at scans.gradle.com for free.
Note however, that info for your build process will be sent to the Gradle servers. You have full control to delete it when you are finished with your inspection.
Finally, you can use build scans with Gradle versions earlier than 4.3 too, you just have to manually add the scans plugin in your buildscript.
Edit:
Incorporating some feedback from the comments some extra notes:
1) It is very difficult to do this by mistake or without understanding that some info for your build will be online (private to you, with the ability to delete it, but still online).
When executing gradle build --scan the following message appears:
Publishing a build scan to scans.gradle.com requires accepting the Gradle
Terms of Service defined at https://gradle.com/terms-of-service. Do you
accept these terms? [yes, no]
You have to explicitly write yes and then the message continues:
Publishing build scan...
https://gradle.com/s/a12en0dasdu
2) In Gradle Enterprise you can host gradle build scans in your own servers. However I have no experience in this and my proposed approach was about the standard Gradle distribution, using Gradle's servers for your build scans.
3) Gradle itself promotes the build scans as the way to deal with most your build problems.
For Android, type this in terminal
gradlew app:dependencies
It will list all the dependencies and the ones with newer versions for you to upgrade like
com.android.support:customtabs:26.1.0 -> 27.1.1 (*)
For recent versions of Gradle (I tested with the 6.4.1 version):
gradle dependencies --configuration compileClasspath
or if you're using the Gradle Wrapper:
gradlew dependencies --configuration compileClasspath
When building for Android with the 'debug' and 'release' compilation profiles, the debugCompileClasspath and releaseCompileClasspath configurations can be used instead of compileClasspath.
I also found useful to run this:
./gradlew dI --dependency <your library>
This shows how are being dependencies resolved (dependencyInsight) and help you debugging into where do you need to force or exclude libraries in your build.gradle
See: https://docs.gradle.org/current/userguide/tutorial_gradle_command_line.html
In Android Studio
1) Open terminal and ensure you are at project's root folder.
2) Run ./gradlew app:dependencies (if not using gradle wrapper, try gradle app:dependencies)
Note that running ./gradle dependencies will only give you dependency tree of project's root folder, so mentioning app in above manner, i.e. ./gradlew app:dependencies is important.
Note that you may need to do something like ./gradlew <module_directory>:<module_name>:dependencies if the module has extra directory before reach its build.gradle. When in doubt, do ./gradlew tasks --all to check the name.
If you want all the dependencies in a single file at the end within two steps.
Add this to your build.gradle.kts in the root of your project:
project.rootProject.allprojects {
apply(plugin="project-report")
this.task("allDependencies", DependencyReportTask::class) {
evaluationDependsOnChildren()
this.setRenderer(AsciiDependencyReportRenderer())
}
}
Then apply:
./gradlew allDependencies | grep '\-\-\-' | grep -Po '\w+.*$' | awk -F ' ' '{ print $1 }' | sort | grep -v '\{' | grep -v '\[' | uniq | grep '.\+:.\+:.\+'
This will give you all the dependencies in your project and sub-projects along with all the 3rd party dependencies.
If you want to get this done in a programmatic way, then you'll need a custom renderer of the dependencies - you can start by extending the AsciiDependencyReportRenderer that prints an ascii graph of the dependencies by default.
double click and run dependency under help in gradle view
Try either this
./gradlew dependencies > ~/dependencies.txt
or
gradle dependencies > ~/dependencies.txt`
which should write the dependencies in text file under user's home directory.

Publish JavaDoc on Jenkins with maven

I have maven project that is built by Jenkins-CI.
How to generate and publish JavaDoc on Jenkins?
Make sure Jenkins javadoc plugin is installed.
Go to http://yourjenkinsserver.com/jenkins/pluginManager/installed to see list of intalled plugins.
Plugin page https://wiki.jenkins-ci.org/display/JENKINS/Javadoc+Plugin
Configure Jenkins job:
In Build section, Goals and options line add:
javadoc:javadoc
That's all. No need to change pom.xml
The simplest thing to do is to create a separate task that runs thr javadoc command, and which runs after the compile task. You pass it the input and output directories.
I would run a separate tomcat for your CI website - it's easier.

Resources