【问题标题】:Gradle sync failed: Cause: compileSdkVersion is not specifiedGradle 同步失败:原因:未指定 compileSdkVersion
【发布时间】:2018-05-25 13:58:42
【问题描述】:

我正在尝试在 android studio 中测试我的 ionic 应用程序。它抛出以下错误。

Gradle sync failed: Cause: compileSdkVersion is not specified.

有什么解决办法吗?我做错了什么。

这是我的 build.gradle 文件

apply plugin: 'com.android.application'

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
    }
}

// Allow plugins to declare Maven dependencies via build-extras.gradle.

allprojects {
    repositories {
        mavenCentral();
        jcenter()
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '4.1.0'
}

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:+'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:+'
    implementation 'com.android.support:appcompat-v7:27.+'
}

【问题讨论】:

  • 请给出编译 sdk 版本。就像在应用级别的 gradle 中一样。
  • 请分享你的gradle文件代码
  • compileSdkVersion 25 buildToolsVersion '26.0.2' defaultConfig { applicationId "com.pt.planner" minSdkVersion 21 targetSdkVersion 25 versionCode 24 versionName "2.4.0" multiDexEnabled true
  • compilesdkversion应该和targetsdkversion一样
  • @Yogesh Borhade - 请检查,我已经分享了文件详细信息

标签: android cordova ionic-framework


【解决方案1】:

您正在使用27.+android 支持 库,因此您必须将sdk 版本27 作为compileSdkVersiontargetSdkVersion 提供,否则您的项目不知道适用于哪个平台你的项目应该被建立。这些参数应该在 build.gradle(app) 中像这样在 android 目录中给出:

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    defaultConfig {
        applicationId "com.example.abc.test"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

只需将此代码粘贴到apply plugin: 'com.android.application'这一行下方

【讨论】:

  • 感谢 Ghulam Moinul Quadir,它现在可以工作了。但是构建仍然失败并给出错误 1. 找不到符号类 CallbackContext 2. 找不到符号类 CordovaPlugin 3. 找不到符号类 PluginResult 4. 找不到符号类 CordovaPlugin 请帮助
  • @AmitAnand 分享你build.gradle(ProjectName)
【解决方案2】:

请在你的 gradle 文件中添加下一行

  compileSdkVersion 26

请查看以下代码以供参考

android {
        compileSdkVersion 26
        buildToolsVersion '27.0.3'

        defaultConfig {
            applicationId ""
            minSdkVersion 15
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }

    }

【讨论】:

    【解决方案3】:

    注意:我的 app/build.gradle 中有一个错误,因此不是 阅读 compileSDKVersion 属性。当我评论这样一行时, 错误消失了:

    //def enableHermes = project.ext.react.get("enableHermes", false);
    

    首先我要点击这个链接:

    ReactNative: Building from source

    然后这个:

    ReactNative: android-development-environment

    然后您可以将其添加到您的 settings.gradle 文件中:

    //
    include ':ReactAndroid'
    //
    project(':ReactAndroid').projectDir = new File(
        rootProject.projectDir, '../node_modules/react-native/ReactAndroid')
    

    对于我的 ReactViro 示例项目,我还必须从 react-native node_modules 目录添加依赖项:

        implementation project(':arcore_client') // remove this if AR not required
        implementation project(':gvr_common')
        implementation project(path: ':viro_renderer')
        implementation project(path: ':react_viro')
    

    在我的 settings.gradle 中:

    //
    include ':react_viro', ':arcore_client', ':gvr_common', ':viro_renderer'
    project(':arcore_client').projectDir = new File('../node_modules/react-viro/android/arcore_client')
    project(':gvr_common').projectDir = new File('../node_modules/react-viro/android/gvr_common')
    project(':viro_renderer').projectDir = new File('../node_modules/react-viro/android/viro_renderer')
    project(':react_viro').projectDir = new File('../node_modules/react-viro/android/react_viro')
    //
    

    【讨论】:

      【解决方案4】:

      在我的情况下,错误来自我自己的插件,我通过在我的 plugin.xml 文件中添加以下行来修复它:(我发现它比更新插件的 gradle 文件更简单)

          <platform name="android">
              <preference name="android-compileSdkVersion" value="30" />
               ...
      

      【讨论】:

      • 它再次停止工作。不知道为什么科尔多瓦对我来说有点断断续续。
      • cordova 平台添加 android 抛出错误,但构建仍在运行。将插件的 gradle 文件更新为 dependencies { implementation "androidx.biometric:biometric:1.1.0" } 提交了我的问题
      【解决方案5】:

      就我而言,我通过替换旧插件应用语法解决了这个问题:

      apply plugin: 'com.android.library'
      apply plugin: 'kotlin-android'
      

      在我所有的 build.gradle 文件中使用新插件 DSL:

      plugins {
          id "com.android.application"
          id "kotlin-android"
      }
      

      【讨论】:

        猜你喜欢
        • 2017-12-06
        • 2018-09-19
        • 2018-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多