Android Studio:NoClassDefFoundError [英] Android Studio: NoClassDefFoundError

查看:229
本文介绍了Android Studio:NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个使用javamail和gmail的smtp服务发送电子邮件的应用程序,但是当我运行它时,它会在我调用Session.getInstance时崩溃。调试后,它显示它是关于com.sun.mail.util.MailLogger的NoClassDefFoundError。我阅读了其他地方,我必须按顺序添加一个较旧的邮件包获取它,但我仍然收到错误。



以下是我在Android Studio中的内容:

  //获取系统属性
属性properties = System.getProperties();

//安装邮件服务器
properties.put(mail.smtp.host,host);
properties.put(mail.smtp.auth,true);
properties.put(mail.smtp.starttls.enable,true);
properties.put(mail.transport.protocol,smtp);
properties.put(mail.smtp.port,587);

authenticator = getAuthenticator();
/ * getAuthenticator的样子:
返回新的javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
返回新的PasswordAuthentication(myemail@gmail.com ,密码);
}
};
* /

//获取默认的Session对象,使用身份验证
尝试{
session = Session.getInstance(properties,authenticator);
} catch(Error e){
//用于调试的不必要的代码
int hold = 0;
}

//创建一个默认的MimeMessage对象。
MimeMessage message = new MimeMessage(session);

尝试{
// Set From:标题的标题字段。
message.setFrom(new InternetAddress(from));

//设置为:标题的标题字段。
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

//设定主题:标题栏
message.setSubject(subject);

//现在设置实际的消息
message.setText(content);

//发送消息
Transport.send(message);
catch(AddressException e){
throw new Error(bad address);
catch(MessagingException e){
throw new Error(bad message data);
}

我的构建看起来像这样:

  apply plugin:'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion22.0.1

defaultConfig {
applicationIdcom.bsitpa.myfirstapp
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName1.0
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
packagingOptions {
exclude'META-INF / LICENSE.txt'
}
}

依赖关系{
编译fileTree(包括: ['* .jar'],dir:'libs')
编译'com.android.support:appcompat-v7:22.2.1'
编译'javax.mail:javax.mail-api: 1.5.3'
//编译'javax.mail:mail:1.5.0-b01'
编译文件('mail-1.5.0-b01.jar')
}

调试错误:
java.lang.NoClassDefFoundError:Lcom / sun / mail / util / MailLogger的解析失败



只要你知道,我完全是AndroidStudio和Gradle的新手。



感谢一下,
Ryan 不知道你是否已经解决了这个问题,但是我发现你需要使用一个Android专用的Java邮件库。

  compile'c​​om.sun.mail:android-mail:1.5.5'
compile'c​​om.sun。 mail:android-activation:1.5.5'

我遇到了你的错误和其他类似的错误最新的Java邮件库,但切换到这些固定的所有。



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


I'm trying to build an app that sends an email using javamail and gmail's smtp service, but when I run it it crashes when I call Session.getInstance. After debugging it appears that it's a NoClassDefFoundError regarding com.sun.mail.util.MailLogger. I read elsewhere that I had to add an older mail package in order to get it, but I'm still getting errors.

Here's what I have in Android Studio:

 // Get system properties
    Properties properties = System.getProperties();

    // Setup mail server
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.transport.protocol","smtp");
    properties.put("mail.smtp.port", "587");

    authenticator = getAuthenticator();
    /* What getAuthenticator looks like:
    return new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("myemail@gmail.com ", "password");
        }
    };
    */

    // Get the default Session object, with authentication
    try {
        session = Session.getInstance(properties, authenticator);
    } catch (Error e) {
        // Unnecessary code for debugging
        int hold = 0;
    }

    // Create a default MimeMessage object.
    MimeMessage message = new MimeMessage(session);

    try {
        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));

        // Set To: header field of the header.
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

        // Set Subject: header field
        message.setSubject(subject);

        // Now set the actual message
        message.setText(content);

        // Send message
        Transport.send(message);
    } catch (AddressException e) {
        throw new Error("bad address");
    } catch (MessagingException e){
        throw new Error("bad message data");
    }

And my build looks like this:

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

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

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'javax.mail:javax.mail-api:1.5.3'
    //compile 'javax.mail:mail:1.5.0-b01'
    compile files('mail-1.5.0-b01.jar')
}

The debugging error: "java.lang.NoClassDefFoundError: Failed resolution of Lcom/sun/mail/util/MailLogger"

And just so you know, I am completely new to AndroidStudio and Gradle.

Thanks a ton, Ryan

解决方案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆