ExoPlayer报NoSuchMethodError:如何仅为ExoPlayer启用Guava?
解决ExoPlayer因Guava依赖冲突导致的NoSuchMethodError问题
问题根源
你全局排除com.google.guava:guava的Gradle配置,导致ExoPlayer的AudioCapabilities类找不到ImmutableMap.Builder.buildOrThrow()方法——这个方法是Guava 28.0及以上版本才新增的,新增的.jar依赖触发了冲突(要么它自带旧版Guava,要么全局排除让ExoPlayer完全拿不到Guava)。
方案一:仅为新增的.jar依赖排除Guava
取消Gradle里的全局Guava排除配置,改为只在新增的依赖中单独排除Guava,这样ExoPlayer就能正常获取所需的Guava版本:
如果新增的是远程Maven依赖:
dependencies { // 仅给新增的依赖排除Guava implementation('你的新增依赖groupId:artifactId:版本号') { exclude group: 'com.google.guava', module: 'guava' } // 保留你的ExoPlayer依赖 implementation 'com.google.android.exoplayer:exoplayer-core:2.X.X' }
如果新增的是本地.jar文件:
- 若这个jar是你自己打包的,重新打包时排除Guava依赖;
- 若无法重新打包,可使用
jarjar工具移除jar内的Guava相关类,避免和ExoPlayer的Guava依赖冲突。
方案二:保留全局排除,只为ExoPlayer启用Guava
如果必须保留全局排除Guava的配置,可以单独为ExoPlayer强制引入兼容版本的Guava,确保它能拿到buildOrThrow()方法:
configurations.all { // 全局排除Guava exclude group: 'com.google.guava', module: 'guava' } dependencies { implementation 'com.google.android.exoplayer:exoplayer-core:2.X.X' // 强制引入Android兼容的Guava版本(需≥28.0,这里用31.0.1-android做示例) implementation('com.google.guava:guava:31.0.1-android') { force = true // 可选:限制该依赖仅被ExoPlayer相关模块使用 attributes { attribute(Bundling.BUNDLING_ATTRIBUTE, Bundling.EXTERNAL) } } }
注意:一定要选Android兼容的Guava版本(带-android后缀),且版本号不低于28.0,否则还是会缺失目标方法。
内容的提问来源于stack exchange,提问作者alpheonix
相关产品推荐
相关产品推荐

