Search code examples
androidproguard

How to tell ProGuard to keep everything in a particular package?


My application has many activities and uses native library too. With the default ProGuard configuration which Eclipse generates ProGuard removes many things - OnClick methods, static members, callback methods which my native library uses... Is it there a simple way to instruct ProGuard to NOT remove anything from my package? Removing things saves only about 2.5% of the application size, but breaks my application completely. Configuring, testing and maintaining it class by class in ProGuard configuration would be a pain.


Solution

  • As final result I found that just keeping all class members is not enough for the correct work of my application, nor necessary. I addded to the settings file this:

    -keepclasseswithmembers class * {
        void onClick*(...);
    }
    -keepclasseswithmembers class * {
        *** *Callback(...);
    }
    

    The case with onClick* is for all methods which I address in android:onClick atribute in .xml layout files (I begin the names of all such methods with 'onClick').

    The case with *Callback is for all callback methods which I call from my native code (through JNI). I place a suffix 'Callback' to the name of every such method.

    Also I added few rows for some special cases with variables which I use from native code, like:

    -keep class com.myapp.mypackage.SomeMyClass {
        *** position;
    }
    

    (for a varible with name 'position' in a class with name 'SomeMyClass' from package com.myapp.mypackage)

    All this is because these onClick, callback etc. must not only be present but also kept with their original names. The other things ProGuard can optimize freely.

    The case with the native methods is important also, but for it there was a declaration in the generated from Eclipse file:

    -keepclasseswithmembernames class * {
        native <methods>;
    }