Proguard corrupts drawable files

假装没事ソ 提交于 2020-01-05 04:05:25

问题


I have strange issue with proguard, somehow it breaks my valid drawable file. Without proguard drawable shows ok. Is proguard supposed to minify xml drawables?

drawable/wide_btn_round_white.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >
            <corners android:radius="25dip" />
            <padding android:bottom="10dp"/>
            <padding android:top="10dp"/>
            <solid android:color="@color/colorOrangeGetStarted" />
        </shape>
    </item>
</selector>

becomes like this

<?xml version="1.0" encoding="utf-8"?>
<x />

build.gradle:

release {
        minifyEnabled true
        shrinkResources true
        signingConfig signingConfigs.release
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

proguard-rules.pro:

-keepattributes *Annotation*
-keepattributes Signature
-keepclassmembers class ** {
    @com.squareup.otto.Subscribe public *;
    @com.squareup.otto.Produce public *;
}
-dontwarn okio.**
-dontwarn retrofit2.Platform$Java8
-assumenosideeffects class android.util.Log {
    public static boolean isLoggable(java.lang.String, int);
    public static int v(...);
    public static int i(...);
    public static int w(...);
    public static int d(...);
    public static int e(...);
}
-keep class com.stripe.** { *; }
-keepclassmembers class com.myapp.services.http.** {
    <fields>;
}
-keepclassmembers class com.myapp.services.ws.** {
    <fields>;
}
-keepclassmembers class * extends java.lang.Enum {
    <fields>;
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

EDIT: This issue happens because of shrinkResources. It obfuscates all drawables that are specified ONLY in style.xml. So for example if I have a button that has style="@style/MyStyle" and in styles.xml:

<style name="MyStyle">
    <item name="android:background">@drawable/wide_btn_round_white</it‌​em>
</style>.

wide_btn_round_white.xml is gonna be broken. But if I specify background directly on button everything would work OK. According wiki i can create app/src/main/res/values/keep.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:keep="@drawable/wide_btn_round_white" /> 

But this thing doesn't work. As I understand it only specifies that this file shouldn't be removed.

Any help is greatly appreciated!

Best regard,


回答1:


In general Proguard does not work on xml files, only Java. So I think your build is some how corrupting your file. Perhaps opening a new project and copy there your source and the .git file(if you got one) will help

The other thing could be Android shrink resources, so try setting shrinkResources to false and see if it helps. If it does, use tools:keep to keep your problematic resources away from shrinker.

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*"
    tools:discard="@layout/unused2" />


来源:https://stackoverflow.com/questions/41880195/proguard-corrupts-drawable-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!