Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add java compiler options when compiling with Android Gradle Plugin?

Tags:

I have a build.gradle file with dependencies { classpath 'com.android.tools.build:gradle:0.13.3'} and apply plugin: 'com.android.application'.

When I do a debug build I get:

gradle clean assembleDebug :myapp:preBuild (...) :myapp:compileDebugJava Note: C:\path\to\MyClass.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.  :myapp:preDexDebug (...) :myapp:assembleDebug  BUILD SUCCESSFUL 

How can I add the -Xlint:unchecked to the underlying task? Gradle Plugin User Guide on Java compilation options is unhelpful.

like image 474
Konrad Jamrozik Avatar asked Dec 03 '14 14:12

Konrad Jamrozik


People also ask

What is ProcessResources in Gradle?

Class ProcessResourcesCopies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.

What is sourceCompatibility in Gradle?

According to Gradle documentation: sourceCompatibility is "Java version compatibility to use when compiling Java source." targetCompatibility is "Java version to generate classes for."

What is runtimeClasspath in Gradle?

main. runtimeClasspath' contains the compiled classes of the source set, and task autowiring automatically adds the necessary task dependencies. Maybe you want 'sourceSets. main. compileClasspath'.

What is Annotationprocessor Gradle?

An annotation processor is a class which extends AbstractProcessor (in the package javax. annotation. processing ) providing the functionality needed to generate whatever it needs to generate, such as classes in the case of mapstruct.


1 Answers

I tried the solution posed by @Konrad Jamrozik but it didn't work with my project due to flavours in my project.

Given that we're just turning on additional warnings, not something that's significantly changing how the compiler operates, I don't see it being an issue that it will be added to both release and debug builds. As such, this answer has a cleaner method that works with flavours: How to add -Xlint:unchecked to my Android Gradle based project?

In my case, adding this to the build.gradle file of the affected module:

gradle.projectsEvaluated {    tasks.withType(JavaCompile) {         options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"     } } 
like image 187
Andrew Avatar answered Sep 21 '22 16:09

Andrew