3

I'm putting the following at the end of my project gradle file:

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs += 
            ['-Xep:MissingOverride:ERROR', 
             '-Xep:MissingCasesInEnumSwitch:ERROR',]
    }
}

However, in my code if I remove an @Override annotation somewhere or remove a switch statement, my Android project still builds. Shouldn't it fail with an error?

ADev
  • 5,259
  • 2
  • 16
  • 29
  • I don't know if it behaves different for android projects but for our java EE build I implemented this: `compileJava{ options.fork = true options.forkOptions.executable = 'javac' options.compilerArgs.addAll( [ '-APackageAcronym='+gradle.ext.packageAcronym, '-s', gradle.ext.generatedQueriesDir ] ) }` since compileJava is it's own task. – Nico Feb 16 '17 at 14:10
  • Do you put `compileJava` at the root level? If I do so in my case I get the following error message: "Error: Could not find method compileJava() ..." – ADev Feb 16 '17 at 14:21
  • 1
    Yes I put it at the root level. I think it's different for android gradle builds? Just thought to give it a shot :) Sorry I wasted you're time. – Nico Feb 16 '17 at 14:45
  • No, thanks for the suggestion! Appreciate you giving the time. :) – ADev Feb 16 '17 at 14:51
  • 1
    I googled a bit and it seems android really hides the task compileJava and denies access to it. But I found this peace which seems to work for other users `allprojects { gradle.projectsEvaluated { tasks.withType(JavaCompile) { options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" } } }` found here: http://stackoverflow.com/questions/18689365/how-to-add-xlintunchecked-to-my-android-gradle-based-project information for compileJava task: http://stackoverflow.com/questions/16853130/run-task-before-compilation-using-android-gradle-plugin – Nico Feb 17 '17 at 06:22
  • Thanks a lot! That works. Would you please write your comment as an answer so I can approve it? – ADev Feb 17 '17 at 10:45

1 Answers1

14

I googled your problem and found this post that described your problem and the solution looks like this:

allprojects { 
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }
}

As is seems the gradle build is different for android projects than for java projects and you can't access the task CompileJava itself as stated in my first comment.

superjos
  • 12,189
  • 6
  • 89
  • 134
Nico
  • 1,727
  • 1
  • 24
  • 42
  • Sir, I salute you. This was a pain, and you solved it! I wonder why Android Studio does not have a clear option for that in the project settings. Anyway, thanks! – Danny E.K. van der Kolk Apr 02 '22 at 13:09