How to add -Xlint:unchecked to my Android Gradle based project?

前端 未结 7 836
囚心锁ツ
囚心锁ツ 2020-11-29 16:05

I tried to add the following to the root build.gradle file:

subprojects {
    gradle.projectsEvaluated {
        tasks.withType(Compile) {
              


        
相关标签:
7条回答
  • 2020-11-29 16:09

    For everyone using gradle.kts use the following to match up with the simple build.gradle file

    build.gradle.kts

    afterEvaluate {
            tasks.withType(JavaCompile::class) {
                options.compilerArgs.add("-Xlint:unchecked")
                options.compilerArgs.add("-Xlint:deprecation")
            }
        }
    

    build.gradle

     gradle.projectsEvaluated {
            tasks.withType(JavaCompile) {
                options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
            }
        }
    
    0 讨论(0)
  • 2020-11-29 16:10

    Put this in your build.gradle file (root directory):

    allprojects { // Projects
       gradle.projectsEvaluated {
          tasks.withType(JavaCompile) {
             options.encoding = 'UTF-8'
             options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
          }
       }
    }
    
    0 讨论(0)
  • 2020-11-29 16:20

    This is what worked for me: (in your project's build.gradle)

    allprojects {
        gradle.projectsEvaluated {
            tasks.withType(JavaCompile) {
                options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 16:21

    For deprecation, you can now use this in gradle kotlin script, which is better than modifying compilerArgs because it's type-safe:

    tasks.withType<JavaCompile> {
        options.isDeprecation = true
    }
    
    0 讨论(0)
  • 2020-11-29 16:30

    Disclaimer: Even though this answer has more than 10 upvotes, it does not address the problem in the context of an Android project. However, Google finds this question in the context of non-Android projects. Thus, I keep this answer for those folks.

    According to JavaCompile, the following seems to be solution:

    compileJava {
        options.encoding = 'UTF-8'
        options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
    }
    

    If you want it to have for the test cases, use compileTestJava

    compileTestJava {
        options.encoding = 'UTF-8'
        options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
    }
    
    0 讨论(0)
  • 2020-11-29 16:31

    I had a different compilation argument to set. The following works for me.

    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-XDignore.symbol.file"
            options.bootClasspath = "$System.env.JAVA_HOME/jre/lib/rt.jar"
        }
    }
    

    You have to set the boot classpath for JDK 1.8 and above for things like Unsafe and sun.swing.* classes. Fix the source code especially for the latter, because Jigsaw Java 9, the up and coming modularity implementation for the JRE, will finally make these methods inaccessible(!). Consider yourself warned.

    0 讨论(0)
提交回复
热议问题