Search code examples
javaandroidandroid-studiopicasa

Error while building android application : java.util.zip.ZipException: duplicate entry: com/sun/mail/handlers/message_rfc822.class


I am developing android application that interacts with Picasa Web albums. SoI was trying to upload a photo using the following code.

   protected Integer doInBackground(String... params) {

        int index = 0;
        int count = 0;
        URL albumPostUrl = null;
        try {
            albumPostUrl = new URL("https://picasaweb.google.com/data/feed/api/user/"+googuserName+"/albumid/" + params[1]);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        while(index < totalFBPicsCount)
        {
            PhotoEntry myPhoto = new PhotoEntry();
            myPhoto.setTitle(new PlainTextConstruct(params[2] + "_" + index));
            myPhoto.setClient("Photo Sync");
            MediaFileSource myMedia = new MediaFileSource(new File(params[0] + "/" + params[2] + "_" +index + ".jpg"), "image/jpeg");
            myPhoto.setMediaSource(myMedia);
            try
            {
            PhotoEntry returPic = myService.insert(albumPostUrl, myPhoto);
            }
            catch (RuntimeException ex)
            {
                ex.printStackTrace();
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
                Log.e("Sai", "PhotSync", ex);
            }
            index++;
        }


        return null;

    }

I got the following exception when executing the line myService.insert(albumPostUrl, myPhoto). Following is logcat trace:

 06-05 01:18:04.086    4272-4422/krishtech.photosync E/dalvikvm﹕ 

    Could not find class 'com.google.gdata.data.media.MediaBodyPart$MediaSourceDataHandler', 
referenced from method com.google.gdata.data.media.MediaBodyPart.initMediaDataHandler 
06-05 01:18:04.091    4272-4422/krishtech.photosync E/dalvikvm﹕ Could not find class 'javax.activation.DataHandler', referenced from method com.google.gdata.data.media.MediaBodyPart.initMediaDataHandler 
06-05 01:18:04.101    4272-4422/krishtech.photosync E/dalvikvm﹕ Could not find class 'javax.mail.internet.CachedDataHandler', referenced from method javax.mail.internet.MimeBodyPart.createCachedDataHandler 
06-05 01:18:04.116    4272-4422/krishtech.photosync E/dalvikvm﹕ Could not find class 'javax.activation.DataHandler', referenced from method javax.mail.internet.MimeBodyPart.attachFile 
06-05 01:18:04.131    4272-4422/krishtech.photosync E/dalvikvm﹕ Could not find class 'javax.activation.DataHandler', referenced from method javax.mail.internet.MimeBodyPart.getDataHandler 
06-05 01:18:04.146    4272-4422/krishtech.photosync E/dalvikvm﹕ Could not find class 'javax.activation.DataHandler', referenced from method javax.mail.internet.MimeBodyPart.setContent 
06-05 01:18:04.146    4272-4422/krishtech.photosync E/dalvikvm﹕ Could not find class 'javax.activation.DataHandler', referenced from method javax.mail.internet.MimeBodyPart.setContent 06-05 01:18:08.866    4272-4422/krishtech.photosync E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #3
            Process: krishtech.photosync, PID: 4272
            java.lang.RuntimeException: An error occured while executing doInBackground()
                    at android.os.AsyncTask$3.done(AsyncTask.java:300)
                    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                    at java.lang.Thread.run(Thread.java:841)
             Caused by: java.lang.NoClassDefFoundError: javax.activation.DataHandler
                    at javax.mail.internet.MimeBodyPart.setContent(MimeBodyPart.java:678)
                    at com.google.gdata.data.media.MediaBodyPart.<init>(MediaBodyPart.java:95)
                    at com.google.gdata.data.media.MediaMultipart.<init>(MediaMultipart.java:126)
                    at com.google.gdata.client.media.MediaService.insert(MediaService.java:390)
                    at krishtech.photosync.MainActivity$startBackuptoGoogle.doInBackground(MainActivity.java:702)
                    at krishtech.photosync.MainActivity$startBackuptoGoogle.doInBackground(MainActivity.java:671)
                    at android.os.AsyncTask$2.call(AsyncTask.java:288)
                    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                    at  java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                    at     java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                    at java.lang.Thread.run(Thread.java:841)

After bit of searching found out that i have to use a android port of Java Mail from here. But when i added the mail.jar, activation.jar and additional.jar and tried to build the application i am getting the following error on Gradle console.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:packageAllDebugClassesForMultiDex'.
> java.util.zip.ZipException: duplicate entry: com/sun/mail/handlers/message_rfc822.class

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED 

Then i see in External Libraries there are libraries activation-1.1 and mail-1.4 in addition to the libraries in libs folder(mail.jar, activation.jar, additional.jar). The External libraries(mail-1.4 and activation-1.1) are included automatically as they are required dependencies for Google Data Java Client Library which i am using to get data from picasa. Please find below build.gradle :

apply plugin: 'com.android.application'
android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "krishtech.photosync"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.facebook.android:facebook-android-sdk:4.1.0'
    compile 'com.google.android.gms:play-services-identity:7.5.0'
    compile('com.google.gdata:core:1.47.1') 
    compile('com.google.api-client:google-api-client:1.16.0-rc') {
        exclude(group: 'xpp3', module: 'xpp3') 
        exclude(group: 'org.apache.httpcomponents', module: 'httpclient') 
        exclude(group: 'junit', module: 'junit') 
        exclude(group: 'com.google.android', module: 'android') 
    }
    compile('com.google.api-client:google-api-client-android:1.16.0-rc') {
        exclude(group: 'com.google.android.google-play-services', module: 'google-play-services')
    }
    compile files('libs/activation.jar')
    compile files('libs/additionnal.jar')
    compile files('libs/mail.jar')
}

So now I am in a fix. If I have to remove mail-1.4 in External Libraries folder (Which I am not able to do it, please let me know how do i do that) then I may cause gdata library to malfunction and if I do not include java port of mail.jar I may not be able to upload the Photo. Please suggest how do I solve this issue.


Solution

  • Well, I solved this issue. The trick was to exclude the mail-1.4.jar that comes along with com.google.gdata:core:1.47.1 to be included while building the application. Made the following changes to build.gradle and now my application is running and also am able to upload to Pics to Picasa using android port of Java mail library.

    compile('com.google.gdata:core:1.47.1') {
        exclude(group: 'javax.mail', module: 'mail')
    }