Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure sourceCompatibility and compilerArgs for all modules, java, android application and android library?

I want to see lint errors in the console and I want to configure to use java 7 just once instead of every module (we have 12 modules).

I put this into my root build.gradle:

allprojects {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xlint:deprecation,unchecked"
        sourceCompatibility = 1.7
        targetCompatibility = 1.7
    }
}

and it does work for pure java projects (apply plugin: 'java' in its own build.gradle) but not for com.android.application and com.android.library modules.

I assume there is some more general "filter" than withType(JavaCompile) I would have to use but I can't find it. Gradle scripts are still magic to me. Poking around I tried JavaCompile's super class AbstractCompile but that didn't do the trick.

How can I avoid having to add

android {
    …
    compileOptions {
        targetCompatibility 1.7
        sourceCompatibility 1.7
    }
}

for the Java version and whatever it would take for the compilerArgs to every Android module?

like image 598
Giszmo Avatar asked Dec 13 '16 06:12

Giszmo


People also ask

Where should I add library in Android Studio?

To use your Android library's code in another app module, proceed as follows: Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Library Dependency in the dropdown. In the Add Library Dependency dialog, use the search box to find the library to add.

What is settings Gradle in Android?

The Gradle settings file The settings.gradle file, located in the root project directory, defines project-level repository settings and tells Gradle which modules it should include when building your app. For most projects, the file looks like the following by default: pluginManagement {

What is Gradle script in Android?

gradle files are the main script files for automating the tasks in an android project and are used by Gradle for generating the APK from the source files.


1 Answers

Try this, it should do the trick for both Android and Java project.

allprojects {
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:deprecation,unchecked"
            sourceCompatibility = 1.7
            targetCompatibility = 1.7
        }
    }
}
like image 64
ToYonos Avatar answered Oct 02 '22 15:10

ToYonos