【问题标题】:Build script error, unsupported Gradle DSL method found: 'android()'!构建脚本错误,发现不受支持的 Gradle DSL 方法:'android()'!
【发布时间】:2014-02-19 12:53:58
【问题描述】:

我使用的是 Android Studio 0.4.5,但无法同步 gradle。

当我尝试这样做时,我得到了这个错误:

Gradle 'MyApp' project refresh failed: Build script error, unsupported Gradle DSL method found: 'android()'!

我的解决方案包含 4 个模块。这是我的根build.graddle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
    }
}

android {    
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

以及其他(为简单起见,我删除了依赖项)

模块 1

apply plugin: 'android'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
    }
}buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
    }

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

模块 2

apply plugin: 'android-library'

android {
    compileSdkVersion 18
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 17
    }

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

模块 3 应用插件:'android-library'

android {
    compileSdkVersion 18
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 17
    }

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

模块 4

apply plugin: 'android-library'

android {
    compileSdkVersion 17
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 17
    }

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

抱歉,拖了这么久,但我在这里变得绝望:(

【问题讨论】:

    标签: android gradle android-studio


    【解决方案1】:

    主要原因是:

    android {    
        packagingOptions {
            exclude 'META-INF/LICENSE.txt'
        }
    }
    

    在根 build.gradle 中。

    【讨论】:

      【解决方案2】:

      Module1 build.gradle 文件中删除以下代码行:

      buildscript {
          repositories {
              mavenCentral()
          }
          dependencies {
              classpath 'com.android.tools.build:gradle:0.8.+'
          }
      }buildscript {
          repositories {
              mavenCentral()
          }
          dependencies {
              classpath 'com.android.tools.build:gradle:0.8.+'
          }
      }
      

      由于您在所有模块中使用相同的配置,因此最好将其仅放在根 gradle 文件中。

      即使你想在模块的 build.gradle 文件中使用此代码,也应该在应用 android 插件之前。

      最终Module1 build.gralde 文件:

      apply plugin: 'android'
      
      android {
          compileSdkVersion 19
          buildToolsVersion "19.0.1"
      
          defaultConfig {
              minSdkVersion 9
              targetSdkVersion 19
          }
      
          buildTypes {
              release {
                  runProguard false
                  proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
              }
          }
      }
      

      还要确保下面提到的配置在模块之间应该相同

      android {
          compileSdkVersion 19
          buildToolsVersion "19.0.1"
      
          defaultConfig {
              minSdkVersion 9
              targetSdkVersion 19
          }
       }
      

      你可以使用任何你想要的东西,但应该是一样的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-21
        • 2014-09-03
        • 1970-01-01
        相关资源
        最近更新 更多