简体   繁体   English

找不到Gradle DSL方法:“ compileOptions()”

[英]Gradle DSL method not found:'compileOptions()'

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;

Even after downloading the jdk 1.7 and installing it, I still have this error 即使下载并安装了jdk 1.7,我仍然遇到此错误

Error:(27, 0) Gradle DSL method not found: 'compileOptions()' Possible causes: The project 'app' may be using a version of the Android Gradle plug-in that does not contain the method (eg 'testCompile' was added in 1.1.0). 错误:(27,0)未找到Gradle DSL方法:'compileOptions()'可能的原因:项目'app'可能正在使用不包含该方法的Android Gradle插件版本(例如,'testCompile'为在1.1.0中添加)。 fixGradleElements>Fix plugin version and sync projectThe project 'app' may be using a version of Gradle that does not contain the method. fixGradleElements>修复插件版本并同步项目项目'app'可能正在使用不包含该方法的Gradle版本。 open.wrapper.file>Open Gradle wrapper file.The build file may be missing a Gradle plugin. open.wrapper.file>打开Gradle包装器文件。构建文件可能缺少Gradle插件。 apply.gradle.plugin">Apply Gradle plugin apply.gradle.plugin“>应用Gradle插件

although you have a typo the real problem is that this: 尽管您有错字,但真正的问题是:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

Should be inside the android {} closure like this: 应该在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'
        }
    }
}

Your problem will be raised by this property targetCompatibsility , must be targetCompatibility 此属性targetCompatibsility将引发您的问题,必须是targetCompatibility

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

btw, are you sure that don´t need to define some dependencies inside your build.gradle file : 顺便说一句,您确定不需要在build.gradle文件中定义一些依赖build.gradle

dependencies {

      ???
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM