How can I make android delete old native libraries?

偶尔善良 提交于 2019-12-03 09:14:05

Ok, so I gave up on android to do this and I had to do it myself. I took some tips from here: http://www.moodstocks.com/2012/03/20/ice-cream-sandwich-why-native-code-support-sucks/.

Here's what I did:

  1. Make three zip files with the contents of the folders libs/armeabi, libs/armeabi-v7a and libs/x86 and named them libs_armeabi.zip, libs_armeabi_v7a.zip and libs_x86.zip respecively.

  2. Move the zip files to the res/raw folder.

  3. Delete the files under the three libs folders and re-create them with touch (in a -nix system), remember that ICS and JB don't delete the old files in the lib folder, so just deleting the files would cause the app to keep those files which are quite large.

  4. Verify the processor architecture programmatically with these two methods:

    public static boolean isArmv7() {
        try {
            return Build.VERSION.SDK_INT >= 4 && Build.class.getField("CPU_ABI").get(null).toString().startsWith("armeabi-v7");
        } catch (Throwable e) {}
    
        return false;
    }
    
    public static boolean isX86() {
        try {
            return Build.VERSION.SDK_INT >= 4 && Build.class.getField("CPU_ABI").get(null).toString().startsWith("x86");
        } catch (Throwable e) {}
    
        return false;
    }
    
  5. Depending on the processor architecture unzip one of the three files programmatically to any folder you want, I'm using /data/data/myPackage/app_libs (using context.getDir("libs",Context.MODE_PRIVATE); returns that path).

Probably there would be more logic involved here, something like extracting the libs_armeabi.zip file, and then extract the files from either libs_armeabi_v7a.zip or libs_x86.zip or none of them, overwriting the files that have the same name (I'm just wandering here because I haven't actually tried that), fortunately for me my libs on the three files were already renamed and are exclusive on each zip file, so I just need to extract one of the three files.

  1. Replace the System.loadLibrary method calls to System.load which supports loading the libraries using its full path, and add the full path for each library.

This method comes with added benefits:

  • As the libraries are zipped the apk size will be reduced, making the application in the google play store smaller.

  • Being the libraries stored inside the raw resources android won't copy them automatically, allowing you to copy them wherever you want and load them using a full path.

  • You can decide to copy the libraries to the sd card if you want, I wouldn't recommend that but now you're able to do it, for example when the phone doesn't have enough space.

Hope this helps!

All files in native lib directory has owner is System. So in your app, you can not delete these file without root permission.

Move your lib directory to res/raw, then manually copy them to internal data is a good choice.

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