java - 找不到 Gradle DSL 方法 :'compileOptions()'

标签 java android gradle gradle-plugin

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "22.0.1"


    defaultConfig {
        applicationId "com.myapp.new"
        minSdkVersion 19
        targetSdkVersion 19
        versionCode 51
        versionName "2.0.5"
    }



    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.org'
        }
    }
}


compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibsility JavaVersion.VERSION_1_7
}



dependencies {
    //testCompile 'junit:junit:4.12'

    //compile 'com.android.support:appcompat-v7:19.0.0'

}

################################################################################
Task.java

package bolts;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Represents the result of an asynchronous operation.
 * 
 * @param <TResult>
 *          The type of the result of the task.
 */
public class Task<TResult> {
  /**
   * An {@link java.util.concurrent.Executor} that executes tasks in parallel.
   */
  public static final ExecutorService BACKGROUND_EXECUTOR = BoltsExecutors.background();

  /**
   * An {@link java.util.concurrent.Executor} that executes tasks in the current thread unless
   * the stack runs too deep, at which point it will delegate to {@link Task#BACKGROUND_EXECUTOR} in
   * order to trim the stack.
   */
  private static final Executor IMMEDIATE_EXECUTOR = BoltsExecutors.immediate();

  /**
   * An {@link java.util.concurrent.Executor} that executes tasks on the UI thread.
   */
  public static final Executor UI_THREAD_EXECUTOR = AndroidExecutors.uiThread();

  /**
   * Interface for handlers invoked when a failed {@code Task} is about to be
   * finalized, but the exception has not been consumed.
   *
   * <p>The handler will execute in the GC thread, so if the handler needs to do
   * anything time consuming or complex it is a good idea to fire off a {@code Task}
   * to handle the exception.
   *
   * @see #getUnobservedExceptionHandler
   * @see #setUnobservedExceptionHandler
   */
  public interface UnobservedExceptionHandler {
    /**
     * Method invoked when the given task has an unobserved exception.
     * <p>Any exception thrown by this method will be ignored.
     * @param t the task
     * @param e the exception
     */
    void unobservedException(Task<?> t, UnobservedTaskException e);
  }

  // null unless explicitly set
  private static volatile UnobservedExceptionHandler unobservedExceptionHandler;

  /**
   * Returns the handler invoked when a task has an unobserved
   * exception or {@code null}.
   */
  public static UnobservedExceptionHandler getUnobservedExceptionHandler() {
    return unobservedExceptionHandler;

即使下载了jdk 1.7并安装了,还是出现这个错误

错误:(27, 0) 找不到 Gradle DSL 方法:'compileOptions()' 可能原因:项目'app'可能使用了不包含该方法的Android Gradle插件版本(例如'testCompile'是在1.1.0中添加的)。 fixGradleElements>Fix plugin version and sync project项目'app'可能使用了不包含该方法的Gradle版本。 open.wrapper.file>打开 Gradle 包装器文件。构建文件可能缺少 Gradle 插件。 apply.gradle.plugin">应用 Gradle 插件

最佳答案

虽然你打错了,但真正的问题是:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

应该像这样在 android {} 闭包中:

android {
    compileSdkVersion 19
    buildToolsVersion "22.0.1"


    defaultConfig {
        applicationId "com.myapp.new"
        minSdkVersion 19
        targetSdkVersion 19
        versionCode 51
        versionName "2.0.5"
    }

    //FIX
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.org'
        }
    }
}

关于java - 找不到 Gradle DSL 方法 :'compileOptions()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35367213/

相关文章:

maven - Grails插件安装

Java多态性-从父映射获取子命令

java - 在 JSP 中格式化 JSON 日期

android - 在现有的 IntelliJ Ultimate 安装上安装 Android Studio

java - "Could not find mopub-android-sdk.apk!"

android - 无法在Android Studio中运行应用

gradle - 在多模块项目中,Gradle 可以将插件构建为一个模块,然后在同一个构建中使用该插件吗?

java - 使用 JSch 列出 SFTP 中的前 N ​​个文件

java - 开始 GWT 和 Java - 从 LAMP 背景出发时,哪条路最好?

android - 不为联系人调用 onActivityResult