Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

android - Prevent Proguard to remove specific drawables

In my Android project, I have some images stored in res/drawable/ which are accessed only from an HTML file loaded in a Webview. For example (code in HTML):

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

These images are removed by Proguard from apk during optimization.

Does somebody know a way to keep these files (even if they are not used directly in the java code)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had a similar issue and wanted to add just a few bits.

The resources ARE NOT stripped away by ProGuard. If you simply unzip your apk you will see that the image files are still there.

The problem is that the Webkit FileLoader will try and load your R$drawable class using reflection. If you do not add any keep rule to your proguard.cfg file that class will be renamed, hence Webkit will not be able to load your resource.

By placing the file into the assets folder your are bypassing the R class system and everything will work.

This is not a solution though if, for example, you want to use different resources for different densities.

I suggest you simply add a very basic keep rule to preserve R inner classes and fields:

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

-keep class **.R$*

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...