6

First off, i've already to referred to a similar post, Android, javamail and proguard

The solution mentioned was to explicitly keep the following in proguard-project.txt:

-dontwarn java.awt.**
-dontwarn java.beans.Beans
-dontwarn javax.security.**

-keep class javamail.** {*;}
-keep class javax.mail.** {*;}
-keep class javax.activation.** {*;}

-keep class com.sun.mail.dsn.** {*;}
-keep class com.sun.mail.handlers.** {*;}
-keep class com.sun.mail.smtp.** {*;}
-keep class com.sun.mail.util.** {*;}
-keep class mailcap.** {*;}
-keep class mimetypes.** {*;}
-keep class myjava.awt.datatransfer.** {*;}
-keep class org.apache.harmony.awt.** {*;}
-keep class org.apache.harmony.misc.** {*;}

At first sight, this seemed to work, as it compiled without any warnings. However, it fails at reading the message content and just skips right over it. I've tried the following:

  1. -includelibraryjars explicitly naming the 3 jar files required for javamail.
  2. -removed the jars as an external library, following the new libs/ include format.
  3. -maintained the default android settings in proguard-android.txt
  4. -followed the troubleshooting guide in the proguard faq.
  5. -started a new project and copied over the source files to it.
  6. -tried various proguard options, including -dontshrink, keepnames, etc
  7. -obsessive project/clean

After a few hours of frustration, here's what i found that seemed to work:

-dontobfuscate
-dontshrink
-keepdirectories
-keeppackagenames javax.mail.**
-keeppackagenames javax.activation.**
-keeppackagenames com.sun.mail.**
-keeppackagenames myjava.**
-keeppackagenames org.apache.harmony.**
-keeppackagenames mailcap.**
-keeppackagenames mimetypes.**
-keep class javamail.** {*;}
-keep class javax.mail.** {*;}
-keep class javax.activation.** {*;}

-keep class com.sun.mail.dsn.** {*;}
-keep class com.sun.mail.handlers.** {*;}
-keep class com.sun.mail.smtp.** {*;}
-keep class com.sun.mail.util.** {*;}
-keep class mailcap.** {*;}
-keep class mimetypes.** {*;}
-keep class myjava.awt.datatransfer.** {*;}
-keep class org.apache.harmony.awt.** {*;}
-keep class org.apache.harmony.misc.** {*;}

-dontwarn java.awt.**
-dontwarn java.beans.Beans
-dontwarn javax.security.**

Of course that's absurd because i'm turning on -dontobfuscate and -dontshrink. Any proguard and javamail gurus have a solution to this? I'm ADT17, using 2.1(api7) for the build. If i could exclude the jars entirely from the process maybe? Any advice will be a godsend at this point.

Community
  • 1
  • 1
ThumbsDP
  • 543
  • 6
  • 18

3 Answers3

18

Problem solved. I've posted the solution here for anyone having issues with the other solution mentioned in the link above.

Because i was using a helper class with javamail (Mail.java), i needed to include that class as a -keep so that it would work. I edited the solution provided at Android, javamail and proguard to include the helper class, since many use this and the other solution may fail horribly without it.

Put this in your proguard-project.txt file. I used the default android settings otherwise.

-dontshrink
-keep class javax.** {*;}
-keep class com.sun.** {*;}
-keep class myjava.** {*;}
-keep class org.apache.harmony.** {*;}
-keep public class Mail {*;}
-dontwarn java.awt.**
-dontwarn java.beans.Beans
-dontwarn javax.security.**
Community
  • 1
  • 1
ThumbsDP
  • 543
  • 6
  • 18
  • 1
    `-keep class com.sun.mail.handlers.** {*;}` did the trick for me, even with shrink on. – Nick Russler Dec 23 '14 at 14:18
  • Nice solution. Thank you. – Brave Aug 24 '15 at 04:35
  • This solution allows compilation and preserve mail functionality even when removing -dontshrink statement :-) – Pascal Feb 29 '16 at 13:47
  • I would strongly council against using Mail.java, at least without a lot of modification. That code contains a number of unnecessary steps, and more importantly it does not allow you to recover the status message returned by the SMTP server. If for any reason the message fails to arrive, your user will be none the wiser. Your solution is shorter than mine, but only because it keeps more binaries, which may result in a larger apk file. – Philip Sheard Feb 29 '16 at 17:23
  • @PhilipSheard Do you have any recommendations? – CyberMew May 19 '17 at 06:09
  • For my case, only following classes did the trick -keep class javax.** {*;} -keep class com.sun.** {*;} -keep class org.apache.harmony.** {*;} – orchidrudra Aug 07 '17 at 08:31
  • Works perfectly. Thank you! – Razvan Sep 22 '17 at 10:33
  • Worked without "-dontshrink". I removed it but works. – Sattar Hummatli May 08 '19 at 14:00
1

In my case the javax.activation was causing the following problem:

Warning: com.sun.mail.handlers.handler_base: can't find referenced method 'boolean equals(java.awt.datatransfer.DataFlavor)' in program class javax.activation.ActivationDataFlavor

So i had to add -dontwarn javax.activation.** in ThumbsDP solution. So all together are the below additions on proguard-rules that did the trick to me:

-dontshrink
-keep class javax.** {*;}
-keep class com.sun.** {*;}
-keep class myjava.** {*;}
-keep class org.apache.harmony.** {*;}
-keep public class Mail {*;}
-dontwarn java.awt.**
-dontwarn java.beans.Beans
-dontwarn javax.security.**
-dontwarn javax.activation.**
angelos_lex
  • 1,593
  • 16
  • 24
0

The existing solution were a bit far fetched for my taste. :)

I think this is a better solution, put inside proguard-rules.pro file:

-libraryjars libs/mail.jar
-libraryjars libs/activation.jar
-libraryjars libs/additionnal.jar

As a bonus

After solving proguard issue, I also did get the following exception: "com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found"

To solve it put also the following line:

-keep class com.google.** {*;}
Izoman
  • 51
  • 5