Proguard 破坏资产或原始音频文件 [英] Proguard breaking audio file in assets or raw

查看:32
本文介绍了Proguard 破坏资产或原始音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 MediaPlayer 播放蜂鸣声的活动,它工作正常,即使在 proguarded 生产版本中也能正常工作.使用最新版本,它现在突然崩溃了

I have an activity that plays a beep sound with MediaPlayer that works fine and used to work fine even in the proguarded production version. With the latest release it now suddenly crashes with

Caused by: android.content.res.Resources$NotFoundException: File res/raw/beep.ogg from drawable resource ID #0x7f060000
at android.content.res.Resources.openRawResourceFd(Resources.java:994)
at android.media.MediaPlayer.create(MediaPlayer.java:855)
at com.digikey.mobile.activity.CaptureActivity.onCreate(SourceFile:135)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
... 11 more
Caused by: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
at android.content.res.AssetManager.openNonAssetFdNative(Native Method)
at android.content.res.AssetManager.openNonAssetFd(AssetManager.java:428)
at android.content.res.Resources.openRawResourceFd(Resources.java:991)
... 16 more

我尝试了许多不同的加载和播放声音的方法,包括将其存储在资产中而不是原始 res/raw 中,当我在尚未保护的应用程序中使用时,它们都可以正常工作,但它们都因发布而失败proguarded/zipaligned 和签名的版本.

I tried a number of different ways of loading and playing the sound including storing it in assets instead of the original res/raw and they all work when I use in the app that has not been proguarded but they all fail with the release version that is proguarded/zipaligned and signed.

我的 proguard 文件相当大,它确实包含了经常建议的修复

My proguard file is rather large and it does contain the often suggested fix of

-keepclassmembers class **.R$* {public static <fields>;}
-keep class **.R$*

有人有任何想法或遇到过这样的事情吗?

Anybody have any ideas or encountered something like this before?

更新:我也尝试过 mp3 文件,它也有同样的问题.

Update: I also tried with mp3 files and it has the same problems.

更新 2:有趣的是,这条消息似乎需要很长时间(接近 1 秒)才能崩溃.好像是在搜索或处理某个文件什么的……但文件真的很小,所以很奇怪.

Update 2: An interesting thing is that it seem to take quite a while (close to 1s) to crash with this message. As if it is searching or processing some file or something .. but the file is really small so its weird.

推荐答案

该错误表明 zip 条目 beep.ogg 被压缩,而它不应该被压缩.

The error suggests that the zip entry beep.ogg is compressed, when it shouldn't be.

可以选择压缩 zip 文件中的各个条目.这种压缩对于读取 zip 文件的应用程序通常是透明的.但是,Android 直接访问某些类型的文件(如 .ogg 和 .mp3),无需解包,这只有在未压缩时才有效.

Individual entries in zip files can optionally be compressed. This compression is typically transparent to the application that reads the zip file. However, Android accesses some types of files (like .ogg and .mp3) directly, without unpacking them, which only works if they are not compressed.

你可以看到哪些条目被压缩了

You can see which entries are compressed with

unzip -lv MyApplication.apk

在方法"列中,Defl"表示压缩,Stored"表示未压缩.

In the column 'Method', 'Defl' means compressed and 'Stored' means uncompressed.

ProGuard 不执行 apk 文件的最终打包——aapt、apkbuilder、jarsigner、zipalign 和可能的后处理步骤.在适当的阶段,他们应该不压缩 .ogg 和 .mp3 文件.您可能需要在 Maven 构建过程中检查它们的集成.

ProGuard doesn't perform the final packaging of the apk file -- aapt, apkbuilder, jarsigner, zipalign, and possible post-processing steps do. At the proper stages, they should leave .ogg and .mp3 files uncompressed. You may need to check their integration in the Maven build process.

顺便提一下,DexGuard 确实执行了最终打包.它有一个选项 -dontcompress 来控制哪些文件或文件类型不被压缩.默认配置不压缩 .ogg 和 .mp3 文件.

DexGuard incidentally does perform the final packaging. It has an option -dontcompress to control which files or file types are left uncompressed. The default configuration leaves .ogg and .mp3 files uncompressed.

(我是 ProGuard 和 DexGuard 的开发者)

(I am the developer of ProGuard and DexGuard)

Manfred Moser 更新:

Update by Manfred Moser:

这确实是问题所在.事实证明,这与 proguard 集成或 Android Maven 插件无关.该问题是由我使用的 Maven Jarsigner Plugin 配置引起的.我已将 removeExistingSignatures 设置为 true,这不是默认值并导致存档中的所有文件都被压缩.我认为这是现阶段 jarsigner 插件的一个奇怪的错误.在任何情况下......默认配置是将此参数设置为 false 所以没有人应该有任何问题......当然除非你像我一样将它设置为 true ;-)

This was indeed the problem. Turns out that this has nothing to do with the proguard integration or the Android Maven Plugin. The problem was caused by the Maven Jarsigner Plugin configuration I used. I had set removeExistingSignatures to true, which is not the default value and caused all files in the archive to be compressed. I consider this a weird bug of the jarsigner plugin at this stage. In any case... the default config is to for this parameter to be set to false so nobody should have any problem ... unless of course you set it to true like I did ;-)

这篇关于Proguard 破坏资产或原始音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆