在某些设备上找不到Android资源 [英] Android resources not found on some devices

查看:81
本文介绍了在某些设备上找不到Android资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个应用程序(带有适量的字符串),我们可以将其翻译为27种以上的语言.我们对该应用程序进行2个构建.这两个版本的区别仅在于包的名称.因此,基本上,我们首先使用软件包名称(例如 com.android.sad.app )构建应用程序,然后再构建一个软件包名称为 com.android.even.sadder.app 的应用程序.代码>.我们有机会在各种各样的Android设备上测试我们的应用程序,并且发现在某些设备上,例如 Samsung ACE Samsung Galaxy S LGOptimus 2x 我们的应用程序无法加载/读取资源,因此,即使该应用程序图标也没有显示,并且在启动该应用程序时,它会因 android.content.res而崩溃.Resources.NotFoundException .在其他设备上,一切正常.

我们发现,如果我们减少应用程序资源中的字符串总数,则我们的应用程序可以在上述设备上成功运行.但是,我们认为这不是解决问题的真正方法,因为可以在有问题的设备上运行带有完整字符串资源的调试版本.

所以我的问题是,有人知道什么可能导致这种非常奇怪的行为吗?

解决方案

在进行了反复试验后,我们发现问题出在 apk 包本身.在构建过程中,我们在构建完成之后但在对 apk 文件进行签名和对齐之前,将一些文件添加到应用程序 apk 中.最初,我们使用自己的工具(用 Java 编写,因此使用 Zip 的Java实现)提取并重新打包 apk ./p>

我们注意到,使用我们的工具重新包装 apk 后,我们能够将 apk 的尺寸减小为 apk Android构建创建的原始 apk .正如我们发现的那样,重新包装是造成问题的原因!

如我们的实验所示,如果重新包装的 apk 小于〜1.6 Mb ,则所有设备都可以读取和使用新包装的 apk .但是,如果 apk 的大小超过了〜1.6 Mb ,则本文中提到的设备(和仿真器)将无法正确读取或使用应用程序 apk .

我一直在寻找有关 apk文件格式(本质上是jar)的一些规范,但是我发现没有任何东西可以解释这种非常奇怪的行为.所以有人可以澄清一下为什么会发生这种奇怪的行为,以及确切的原因是什么?

注意:从现在开始,我们将使用Android aapt 工具将文件插入包中,而不是我们一直使用的工具和最终apk可以被所有设备读取

We have an application(with moderate amount of strings) which we translate to 27+ languages. We make 2 builds of the application. These 2 builds only differ in the name of the package. So basically we first do a build of our application with package name lets say com.android.sad.app and then another one with package name com.android.even.sadder.app. We had the chance to test our application on a great variety of Android devices and we have found out that on some devices like Samsung ACE, Samsung Galaxy S or LG Optimus 2x our application can't load/read the resources so even the application icon isn't shown and when the application is started it crashes with android.content.res.Resources.NotFoundException. On other devices everything is working just fine.

We have found out that if we reduce the overall amount of strings in the resources of the application, our application can successfully run on the above mentioned devices. However we do not think this is the real solution to our problem because the debug build with full strings in resources can be ran on the devices in question.

So my question would be does someone knows what can potentially cause this very strange behavior ?

解决方案

After some trial and error experimenting we have found out that the problem was with the apk package itself. In our build process we add some files to our application apk right after the build has finished but before signing and aligning the apk file. Originally we were extracting and repackaging the apk with our own tool (which is written in Java and thus using the Java implementation of Zip).

We have noticed that after repackaging the apk with our tool we were able to reduce the size of the apk to half size of the original apk created by the Android build. As we have found out this repackaging was the cause of our problem!

As our experiments have shown if the repacked apk was smaller then ~1.6 Mb, all devices were able to read and work with the newly repacked apk. However if the size of the apk have exceeded ~1.6 Mb the devices (and the emulator) mentioned in this post were not able to correctly read or work with the application apk .

I have been looking around for some specification on the apk file format(which is essentially jar), but I have found nothing that would explain this very odd behavior. So could somebody please clarify why is this strange behavior happening and what are the exact reasons?

Note: from now on we are using the Android aapt tool to insert our files to the package, instead of the tool we have been using and the final apk can be read by all devices

这篇关于在某些设备上找不到Android资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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