【问题标题】:How can I set the compileOptions for my Gradle Java plugin?如何为我的 Gradle Java 插件设置 compileOptions?
【发布时间】:2021-01-19 00:05:45
【问题描述】:

我想在我的 gradle 构建中设置 -parameters 命令,以便我可以使用反射来访问参数的名称。看来我应该使用以下闭包来执行此操作。

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

但是compileOptions被列为只读,当我查看源代码时没有setter。

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

【问题讨论】:

    标签: java gradle


    【解决方案1】:

    请尝试:

    apply plugin: 'java'
    
    compileJava {
        options.compilerArgs << '-parameters' 
    }
    

    【讨论】:

      【解决方案2】:
      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

      【讨论】:

      • 项目级还是应用/模块级?
      【解决方案3】:

      你不能覆盖所有的选项(因为'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

      【讨论】:

        【解决方案4】:

        如果你使用的是 kotlin,那么:

        build.gradle.kts

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

        【讨论】:

          【解决方案5】:

          您可以像这样在 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
              }
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-05-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-10-30
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多