使用 file:///android_res/drawable/ url 在 eclipse 下有效,但在生产中无效

发布于 2024-11-08 18:02:18 字数 655 浏览 0 评论 0原文

我有一个问题让我发疯。我有一个静态 html 文件 (assets/help/index.html),需要包含一些图像。因为我想要不同密度的不同图像,并且图像已经包含在drawable-{ldpi,mdpi,hdpi}中,所以我想我应该使用以下html代码:

<img src="file:///android_res/drawable/image.png">

这在eclipse下效果很好!不幸的是,在生产版本(使用 maven android 插件构建)中,显示 html 页面的 webview 显示损坏的图像图标。

我尝试使用 loadUrl 和 loadDataWithBaseUrl 打开页面(首先自己读取文件),后者的基本 url 为 file:///android_res/drawable。两次尝试在 eclipse 下都成功,但在 maven 版本中都失败了。

因此,我解压了 Eclipse 生成的 apk 和 maven 生成的 apk,并在两者之间进行了 diff -r,因为显然肯定存在差异。

我很困惑地发现只有一些细微的差异(主要是签名差异,因为 eclipse apk 是使用调试证书签名的,maven apk 是使用我的官方证书签名的)。除此之外,APK 的内容是相同的!

有谁知道发生了什么事或如何继续发现更多信息?

I have a problem that is driving me nuts. I have a static html file (assets/help/index.html) that needs to include some images. As I want different images for different densities and the images are already included in drawable-{ldpi,mdpi,hdpi} I thought I'd use the following html code:

<img src="file:///android_res/drawable/image.png">

This works excellent under eclipse! Unfortunately in the production version (build with the maven android plugin) the webview that displays the html page shows broken image icons.

I have tried opening the page using loadUrl and loadDataWithBaseUrl (first reading the file myself), the latter with a base url of file:///android_res/drawable. Both attempts succeed under eclipse but fail in the maven version.

So I unpacked both the Eclipse generated apk and the maven generated one and did a diff -r between the two because there must clearly be a difference.

I'm baffled to find only a few trivial differences (mostly signing differences as the eclipse apk is signed with the debug certificate and the maven one with my official certificate). Apart from that, the content of the apks are identical!

Does anyone have any idea what is going on or how to proceed in uncovering more information?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

つ可否回来 2024-11-15 18:02:18

我使用 proguard 遇到了类似的问题,我找到了一个解释(以及一个解决方案!) 此处

就我而言,混淆的 R 类和 android_res/[...] 无法再找到。

我解决了向 proguard.cfg 添加一条规则:

-keepclassmembers class **.R$* {
    public static <fields>;
}

-keep class **.R$*

我不了解 Maven,但希望这会有所帮助。

I had a similar issue using proguard and I found an explanation (and the a solution!) here.

In my case proguard obfuscated R class and android_res/[...] couldn't be found anymore.

I solved adding a rule to proguard.cfg:

-keepclassmembers class **.R$* {
    public static <fields>;
}

-keep class **.R$*

I don't know about maven but hope this helps.

墨小墨 2024-11-15 18:02:18

使用 file:///android_res 而不是 file:///android_asset 是有原因的。其中之一是 Android 库项目不能拥有资产。

如果您碰巧想要从 WebView 访问库项目中定义的资源,并使用 android-maven-plugin 的相应选项重命名包名称(该选项又依赖于某些 Android SDK)功能)然后你遇到了一个相当奇怪的错误。

Android 在 android.webkit.BrowserFrame::inputStreamForAndroidResource 中执行此操作(至少在 4.1.2 中):

final Class<?> d = mContext.getApplicationContext().getClassLoader().loadClass(
mContext.getPackageName() + ".R$" + subClassName);

这是 R 类的动态查找,它使用应用程序的包名称作为 Java 包名称。然而,这不起作用,因为重命名的包名称中的 R 类将不存在。

可能的解决方法如下:
在发布版本期间,动态创建真实 R 类的副本,并将其放入适合重命名的应用程序包名称的 Java 包中。确保也更新复制源中的“package”语句。

There are reasons for using file:///android_res instead of file:///android_asset. One of them is that Android library projects cannot have assets.

If you happen to want to access resources which are defined in a library project from a WebView and rename the package name using the respective option of the android-maven-plugin (which in turn relies on some Android SDK feature) then you hit a rather weird bug.

Android does this in the android.webkit.BrowserFrame::inputStreamForAndroidResource (at least in 4.1.2):

final Class<?> d = mContext.getApplicationContext().getClassLoader().loadClass(
mContext.getPackageName() + ".R$" + subClassName);

This is a dynamic lookup of the R class and it uses the application's package name as a Java package name. However this does not work because the R class in the renamed package name will not exist.

A possible workaround is the following:
During your release build, dynamically make a copy of your real R class and place it into the Java package that fits the renamed application package name. Make sure to update the ''package'' statement in the copied source as well.

素食主义者 2024-11-15 18:02:18

我会看看 AsssetManager 作为访问方式与应用程序捆绑在一起的文件。可以在此处找到教程(带有代码示例)。或者,您可以将文件放入 res/raw 并使用 openRawResource()

I would take a look into AsssetManagers as a way to access files bundled with the application. A tutorial (with code example) can be found here. Alternatively you could put the file in res/raw and read them with openRawResource()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文