How to resolve java.lang.VerifyError when using javamail on android

965 views Asked by At

The code giving the error (simplified):

        store = session.getStore("imaps");
        store.connect();
        inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_ONLY);
        Message message = inbox.getMessage(1);
   -->  mailContent = message.getContent().toString();  

Error message

java.lang.VerifyError: Rejecting class com.sun.mail.handlers.text_plain that attempts to sub-type erroneous class com.sun.mail.handlers.handler_base (declaration of 'com.sun.mail.handlers.text_plain' appears in /data/app/~~Z3AcOYbtbz2T2e7VoPzr3w==/com.example.epistula-WhLKom1ZTZ24Nr1ACPcg8Q==/base.apk)
    at java.lang.Class.newInstance(Native Method)

...

    Caused by: java.lang.VerifyError: Verifier rejected class com.sun.mail.handlers.handler_base: java.awt.datatransfer.DataFlavor[] com.sun.mail.handlers.handler_base.getTransferDataFlavors() failed to verify: java.awt.datatransfer.DataFlavor[] com.sun.mail.handlers.handler_base.getTransferDataFlavors(): [0x4]  can't resolve returned type 'Unresolved Reference: java.awt.datatransfer.DataFlavor[]' or 'Reference: javax.activation.ActivationDataFlavor[]' (declaration of 'com.sun.mail.handlers.handler_base' appears in /data/app/~~Z3AcOYbtbz2T2e7VoPzr3w==/com.example.epistula-WhLKom1ZTZ24Nr1ACPcg8Q==/base.apk)
    at java.lang.Class.newInstance(Native Method) 

...

My dependencies

        implementation 'com.sun.mail:android-mail:1.6.4'
        implementation 'com.sun.mail:android-activation:1.6.4'

Evrything worked fine until I tried getting the actual mail content. Reading folders and Subject etc ok. I am new to Android Studio (and Java), but I can guess this is some kind of versoning conflict.

But what version of what should be changed in what direction and how I am not able to figure out.

Current theory is that the problem is caused by the interface DataContentHandler. The "erroneous" class handler_base implements this interface. Android studio comes with a predefined version of the interface, in package javax.activation. android-activation reimplements (and changes) this interface (and probably the whole package). This is of some reason no problem to the compiler, but when the sub-typing happens in run-time, there is a conflict.

But how do I remove, or suspend, the old version of the interface? I have tried to delete it in the jar-file, but that has no consequence. I have not access to the source code, so I cannot change the name. And I don't even know if the theory holds.

Update

Now I sort of "fixed" the error by swapping to another device. I will not add this as an answer since I have no idea what devices will allow the code to run and why.

0

There are 0 answers