Cannot get property 'compileSdkVersion' on extra properties extension as it does not exist Open File
Asked Answered
S

4

28

I imported a project downloaded from GitHub into my Android Studio project as module. The "Import module..." wizard worked fine, but when the Adroid Studio tried to rebuild the project, it returned me this error:

Cannot get property 'compileSdkVersion' on extra properties extension as it does not exist Open File

The error is related to this line in the "build.gradle" file of the imported module:

compileSdkVersion rootProject.compileSdkVersion

I tried to add "ext" section in the project "build.gradle" like this:

ext {
    compileSdkVersion 26
}

But in this way I receive a new error:

Gradle DSL method not found: 'compileSdkVersion()' Possible causes: ... 
Supertanker answered 5/12, 2017 at 12:23 Comment(1)
Add your build.gradleWitte
C
48

In your top-level file use:

ext {
    compileSdkVersion = 26
}

In your module/build.gradle file use:

android {
  compileSdkVersion rootProject.ext.compileSdkVersion
  ...
}
Cirilla answered 5/12, 2017 at 13:45 Comment(14)
If I add the block "ext { compileSdkVersion = 26 }" in the top-level (build.gradle), the build action return me this error: Error:(26, 1) A problem occurred evaluating root project 'mySticker'. > Could not find method compileSdkVersion() for arguments [26] on object of type org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension.Supertanker
Are you sure about the = sign? Because this error happens if you add compileSdkVersion 26 without signCirilla
What is the 'top level file'?Dionisio
@Dionisio the build.gradle in the root of the projectCirilla
@GabrieleMariotti: Thanks that it solved for properties compileSdkVersion but it gives the same error but on another property buildToolsVersion. I assume that I can apply same way for this property too?Falcongentle
@Falcongentle Yes you can.Cirilla
For those who not on Android: it is needed to set project.ext but not just extStraightforward
sorry i cannot find "top-level" file. is there any other name it has?. mind telling us here?Roussillon
@jerinho The top level file is the build.gradle that you can find in the root of the project.Cirilla
@GabrieleMariotti Can you please help me here: #53441256Mcelhaney
@GabrieleMariotti I followed all your steps mentioned in the answer. So I have made changes in android -> app->build.gradle file and android-> build.gradle file is it correct ?Mcelhaney
@Falcongentle it solved for properties compileSdkVersion but it gives the same error for another property buildToolsVersion after build tools it says minSdkVersion why am I getting all this ? Can you please help me here: #53441256Mcelhaney
it will be better for freshers like me if you give an example for rootProject and top-level in your answer. Thanks BTW it works for meConjugal
I also get this Could not find method compileSdkVersion() for arguments, no matter what I do. Now I hardcoded the version everywhere. Can somebody give an example with whole file contents, please?!Blackett
A
3

Another way:

Your build.gradle in top-level module

ext {
    minSdk = 21
    targetSdk = 29
    compileSdk = 29
    buildTools = '29.0.3'
}

Your build.gradle in app module

android {
    def buildConfig = rootProject.extensions.getByName("ext")

    compileSdkVersion buildConfig.compileSdk
    buildToolsVersion buildConfig.buildTools
    defaultConfig {
        minSdkVersion buildConfig.minSdk
        targetSdkVersion buildConfig.targetSdk
    }
    // ...
}
Anchovy answered 20/6, 2020 at 15:14 Comment(0)
H
-1

In build.gradle you need to write compilesdkversion under android tag as in this example:

android { .. compileSdkVersion 26 // 26 is an example ..}

By the way. You can build that module as library then import it into your project as .aar file.

Hesychast answered 5/12, 2017 at 12:29 Comment(0)
G
-2

Change your .gradle android part to this

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "your App id"
        minSdkVersion 18
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
Godship answered 5/12, 2017 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.