【问题标题】:Changing the kotlin version results in change in compilesdkversion for some reasons由于某些原因,更改 kotlin 版本会导致 compilesdkversion 发生变化
【发布时间】:2021-12-02 21:26:31
【问题描述】:

我克隆了一个颤振项目,并尝试在安卓设备上运行它。但是我遇到了这个错误:

任务“:app:checkDebugAarMetadata”执行失败。 发生多个任务操作失败: 执行 com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction 时发生故障 > 指定的 minCompileSdk (31) 依赖项的 AAR 元数据(META-INF/com/android/build/gradle/aar-metadata.properties) 大于此模块的 compileSdkVersion (android-30)。 依赖:androidx.window:window-java:1.0.0-beta04。 AAR 元数据文件:C:\Users\Harshit.gradle\caches\transforms-2\files-2.1\ac98fb722099a50563f635966aedbe29\jetified-window-java-1.0.0-beta04\META-INF\com\android\build\gradle\a ar-metadata.properties。

错误提示当前compileSdkVersion是30,但是有些依赖只对31有效。

所以,我只是在我的应用级 build.gradle 中将 compileSdkVersion 和 targetSdkVersion 从 30 更改为 31,然后我尝试再次运行它,但又遇到了另一个错误:

任务:location:compileDebugKotlin e:在依赖项中发现不兼容的类。从类路径中删除它们或使用“-Xskip-metadata-version-check”来抑制错误 e: C:/Users/Harshit/.gradle/caches/transforms-2/files-2.1/1028a8ca100cbb0fda6fd6257a450fc1/jetified-window-1.0.0-beta04-api.jar!/META-INF/window_release.kotlin_module: 模块已编译使用不兼容的 Kotlin 版本。其元数据的二进制版本是 1.5.1,预期版本是 1.1.15。

在谷歌快速搜索后,我找到了一个修复,我需要在 android/build.gradle 文件中提及最新的 kotlin 版本,所以我改变了这个:

ext.kotlin_version = '1.3.50'

到这里:

ext.kotlin_version = '1.6.0'

并下载了相应的 kotlin 插件。然后我再次尝试运行项目,却遇到了这个错误:

任务“:google_maps_flutter:checkDebugUnitTestAarMetadata”执行失败。 发生多个任务操作失败: 执行 com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction 时发生故障 > 指定的 minCompileSdk (31) 依赖项的 AAR 元数据(META-INF/com/android/build/gradle/aar-metadata.properties) 大于此模块的 compileSdkVersion (android-29)。 依赖:androidx.window:window-java:1.0.0-beta04。 AAR 元数据文件:C:\Users\Harshit.gradle\caches\transforms-2\files-2.1\9d7a6c54f576894d03496d57f9d5a318\jetified-window-java-1.0.0-beta04\META-INF\com\android\build\gradle\a ar-metadata.properties。

这个错误和第一个错误是一样的,只是这里提到了当前的compileSdkVersion是29,尽管我在设置为31之后并没有改变它。

所以,出于某种原因,升级 kotlin 的版本会导致 compileSdkVersin 自动设置为 29,我不知道为什么。

我怎样才能摆脱这个错误,即当前的 compileSdkVersion 是 29,即使我将它设置为 31?

为什么更改 kotlin 版本会欺骗它相信 compileSdkVersion 是 29,即使它的值在 build.gradle 中设置为 31?

android/build.gradle:

buildscript {
    ext.kotlin_version = '1.6.0'
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.8'
        
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app/build.gradle:

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 31

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.pranshupandya.locus_stalker"
        minSdkVersion 20
        targetSdkVersion 31
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:28.2.0')
    implementation 'com.google.firebase:firebase-analytics'
    def multidex_version = "2.0.1"
    implementation "androidx.multidex:multidex:$multidex_version"
}
apply plugin: 'com.google.gms.google-services'

【问题讨论】:

    标签: android flutter kotlin gradle


    【解决方案1】:
    1. 尝试使用以下方法检查颤振通道:颤振通道。
    2. 如果是 beta 版本,则使用:flutter channel stable 更改为稳定版本
    3. 然后扑腾干净。 flutter pub get 然后运行项目。

    注意:不要尝试为 Flutter 项目修改现有的 kotlin/java 配置。

    【讨论】:

      【解决方案2】:
      android {
          compileSdkVersion 31  // update to 31 from 30 
      
          sourceSets {
              main.java.srcDirs += 'src/main/kotlin'
          }
      
          defaultConfig {
              // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
              applicationId "com.ryx.mallties"
              minSdkVersion 21
              targetSdkVersion 31  // update to 31 from 30 
              versionCode flutterVersionCode.toInteger()
              versionName flutterVersionName
          }
      
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案3】:
      1. 转到项目结构并检查您的 SDK 是否正确映射到最新版本。我有 JavaSDK 17 和 Android sDK 30

      导航到您的项目 build.gradle

      确保您已为 compileSdkVersion targetSdkVersion

      我让他们早先输入相同的值,但错误仍然存​​在。因此,请确保它们比另一个少一个。总之应该是两个不同的版本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-07
        • 2011-10-12
        • 2012-08-19
        • 1970-01-01
        相关资源
        最近更新 更多