File Usage: /// android_res / drawable / url works under eclipse, but not in production

I have a problem that drives me crazy. I have a static html file (assets / help / index.html) that should contain some images. Since I want different images for different densities, and the images are already included in drawable- {ldpi, mdpi, hdpi}, I thought I would use the following html code:

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

This works great for an eclipse! Unfortunately, in the production version (build with the maven android plugin), a web view displaying an html page shows broken images.

I tried to open the page with loadUrl and loadDataWithBaseUrl (first I read the file myself), the last with the base file url: /// android_res / drawable. Both attempts succeed in eclipse, but do not work in the maven version.

So, I unpacked both the generated Eclipse apk and the generated maven, and did diff -r between them, because obviously there should be a difference.

I'm confusing to find only a few trivial differences (basically signing the differences, since eclipse apk signs with a debug certificate and maven with my official certificate). In addition, the contents of apks are identical!

Does anyone have an idea of ​​what is happening or how to act in the disclosure of additional information?

+6
source share
3 answers

I had a similar problem using proguard and I found an explanation (and solution!) Here .

In my case, proguard obfuscated R class and android_res / [...] can no longer be found.

I decided to add the rule to proguard.cfg:

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

I do not know about maven, but hope this helps.

+4
source

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

If you want to access the resources that are defined in the library project from WebView and rename the package name using the corresponding Android-maven-plugin option (which, in turn, depends on some features of the Android SDK), then you got into a rather strange a mistake.

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

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

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

A possible workaround is this: When creating a release, dynamically create a copy of your real R class and put it in the Java package that matches the name of the renamed application package. Be sure to update the `` package '' statement in the copied source.

+1
source

I would consider AsssetManager as a way to access files associated with the application. A tutorial (with sample code) can be found here . Alternatively, you can put the file in res/raw and read them with openRawResource ()

0
source

Source: https://habr.com/ru/post/888527/


All Articles