Unzip from assets doesn't work when using proguard

≡放荡痞女 提交于 2019-12-12 02:27:25

问题


I have a 20MB database stored in the apk's assets, which on first run is extracted for use. To do this I use

PackageManager pm = context.getPackageManager();
String apkFile = pm.getApplicationInfo(context.getPackageName(), 0).sourceDir;
ZipFile zipFile = new ZipFile(apkFile); 
ZipEntry entry = zipFile.getEntry("assets/FILENAME");
myInput = zipFile.getInputStream(entry);
myOutput = new FileOutputStream(file);
    byte[] buffer = new byte[1024*4];
int length;
int total = 0;
int counter = 1;
while ((length = myInput.read(buffer)) > 0) {
    total += length;
    counter++;
    if (counter % 32 == 0) {
        publishProgress(total);
    }
        myOutput.write(buffer, 0, length);
}

All works fine when I export from eclipse (android 2.2 target) without using proguard. When I export with proguard, the unzip starts to work for a few seconds (and a few progress updates, to 8%), but then crashes with java.io.IOException at java.util.zip.InflaterInputStream.read(.. )

It works on the emulator, but crashes on devices (many devices, but I think always works in Android 4, crashes in Android 2.2). My proguard.cfg is basically the default one. Nothing I have tried changing seems to help, any ideas?


回答1:


ProGuard optimizations may uncover bugs in the code that is processed. For instance, optimizations can potentially change the timing of multi-threaded code, causing problems if it is not synchronized properly. You could double-check your code. For instance, are myInput and myOutput fields, that are manipulated in other threads? Is the problem deterministic?

ProGuard optimizations may also uncover bugs in the virtual machine or in run-time classes. It's possible that you've run into a bug that has been fixed in recent versions of Android.

Since the processed code does work on recent versions of Android, it's probably not a bug in ProGuard, but you could check if the latest release makes a difference (version 4.7 at this time of writing).

You can report the problem on ProGuard's bug tracker, with a sample that illustrates the problem (at least with more complete code and a complete stack trace). In the meanwhile, you can work around it by switching off optimization.



来源:https://stackoverflow.com/questions/9851974/unzip-from-assets-doesnt-work-when-using-proguard

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!