【问题标题】:Gradle DSL method not found: 'compileSdkVersion()' after importing a module into the project未找到 Gradle DSL 方法:将模块导入项目后出现“compileSdkVersion()”
【发布时间】:2017-09-06 07:45:52
【问题描述】:

我在 Android Studio 中有一个项目。我需要在这个项目中导入一个模块:https://github.com/square/android-times-square/ 这个包中有一个库和一个示例。我将它们都导入。 我下载 .zip 并在 Android Studio 中执行 File -> New -> Import Module 之后我得到 graddle 错误:

Error:(16, 0) Gradle DSL method not found: 'compileSdkVersion()'
Possible causes:
The project 'MyProject' may be using a version of the Android Gradle plug-in that does not contain the method 
(e.g. 'testCompile' was added in 1.1.0).
Upgrade plugin to version 2.3.3 and sync project. The project 'MyProject' may be using a version of Gradle that does not contain the method.
Open Gradle wrapper file. The build file may be missing a Gradle plugin.
Apply Gradle plugin

我已单击此错误消息中列出的所有链接。但这并没有帮助。 gradle 脚本有什么问题? 我不明白为什么有这么多的gradle文件我需要修复哪个(((

build.gradle(我的项目)

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

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

ext {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    minSdkVersion 16
    targetSdkVersion 25
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(库)

apply plugin: 'com.android.library'
apply plugin: 'checkstyle'

task checkstyle(type: Checkstyle) {
  configFile rootProject.file('checkstyle.xml')
  source 'src/main/java'
  ignoreFailures false
  showViolations true
  include '**/*.java'

  classpath = files()
}

afterEvaluate {
  if (project.tasks.findByName('check')) {
    check.dependsOn('checkstyle')
  }
}

android {
  compileSdkVersion rootProject.ext.compileSdkVersion
  buildToolsVersion rootProject.ext.buildToolsVersion

  defaultConfig {
    minSdkVersion rootProject.ext.minSdkVersion
  }


  lintOptions {
    warning 'MissingTranslation'
    textReport true
    textOutput 'stdout'
  }
}

dependencies {
  testCompile deps.festandroid
  testCompile deps.junit
  testCompile deps.robolectric
  testCompile deps.intellijannotations
}

apply from: rootProject.file('gradle/gradle-mvn-push.gradle')

build.gradle(示例)

apply plugin: 'com.android.application'

android {
  compileSdkVersion rootProject.ext.compileSdkVersion
  buildToolsVersion rootProject.ext.buildToolsVersion

  compileOptions {
    sourceCompatibility rootProject.ext.sourceCompatibilityVersion
    targetCompatibility rootProject.ext.targetCompatibilityVersion
  }

  defaultConfig {
    applicationId 'com.squareup.timessquare.sample'
    minSdkVersion rootProject.ext.minSdkVersion
    targetSdkVersion rootProject.ext.targetSdkVersion
    versionCode 1
    versionName '1.0.0'
  }

  lintOptions {
    disable 'MissingTranslation'
  }
}

dependencies {
  compile project(':library')
}

build.gradle (module:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.myproject.myproject2"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:support-v4:25.3.1'
    testCompile 'junit:junit:4.12'
}

settings.gradle

include ':app', ':sample', ':library'

【问题讨论】:

    标签: android gradle android-gradle-plugin import-module


    【解决方案1】:

    错误:(16, 0) Gradle DSL 方法未找到:'compileSdkVersion()'

    您不能在顶级文件中使用此语法:

    ext {
        compileSdkVersion 25
        buildToolsVersion "25.0.2"
        minSdkVersion 16
        targetSdkVersion 25
    }
    

    你可以改用这个语法(注意=

    ext {
        // The following are only a few examples of the types of properties you can define.
        compileSdkVersion = 25
        buildToolsVersion = "26.0.1"
        ...
    }
    

    【讨论】:

    • 谢谢。我更改了顶层文件(我只留下了compileSdkVersion和buildToolsVersion)。
    • 我还更改了模块的 gradle 文件,因为选项 rootProject.ext.minSdkVersion 和 rootProject.ext.targetSdkVersion 导致了新错误(它们不存在)。我将它们更改为数字。现在还有另一个问题:错误:(46, 1) 评估项目 ':library' 时出现问题。 > 无法为 org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler 类型的对象获取未知属性“deps”。
    • @Evgenia 我的脚本只是一个例子。您也可以使用 minSdkVersion,targetSdkVersion,但使用与上面报告的 = 相同的语法。
    • 再次感谢!我没有注意到区别在于使用“=”。我已经用“deps”注释掉了这个块(库 gradle 文件中的依赖项 - 我稍后会修复或替换它,当我明白它负责什么时)并且一切正常!!!非常感谢!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 2015-09-25
    • 2015-05-19
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多