4

I am using the smack library (4.1.2) in my Android app to connect to an XMPP server. It runs perfectly well when the code is NOT minified (ie with proguard). But in release mode, with proguard enabled, the app crashes as soon as it connects to the server.

I am trying to keep all relevant smack classes 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 this doesn't work. Below is the crash stack trace.

 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 would fix this?

Update: I already referred to the other issue in my report and explained that proposed solution is not a fix in this case.

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

Community
  • 1
  • 1
Steve
  • 395
  • 2
  • 12
  • Make sure you have written `-keep class org.jivesoftware.smack.** { *; } -keep class org.jivesoftware.smackx.** { *; }` in the same proguard file which is set in build.gradle.. or in the proguard-rules.pro – Nilay Dani Jul 01 '15 at 09:39
  • Yes, I wondered if those proguard rules were not taking effect at first too. But as you can see in the stack trace, org.jivesoftware.smack.* classes still have their original names. – Steve Jul 01 '15 at 09:46
  • I am afraid something is wrong in your code because it is ClassCastException on ParameterizedType So check where you have used that casting – Nilay Dani Jul 01 '15 at 09:56
  • That cast is in the smack library (FlexibleStanzaTypeFilter). But remember, the error is not present with proguard off. Looking at the source of FlexibleStanzaTypeFilter https://www.igniterealtime.org/builds/smack/docs/latest/javadoc/src-html/org/jivesoftware/smack/filter/FlexibleStanzaTypeFilter.html, it does appear to do reflection, so it's not surprising that proguard issues might appear. So I'm guessing the suggested proguard configuration may not be enough! – Steve Jul 01 '15 at 10:12
  • possible duplicate of [What are the recommended ProGuard rules for Smack 4.1?](http://stackoverflow.com/questions/29678908/what-are-the-recommended-proguard-rules-for-smack-4-1) – Okas Jul 01 '15 at 10:43
  • Not a duplicate - I already mentioned the linked-to issue. – Steve Jul 01 '15 at 13:20

2 Answers2

8

So, I found a solution. Can't believe I spend a whole day on this problem! Hope this saves someone else the same trouble:

The line causing the problem (in smack library) is

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

Note the cast. The problem appears to be the proguard by default looses some information on types, even if you have specified "-keep" for your classes. The crucial bit of helpful information is here: Field.getGenericType() returns instance of java.lang.Class instead of Type

The answer to my question is therefore that the following proguard config is required:

-keepattributes Signature
-keep class org.jivesoftware.smack.** { *; }
-keep class org.jivesoftware.smackx.** { *; }
Community
  • 1
  • 1
Steve
  • 395
  • 2
  • 12
0

xpp3 need to proguard too if you notadd this

configurations {
    all*.exclude group: 'xpp3', module: 'xpp3'
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103