【问题标题】:Cannot get property 'compileSdkVersion' on extra properties extension as it does not exist Open File无法在额外属性扩展上获取属性“compileSdkVersion”,因为它不存在 打开文件
【发布时间】:2018-05-19 02:39:24
【问题描述】:

我将从 GitHub 下载的项目作为模块导入到我的 Android Studio 项目中。 “导入模块...”向导工作正常,但是当 Adroid Studio 尝试重建项目时,它返回了这个错误:

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

错误与导入模块的“build.gradle”文件中的这一行有关:

compileSdkVersion rootProject.compileSdkVersion

我尝试在项目“build.gradle”中添加“ext”部分,如下所示:

ext {
    compileSdkVersion 26
}

但是这样我收到一个新的错误:

Gradle DSL method not found: 'compileSdkVersion()' Possible causes: ... 

【问题讨论】:

  • 添加你的 build.gradle

标签: android android-studio android-gradle-plugin build.gradle gradle-plugin


【解决方案1】:

在 build.gradle 中,你需要在 android 标签下编写 compilesdkversion,如下例所示:

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

顺便说一句。您可以将该模块构建为库,然后将其作为 .aar 文件导入到您的项目中。

【讨论】:

    【解决方案2】:

    将您的 .gradle android 部分更改为此

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

    【讨论】:

      【解决方案3】:

      在您的顶级文件中使用:

      ext {
          compileSdkVersion = 26
      }
      

      在您的module/build.gradle 文件中使用:

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

      【讨论】:

      • 如果我在顶层 (build.gradle) 中添加块“ext { compileSdkVersion = 26 }”,构建操作会返回此错误:错误:(26, 1) 发生问题评估根项目“mySticker”。 > 在 org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension 类型的对象上找不到参数 [26] 的方法 compileSdkVersion()。
      • 您确定 = 符号吗?因为如果你添加 compileSdkVersion 26 without sign 就会发生这个错误
      • 什么是“顶级文件”?
      • @Isaac 是的,你可以。
      • 不管我做什么,我也得到了这个Could not find method compileSdkVersion() for arguments。现在我到处硬编码版本。有人可以举一个完整文件内容的例子吗?!
      【解决方案4】:

      另一种方式:

      您在顶级模块中的build.gradle

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

      您在应用程序模块中的build.gradle

      android {
          def buildConfig = rootProject.extensions.getByName("ext")
      
          compileSdkVersion buildConfig.compileSdk
          buildToolsVersion buildConfig.buildTools
          defaultConfig {
              minSdkVersion buildConfig.minSdk
              targetSdkVersion buildConfig.targetSdk
          }
          // ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-11
        • 2021-06-20
        • 2021-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多