Smack for android fails when using proguard

I am using the smack library (4.1.2) in my Android application to connect to the XMPP server. It works great when the code is NOT counted (i.e. C proguard). But in release mode, with proguard turned on, the application crashes as soon as it connects to the server.

I try to ensure that all relevant smack classes are unminified, as suggested here: What are the recommended ProGuard rules for Smack 4.1?

-keep class org.jivesoftware.smack.** { *; } -keep class org.jivesoftware.smackx.** { *; } 

But for me it does not work. The trace of the accident stack is shown below.

  java.lang.ExceptionInInitializerError at org.jivesoftware.smackx.privacy.PrivacyListManager.<init>(PrivacyListManager.java:126) at org.jivesoftware.smackx.privacy.PrivacyListManager.getInstanceFor(PrivacyListManager.java:210) at org.jivesoftware.smackx.privacy.PrivacyListManager$1.connectionCreated(PrivacyListManager.java:81) at org.jivesoftware.smack.tcp.XMPPTCPConnection.initConnection(XMPPTCPConnection.java:636) at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:834) at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:365) ... Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType at org.jivesoftware.smack.filter.FlexibleStanzaTypeFilter.<init>(FlexibleStanzaTypeFilter.java:40) at org.jivesoftware.smackx.privacy.filter.SetActiveListFilter.<init>(SetActiveListFilter.java:27) at org.jivesoftware.smackx.privacy.filter.SetActiveListFilter.<clinit>(SetActiveListFilter.java:25) 

Any ideas on what proguard config fix this?

Update: I already mentioned another problem in my report and explained that the proposed solution is not a solution in this case.

Update 2: I restored the stack trace with line numbers enabled.

+3
android proguard xmpp android-proguard smack
source share
2 answers

So, I have found a solution. I can’t believe that I’ve been working on this problem all day! Hope this saves someone else the same problem:

The line causing the problem (in the smack library) is

 stanzaType = (Class<S>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; 

Pay attention to the cast. The problem is that proguard loses some type information by default, even if you specify -keep for your classes. The most important bit of useful information is here: Field.getGenericType () returns an instance of java.lang.Class instead of type

So the answer to my question is that the following proguard configuration is required:

 -keepattributes Signature -keep class org.jivesoftware.smack.** { *; } -keep class org.jivesoftware.smackx.** { *; } 
+5
source share

xpp3 also needs to be protected if you do not add this

 configurations { all*.exclude group: 'xpp3', module: 'xpp3' } 
0
source share

All Articles