Android Studio - 生成错误 - Android 任务已创建

Android Studio - Error Building - Android tasks have already been created

构建项目时出现以下错误:

错误:(2, 0) Android 任务已经创建。 调用 android.applicationVariants 时会发生这种情况, android.libraryVariants 或 android.testVariants。 一旦调用这些方法,就不可能 继续配置模型。

根目录 build.gradle:

buildscript {
      repositories {
        jcenter()
      }
      dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
      }
}
apply plugin: 'android'

android {
    compileSdkVersion 'Google Inc.:Google APIs:19'
    buildToolsVersion '19.1.0'
    defaultConfig {
       versionCode 5
       versionName '5'
       targetSdkVersion 19
       minSdkVersion 10
       applicationId 'tsp.movil'
    }
}
dependencies {
     compile 'com.google.android.gms:play-services:7.0.0'
     compile 'com.android.support:support-v4:22.1.1'
}

应用build.gradle:

android {
     compileSdkVersion 'Google Inc.:Google APIs:19'
     buildToolsVersion '19.1.0'
     defaultConfig {
        applicationId "tsp.movil"
        minSdkVersion 10
        targetSdkVersion 19
        versionCode 5
        versionName '5'
     }
     buildTypes {
         release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),      'proguard-rules.txt'
         }
     }
     productFlavors {
     }
}
dependencies {
    compile 'com.android.support:support-v4:19.1.0'
    compile 'com.google.android.gms:play-services:+'
    compile files('libs/zbar.jar')
}

看看这个: How to define common android properties for all modules using gradle 第三条评论说 Android 任务已经创建。调用 android.applicationVariants、android.libraryVariants 或 android.testVariants 时会发生这种情况。一旦调用这些方法,就无法继续配置模型。

您的顶级 build.gradle 文件应该只包含项目所有模块通用的配置。

进行以下更改:

build.gradle文件应该只有这段代码

buildscript {
      repositories {
        jcenter()
      }
      dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
      }
}

allprojects{
    repositories {
        jcenter()
      }
}

我遇到过类似的问题,但出现相同的错误消息。在我的案例中,问题是我在包中创建了一个模块,然后在 (MacOS) Finder 中手动删除了它。

问题是我也忘记从 settings.gradle 文件中删除 include ::myModule 行。