Gradle DSL method not found: 'compileSdkVersion()' after importing a module into the project

4.7k views Asked by At

I have a project in Android Studio. I need to import a module in this project: https://github.com/square/android-times-square/ There is a library and a sample in this packege. I import both of them. I download .zip and in Android Studio do File -> New -> Import Module And after that I get the graddle error:

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

I have clicked all the links listed in this error message. But it did not help. What's wrong in gradle scripts? I don't understand why there are so many gradle files and which one I need to fix (((

build.gradle (MyProject)

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 (library)

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 (sample)

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'
1

There are 1 answers

4
Gabriele Mariotti On BEST ANSWER

Error:(16, 0) Gradle DSL method not found: 'compileSdkVersion()'

You can't use this syntax in the top-level file:

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

Instead you can use this syntax (pay attention to = )

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