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

Android应用APK与Android Studio运行时动画表现不一致问题

解决方案

1. 修复ProGuard/R8混淆规则

Release包开启混淆后,ExoPlayer核心类可能被混淆导致功能异常,在proguard-rules.pro中添加以下规则:

# 保护ExoPlayer/Media3相关类不被混淆
-keep class com.google.android.exoplayer2.** { *; }
-keep interface com.google.android.exoplayer2.** { *; }
-keep class androidx.media3.** { *; }
-keep interface androidx.media3.** { *; }

2. 禁止压缩MP4资源

打包时默认压缩会损坏MP4文件,在Module级build.gradle的release构建类型中添加不压缩配置:

android {
    buildTypes {
        release {
            // 其他配置...
            aaptOptions {
                noCompress "mp4"
            }
        }
    }
}

3. 优化资源URI解析方式

手动拼接URI容易出错,改用更可靠的方式生成资源URI:

// 替换原有的URI解析代码
val uri = Uri.Builder()
    .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
    .authority(context.packageName)
    .appendPath(source.toString())
    .build()

如果使用AndroidX Media3,可直接用官方工具方法:

val uri = RawResourceDataSource.buildRawResourceUri(source)

4. 调整播放器播放时机

避免在播放器未准备完成时调用播放,通过状态回调触发播放:

videoPlayer = ExoPlayer.Builder(context).build().also { player ->
    player.setMediaItem(MediaItem.Builder().setUri(uri).build())
    player.repeatMode = ExoPlayer.REPEAT_MODE_ALL
    // 添加播放状态监听
    player.addListener(object : Player.Listener {
        override fun onPlaybackStateChanged(playbackState: Int) {
            super.onPlaybackStateChanged(playbackState)
            if (playbackState == Player.STATE_READY && playOnStart) {
                player.play()
            }
        }
    })
    player.prepare()
}

5. 验证资源是否被正确打包

检查src/main/res/raw目录下的bumper_animation_light.mp4文件是否存在,且未被构建脚本排除。可解压Release APK,查看res/raw目录下是否包含该文件。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 04:35:07