Monday, July 30, 2018

Android force compress sqlite with jpg extension

Leave a Comment

I have an old class in an Android app which copies a pre-generated sqlite database. Up to Android 2.3 we needed to change the sqlite extension to an extension which won't be compressed (like jpg as in this question). As we stop the support for Android prior to 4.1 this is no longer needed.

I want to reduce the apk size, but I want to avoid changing the database's filename. Is there a way to mark the file so it will be compressed when creating the apk although it has the jpg extension?

2 Answers

Answers 1

Which filetypes are not compressed is controlled by the noCompress property :

android {      aaptOptions {         noCompress "txt"     }      .... 

The corresponding aapt option is -0 :

-0 specifies an additional extension for which such files will not be stored compressed in the .apk. An empty string means to not compress any files at all.

Unfortunately it seems that you can either add new filetypes to the default list or completely disable compression, but not remove a filetype.

So the only solution I can think of would be to do it after the build. For example, with the Linux / Cygwin zip and unzip :

Extract the file :

$ unzip my_app.apk assets/*.jpg Archive:  my_app.apk  extracting: assets/some_file.jpg 

Replace it in the apk, (by default zip always compress) :

$ zip -r my_app.apk assets/* updating: assets/some_file.jpg (deflated 91%) 

Realign the apk (before signing, as we use the new apksigner) :

$ zipalign 4 my_app.apk my_app_aligned.apk 

Sign it :

$ apksigner sign --ks my_keystore.keystore -v -out my_app_aligned_signed.apk.apk my_app_aligned.apk Keystore password for signer #1: ********** Signed 

Answers 2

Instead of avoiding to properly change the filename suffix from jpg to db, it might be better to see if the file with the old one suffix still exists on the device and then rename it accordingly, before opening it - so that the databases of previous versions would seamlessly be migrated (and also, to be able to open it on a computer with the correct handler). just changing the filename will otherwise lead to amnesia due to an inaccessible database file.

aaptOptions {noCompress "db"} might be option, but most likely this is not required, unless the file's content would otherwise be corrupted (a smaller package size is ordinary to be preferred).

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment