Search code examples
androidproguard

Proguard corrupts drawable files


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,


Solution

  • 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" />