簡體   English   中英

如何為我的 Gradle Java 插件設置 compileOptions?

[英]How can I set the compileOptions for my Gradle Java plugin?

我想在我的 gradle 構建中設置 -parameters 命令,以便我可以使用反射來訪問參數的名稱。 似乎我應該使用以下閉包來做到這一點。

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

但是 compileOptions 被列為只讀,當我查看源代碼時,沒有設置器。

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

我怎么能告訴 javac 編譯器在 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

請嘗試:

apply plugin: 'java'

compileJava {
    options.compilerArgs << '-parameters' 
}
tasks.withType(JavaCompile) {
    configure(options) {
        options.compilerArgs << '-Xlint:deprecation' << '-Xlint:unchecked' // examples
    }
}

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

您不能覆蓋所有選項(因為 'options' 屬性是只讀的),但您可以一一設置它們。 例如:

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

    //enable incremental compilation
    options.incremental = true
}

查看文檔: https : //gradle.org/docs/current/dsl/org.gradle.api.tasks.compile.JavaCompile.htmlhttps://gradle.org/docs/current/dsl/org.gradle .api.tasks.compile.CompileOptions.html

您可以在App.gradle文件中使用編譯選項,如下所示:

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
    }

}

如果您使用的是 kotlin,則:

build.gradle.kts

tasks.withType<JavaCompile>(){
    options.compilerArgs.addAll(listOf("-nowarn", "-Xlint:none"))
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM