java.lang.NoClassDefFound error javax Gmail API

1.6k views Asked by At

I'm running into an exception in a gmail client I'm working on. The exception is the

java.lang.NoClassDefFound that is triggered on the getDefaultInstance line

   Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

I've included the javax jar as follows:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.google.android.gms:play-services:7.3.0'
    compile 'com.google.api-client:google-api-client:1.20.0'
    compile 'com.google.api-client:google-api-client-android:1.20.0'
    compile 'com.google.api-client:google-api-client-gson:1.20.0'
    compile 'com.google.apis:google-api-services-gmail:v1-rev29-1.20.0'
    compile  'javax.mail:javax.mail-api:1.5.2'
}

I've googled this but not quite sure how to fix this. I've tried clean and rebuild and in both cases the exception is still thrown. Any help would be appreciated.

Exeception detail Message:

Didn't find class "com.sun.mail.util.MailLogger" on path: DexPathList[[zip file "/data/app/com.android.application.androidgmailclient-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

UPDATE

I found that the missing class "MailLogger" is not a part of the javax.mail-api rather it is a part of the 'javax.mail:mail:1.5.0-b01' jar with 1.5.0-b01 being the latest version:

http://mvnrepository.com/artifact/javax.mail/mail/1.5.0-b01

So I included the compile 'javax.mail:mail:1.5.0-b01' into my gradle file and then I found the jar in the external libraries. I copied the jar into app/libs and then hit "Add as Library". I can see that the MailLogger class is now present which is what was bombing in the exception. My gradle file now looks like this:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.0 rc2"

    packagingOptions{
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

    defaultConfig {
        applicationId "com.android.app.androidgmailclient"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.google.android.gms:play-services:7.3.0'
    compile 'com.google.api-client:google-api-client:1.20.0'
    compile 'com.google.api-client:google-api-client-android:1.20.0'
    compile 'com.google.api-client:google-api-client-gson:1.20.0'
    compile 'com.google.apis:google-api-services-gmail:v1-rev29-1.20.0'
    compile 'javax.mail:mail:1.5.0-b01'
    compile files('libs/mail-1.5.0-b01.jar')
}

However, when I go to build the project now I get this build error:

Error:Execution failed for task ':app:dexDebug'.

com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2

Any ideas?

3

There are 3 answers

0
lqu On

I assume you have compiled your code successfully, but got a runtime exception. You need to include the jar files in the final package. Have a look at this one, and this one.

0
Nitin Varun On

This error occurred because you are adding same library twice.

compile 'javax.mail:mail:1.5.0-b01'
compile files('libs/mail-1.5.0-b01.jar')

delete one of these line then I think it should work fine.

0
Leonardo Casale On

Not sure if you solved this already, but I found you need to use an Android specific Java Mail library.

compile 'com.sun.mail:android-mail:1.5.5'
compile 'com.sun.mail:android-activation:1.5.5'

I was running into your error and other similar ones using the latest Java Mail libraries but switching to these fixed all of them.

https://java.net/projects/javamail/pages/Android

Android Studio: NoClassDefFoundError