Monday, 2 April 2012

Beware of Proguard

ProGuard is really useful when you want to protect your code and optimize it.
But it can also cause some trouble if it does more than what it supposed to do.

I had created this fragment class called NurseryFragment. This fragment was part of an activity and was included via an XML layout file like this one:


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

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

    <!-- "Fragment A" -->

    <fragment class="com.forde.kutiz.NurseryFragment"

              android:id="@+id/frag_nurseryfragment"

              android:layout_width="match_parent"

              android:layout_height="match_parent"/>

</FrameLayout>


In debug mode, everything was working fine.
But with my release package, I kept getting this exception when I was trying to launch the activity.


04-02 12:41:11.271: D/dalvikvm(32607): GC_CONCURRENT freed 578K, 54% free 3152K/6727K, external 1625K/2137K, paused 1ms+2ms
04-02 12:42:00.040: D/dalvikvm(1171): GC_FOR_MALLOC freed 756K, 50% free 3424K/6791K, external 1893K/2137K, paused 34ms
04-02 12:42:00.083: D/dalvikvm(1171): GC_FOR_MALLOC freed 710K, 50% free 3404K/6791K, external 1677K/2137K, paused 30ms
04-02 12:42:00.251: D/dalvikvm(1171): GC_CONCURRENT freed 711K, 50% free 3457K/6791K, external 1916K/2137K, paused 1ms+4ms
04-02 12:42:06.642: D/dalvikvm(1171): GC_EXPLICIT freed 602K, 51% free 3395K/6791K, external 1888K/2137K, paused 91ms
04-02 12:42:09.392: D/dalvikvm(1253): GC_CONCURRENT freed 1286K, 59% free 2788K/6663K, external 2880K/3596K, paused 11ms+3ms
04-02 12:42:17.540: D/dalvikvm(32607): GC_CONCURRENT freed 578K, 54% free 3152K/6727K, external 1625K/2137K, paused 4ms+2ms
04-02 12:43:00.079: D/dalvikvm(1171): GC_FOR_MALLOC freed 701K, 50% free 3404K/6791K, external 1676K/2137K, paused 20ms
04-02 12:43:00.208: D/dalvikvm(32145): GC_EXTERNAL_ALLOC freed 332K, 39% free 7759K/12615K, external 4026K/4042K, paused 92ms
04-02 12:43:00.302: D/dalvikvm(1171): GC_CONCURRENT freed 708K, 50% free 3457K/6791K, external 1914K/2137K, paused 2ms+4ms
04-02 12:43:25.021: D/dalvikvm(32607): GC_CONCURRENT freed 578K, 54% free 3152K/6727K, external 1625K/2137K, paused 2ms+3ms

This was caused by proguard which probably removes this class since it is not referenced by any file except the XML layout file.
To solve this issue, all you have to do is add a rule in the proguard.cfg of your project to prevent Proguard to remove your class:


-keep public class com.forde.kutiz.NurseryFragment




No comments:

Post a Comment