Proguard and Netty 5 on Android

I've seen a couple of questions on this issue, but they are for older versions of Netty.
I tried my answers by switching to org.jboss.netty from io.netty, but the same error occurs.

I am trying to compile an Android app that uses Netty 5.0.0Alpha2 (build 16) with Proguard enabled.

The application works fine without Proguard.
As soon as I enable Proguard, I get this exception when it tries to use Netty:

java.lang.IllegalStateException: unknown type parameter 'I': class io.netty.channel.SimpleChannelInboundHandler
    at io.netty.util.internal.TypeParameterMatcher.find0(Unknown Source)
    at io.netty.util.internal.TypeParameterMatcher.find(Unknown Source)
    at io.netty.channel.SimpleChannelInboundHandler.<init>(Unknown Source)
    at io.netty.channel.SimpleChannelInboundHandler.<init>(Unknown Source)
    ...

      

This is my Proguard config:

# billing
-keep class com.android.vending.billing.**

# butterknife
-dontwarn butterknife.internal.**
-keep class **$$ViewInjector {
    *;
}
-keepnames class * {
    @butterknife.InjectView *;
}

# admob
-keep public class com.google.android.gms.ads.** {
    public *;
}

-keep public class com.google.ads.** {
    public *;
}

# logging
-assumenosideeffects class android.util.Log

# netty (partial)
-dontwarn io.netty.**
-dontwarn sun.**

      

I tested it with no -dontwarn options to see if the warnings point me in the right direction, but it doesn't contain additional dependencies like slf4j and Tomcat.

I have also tried to exclude all Netty classes, for example:

-keep class io.netty.** {
    *;
}

      

... but that doesn't fix it either.

+3


source to share


1 answer


I fixed this issue with some carefully applied Proguard rules after reading parts of the fairly large Netty sources:

-keepattributes Signature,InnerClasses
-keepclasseswithmembers class io.netty.** {
    *;
}
-keepnames class io.netty.** {
    *;
}

      

My original exception was caused by the type variables being removed from the bytecode that Netty uses through reflection. Signature

c -keepattributes

stores this information.

You get a slightly different exception, unless Signature

the -keepattributes

- adding InnerClasses

fixes this by returning even more information to the class files.



Later I received java.lang.NoSuchFieldException: ctl

; what is -keepnames for. So the field is still named ctl

as Netty expects.

Finally, some contributors (like those ctl

seen earlier) have been removed by Proguard because Netty only uses them through reflection. The final rule is to -keepclasseswithmembers

ensure that Proguard does not remove them.

If you take this approach, I highly recommend that you only use unnecessary jars and not the -all jar. Switching from -all to the Netty banks I wanted brought my counting method down after I passed the 65k limit. Shrinking your jars takes a bit of trial and error, though, since the separation is unclear and there aren't really any resources talking about what.

* not very thorough, I just hit the rules in the file and deleted them if they didn't do anything. Probably the best way to do this, which doesn't store this information throughout the entire program, but just Netty instead.

+8


source







All Articles