0

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?

wordy
  • 539
  • 5
  • 15
  • Why aren't you using `getResources().getAssets()` to get an `AssetManager`, and use that to get at your assets? – CommonsWare Mar 24 '12 at 13:05
  • I believe that is limited to files less than 1MB, at least on Android 2, whereas direct unzip works (at least without proguard). – wordy Mar 24 '12 at 19:29
  • `AssetManager` works fine for 1+MB files. However, they have to have a file extension that prevents them from being compressed. [`SQLiteAssetHelper`](https://github.com/jgilfelt/android-sqlite-asset-helper), for example, works perfectly well with 1+MB files, through the simple expedient of storing them a ZIP archives within the `assets/` project folder. – CommonsWare Mar 24 '12 at 19:39
  • Adding a zipped file to assests does seem to be a good alternative approach - it works, thank you. (slightly slower extracting though?) – wordy Mar 24 '12 at 22:21
  • Just hit the same problem. A file.zip in my assets directory is invalid after running proguard. – slott Mar 06 '14 at 17:01

1 Answers1

0

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.

Eric Lafortune
  • 45,150
  • 8
  • 114
  • 106
  • As far as I can tell it is deterministic and the threading is trivial. Thanks, indeed it may be a bug in android. For the moment I think CommonsWare's suggestion is the best solution - I will accept as an answer if he'd like to post as an answer (though of course not an answer to the original question). – wordy Mar 25 '12 at 09:27