如何让android删除旧的本机库? [英] How can I make android delete old native libraries?

查看:32
本文介绍了如何让android删除旧的本机库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的应用程序中删除一些本机库,因为我不再使用它们,但是在我的手机中安装了 apk 后,我可以看到本机库仍然存在.正如我所看到的,android 确实通过替换我的 libs/目录中的文件来正确复制/更新本机库,但显然当它们不在 apk 中时它不会删除它们.我怎么知道图书馆还在那里?首先,应用程序的大小保持不变,我正在删除一些非常大的库,其次,如果我保留 System.load(...) 语句,应用程序仍然能够加载库.

I'm trying to delete some native libraries from my application because I don't use them anymore, but after I install the apk in my phone I can see that the native libraries are still there. As I've seen, android does copy/update native libraries correctly just by replacing the file in my libs/ directory, but apparently it doesn't delete them when they're not in the apk anymore. How do I know the libraries are still there? Well first of all the application's size remains the same and I'm deleting some really big libraries, and second, if I leave the System.load(...) statement the app is still able to load the library.

我为此找到的唯一解决方案是卸载应用程序并重新安装,但这对于用户从 google play 更新应用程序来说并不是一个明显的解决方案,是的,这个问题严重影响了我的应用程序功能.

The only solution that I've found for this has been to uninstall the app and make a fresh install, but that's not an obvious solution for a user updating the app from google play, and yes this problem affects severely my app functionality.

我已经检查过,显然我可以自己删除文件,比如在服务创建期间,但我不想以这种方式弄乱安装.

I've checked and apparently I can delete the files by my self, say during a service creation, but I don't want to mess with the installation this way.

你知道有没有办法告诉 Android 它需要删除所有原生库并重新复制它们?

Do you know if there's a way to tell Android that it needs to delete all the native libraries and copy them again?

谢谢,迈克

我刚刚发现 GB 正确更新了库,它会删除现在丢失的库,但 ICS 和 JB 都不会这样做,即使它们不再存在于 apk 中,这两个文件也会保留旧的 .so 文件.

I've just found that GB updates the libraries correctly, it deletes the now-missing as it should, but neither ICS nor JB do, those two leave the old .so files even when they're not in the apk anymore.

我尝试从/data/data/mypackage/lib 手动删除 .so 文件,但它不会让我这样做.

I tried deleting the .so files manually from /data/data/mypackage/lib but it won't let me.

有什么想法吗?

推荐答案

好的,所以我放弃了 android 来做这件事,我不得不自己做.我从这里得到了一些提示:http://www.moodstocks.com/2012/03/20/ice-cream-sandwich-why-native-code-support-sucks/.

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/.

这是我所做的:

  1. 使用文件夹 libs/armeabi、libs/armeabi-v7a 和 libs/x86 的内容制作三个 zip 文件,并将它们分别命名为 libs_armeabi.zip、libs_armeabi_v7a.zip 和 libs_x86.zip.

  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.

将 zip 文件移动到 res/raw 文件夹.

Move the zip files to the res/raw folder.

删除三个libs文件夹下的文件并用touch重新创建(在-nix系统中),记住ICS和JB不会删除lib文件夹下的旧文件,所以删除文件会导致应用保留那些非常大的文件.

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.

使用这两种方法以编程方式验证处理器架构:

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;
}

  • 根据处理器架构,以编程方式将三个文件之一解压缩到您想要的任何文件夹,我正在使用/data/data/myPackage/app_libs(使用 context.getDir("libs",Context.MODE_PRIVATE); 返回该路径).

    这里可能会涉及更多的逻辑,比如提取 libs_armeabi.zip 文件,然后从 libs_armeabi_v7a.zip 或 libs_x86.zip 中提取文件,或者不提取文件,覆盖具有相同名称的文件(我我只是在这里闲逛,因为我还没有真正尝试过),幸运的是我的三个文件中的库已经被重命名并且在每个 zip 文件中都是独占的,所以我只需要提取三个文件中的一个.

    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. 将 System.loadLibrary 方法调用替换为 System.load,后者支持使用其完整路径加载库,并为每个库添加完整路径.

    这种方法带来了额外的好处:

    This method comes with added benefits:

    • 压缩库后,apk 大小将减小,从而使 Google Play 商店中的应用程序更小.

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

    作为存储在原始资源中的库,android 不会自动复制它们,允许您将它们复制到任何您想要的位置并使用完整路径加载它们.

    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.

    如果需要,您可以决定将库复制到 SD 卡,我不建议这样做,但现在您可以这样做,例如当手机没有足够的空间时.

    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.

    希望这会有所帮助!

    这篇关于如何让android删除旧的本机库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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