You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级API34后仍无法适配Media3依赖,如何修改build.gradle?

解决Media3依赖编译错误问题

我在构建视频播放器时,添加以下3个Media3依赖后出现编译错误:

implementation("androidx.media3:media3-exoplayer:1.3.1")
implementation("androidx.media3:media3-exoplayer-dash:1.3.1")
implementation("androidx.media3:media3-ui:1.3.1")

报错信息:

  1. Dependency 'androidx.media3:media3-exoplayer-dash:1.3.1' requires libraries and applications that
    depend on it to compile against version 34 or later of the
    Android APIs.

    :app is currently compiled against android-33.

    Also, the maximum recommended compile SDK version for Android Gradle
    plugin 7.4.2 is 33.

    Recommended action: Update this project's version of the Android Gradle
    plugin to one that supports 34, then update this project to use
    compileSdkVerion of at least 34.

    Note that updating a library or application's compileSdkVersion (which
    allows newer APIs to be used) can be done separately from updating
    targetSdkVersion (which opts the app in to new runtime behavior) and
    minSdkVersion (which determines which devices the app can be installed
    on).

我已经尝试将模块的compileSdkVersion和targetSdkVersion更新为34,但问题仍未解决,以下是模块完整的build.gradle文件:

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'maven-publish'
group = 'expo.modules.webview'
version = '0.5.0'
buildscript {
  def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
  if (expoModulesCorePlugin.exists()) {
    apply from: expoModulesCorePlugin
    applyKotlinExpoModulesCorePlugin()
  }

  // Simple helper that allows the root project to override versions declared by this library.
  ext.safeExtGet = { prop, fallback ->
    rootProject.ext.has(prop)? rootProject.ext.get(prop) : fallback
  }
  // Ensures backward compatibility
  ext.getKotlinVersion = {
    if (ext.has("kotlinVersion")) {
      ext.kotlinVersion()
    } else {
      ext.safeExtGet("kotlinVersion", "1.9.20")
    }
  }
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${getKotlinVersion()}")
  }
}
afterEvaluate {
  publishing {
    publications {
      release(MavenPublication) {
        from components.release
      }
    }
    repositories {
      maven {
        url = mavenLocal().url
      }
    }
  }
}
android {
  compileSdkVersion safeExtGet("compileSdkVersion", 34)

  def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
  if (agpVersion.tokenize('.')[0].toInteger() < 8) {
    compileOptions {
      sourceCompatibility JavaVersion.VERSION_11
      targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
      jvmTarget = JavaVersion.VERSION_11.majorVersion
    }
  }

  namespace "expo.modules.webview"
  defaultConfig {
    minSdkVersion safeExtGet("minSdkVersion", 21)
    targetSdkVersion safeExtGet("targetSdkVersion", 34)
    versionCode 1
    versionName "0.5.0"
  }
  lintOptions {
    abortOnError false
  }
  publishing {
    singleVariant("release") {
      withSourcesJar()
    }
  }
  // Enables Compose functionality
  buildFeatures {
    compose true
  }
  // Kotlin compiler version must be tied to Kotlin version
  composeOptions {
    kotlinCompilerExtensionVersion = "1.5.10"
  }
}
repositories {
  mavenCentral()
}
dependencies {
  implementation project(':expo-modules-core')
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
  implementation("androidx.media3:media3-exoplayer:1.3.1")
  implementation("androidx.media3:media3-exoplayer-dash:1.3.1")
  implementation("androidx.media3:media3-ui:1.3.1")
  // Importing Jetpack Compose
  implementation(platform("androidx.compose:compose-bom:2024.05.00"))
  // and ui library
  implementation("androidx.compose.material3:material3")
  // OPTIONAL - Android Studio Preview support
  implementation("androidx.compose.ui:ui-tooling-preview")
  debugImplementation("androidx.compose.ui:ui-tooling")

}

解决方案

问题核心是Android Gradle插件(AGP)版本不兼容:当前使用的AGP 7.4.2最高仅支持compileSdk 33,即便模块内设置了compileSdk 34,也会被AGP版本限制。按以下步骤修复:

  1. 升级AGP版本
    打开根目录下的build.gradle(非模块级),找到dependencies块中的com.android.tools.build:gradle,将版本升级到支持compileSdk 34的版本,比如:

    classpath 'com.android.tools.build:gradle:7.4.3'
    

    也可直接升级到8.x稳定版(如8.0.2),该版本要求Kotlin版本≥1.8.0,你的项目用的1.9.20完全兼容。

  2. 确认根项目compileSdkVersion为34
    模块中使用safeExtGet("compileSdkVersion", 34)会优先读取根项目定义的compileSdkVersion值。如果根项目的ext.compileSdkVersion仍为33,模块会继承该值导致编译失败。
    打开根项目的build.gradle或gradle.properties,确保配置:

    ext {
        compileSdkVersion = 34
        // 其他配置...
    }
    
  3. 清理缓存并重新编译

    • 在Android Studio中点击File -> Invalidate Caches...,勾选Invalidate and Restart重启IDE
    • 或用命令行执行:
      ./gradlew clean build --refresh-dependencies
      

内容的提问来源于stack exchange,提问作者KAT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 11:57:34