25

I want to set the -parameters command on my gradle build so that I can use reflection to access the name of the parameters. It seems like I should be doing this with the following closure.

compileJava {
    compileOptions {
        compilerArgs << '-parameters'
    }
}

But compileOptions is listed as read-only, and when I look at the source code there's no setter.

https://gradle.org/docs/current/dsl/org.gradle.api.tasks.compile.JavaCompile.html#org.gradle.api.tasks.compile.JavaCompile:options

How am I suppose to be able to tell the javac compiler what args to use in Gradle?

Groovy:       2.3.6
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM:          1.8.0_40 (Oracle Corporation 25.40-b25)
OS:           Windows 7 6.1 amd64
Jazzepi
  • 5,259
  • 11
  • 55
  • 81

5 Answers5

26

Please try:

apply plugin: 'java'

compileJava {
    options.compilerArgs << '-parameters' 
}
Opal
  • 81,889
  • 28
  • 189
  • 210
24
tasks.withType(JavaCompile) {
    configure(options) {
        options.compilerArgs << '-Xlint:deprecation' << '-Xlint:unchecked' // examples
    }
}

Source: http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
9

If you're using kotlin, then:

build.gradle.kts

tasks.withType<JavaCompile>(){
    options.compilerArgs.addAll(listOf("-nowarn", "-Xlint:none"))
}
user64141
  • 5,141
  • 4
  • 37
  • 34
6

You cannot overwrite all of the options (since 'options' property is read-only), but you can set them one by one. For example:

compileJava {
    //enable compilation in a separate daemon process
    options.fork = true

    //enable incremental compilation
    options.incremental = true
}

Check out the docs: https://gradle.org/docs/current/dsl/org.gradle.api.tasks.compile.JavaCompile.html and https://gradle.org/docs/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html

Rafal G.
  • 4,252
  • 1
  • 25
  • 41
6

You can work with Compile Options in App.gradle file like this:

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.2"
    defaultConfig {
        applicationId "com.example.aliazaz.menuapp"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    /*Add Compile options in following block*/
    compileOptions {

        //Like these 
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}
Ali Azaz Alam
  • 1,782
  • 1
  • 16
  • 27