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

34,415

Solution 1

In your top-level file use:

ext {
    compileSdkVersion = 26
}

In your module/build.gradle file use:

android {
  compileSdkVersion rootProject.ext.compileSdkVersion
  ...
}

Solution 2

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
    }
    // ...
}
Share:
34,415

Related videos on Youtube

vittochan
Author by

vittochan

Updated on June 21, 2020

Comments

  • vittochan
    vittochan about 4 years

    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: ... 
    
    • DeKaNszn
      DeKaNszn over 6 years
      Add your build.gradle
  • vittochan
    vittochan over 6 years
    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.DefaultExtraPropertiesExtens‌​ion.
  • Gabriele Mariotti
    Gabriele Mariotti over 6 years
    Are you sure about the = sign? Because this error happens if you add compileSdkVersion 26 without sign
  • jeancode
    jeancode almost 6 years
    What is the 'top level file'?
  • Gabriele Mariotti
    Gabriele Mariotti almost 6 years
    @jeancode the build.gradle in the root of the project
  • Isaac
    Isaac almost 6 years
    @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?
  • Gabriele Mariotti
    Gabriele Mariotti almost 6 years
    @Isaac Yes you can.
  • Michael Z
    Michael Z almost 6 years
    For those who not on Android: it is needed to set project.ext but not just ext
  • stackunderflow
    stackunderflow over 5 years
    sorry i cannot find "top-level" file. is there any other name it has?. mind telling us here?
  • Gabriele Mariotti
    Gabriele Mariotti over 5 years
    @jerinho The top level file is the build.gradle that you can find in the root of the project.
  • fun joker
    fun joker over 5 years
    @GabrieleMariotti Can you please help me here: stackoverflow.com/questions/53440756/…
  • fun joker
    fun joker over 5 years
    @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 ?
  • fun joker
    fun joker over 5 years
    @Isaac 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: stackoverflow.com/questions/53440756/…
  • Aabir Hussain
    Aabir Hussain about 5 years
    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 me
  • Daniel
    Daniel about 5 years
    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?!