简体   繁体   English

在Android Studio中添加外部库时出现问题

[英]Issue while adding external libraries in Android Studio

I'm using javax.mail to send mails from my activity. 我正在使用javax.mail从我的活动中发送邮件。 To develop this app, I'm using Android Studio. 要开发此应用程序,我正在使用Android Studio。

To use this method, 3 .jar files are needed ( mail.jar, activation.jar and additionnal.jar ). 要使用此方法,需要3 .jar文件( mail.jar,activation.jar和additionnal.jar )。 All info I have readed about says to put these 3 files in libs folder, add them as library and name them in gradle.build. 我所知道的所有信息都说将这三个文件放在libs文件夹中,将它们添加为库并在gradle.build中命名。

My first problem has come when I have realized that the "Add to library" option is not shown for me. 当我意识到没有为我显示“添加到库”选项时,我遇到了第一个问题。 So I've tried to add the dependecies manually in Module Settings, and them in gradle.build, etc... but no one of these methods worked for me, I couldn't select the imports. 所以我试图在模块设置中手动添加dependecies,并在gradle.build等中添加它们......但是这些方法中没有一个对我有用,我无法选择导入。

But finally I found a way that at a first look seems to work. 但最后我找到了一种初看起来似乎有效的方法。 Going to Module Setings/app/dependencies I select the + button and select "Library dependencies". 转到模块设置/应用程序/依赖项我选择+按钮并选择“库依赖项”。 Then I use Maven Central Search and I found these 2: 然后我使用Maven Central Search,我找到了这两个:

  • javax.mail:mail:1.5.0-b01 javax.mail:邮箱:1.5.0-B01
  • javax.activation:activation:1.1.1 javax.activation中:激活:1.1.1

Then I go to the top-level gradle.build and add: 然后我去顶级gradle.build并添加:

allprojects {
    repositories {
        mavenCentral()
    }
}

And after this, in the app inner gradle.build: 在此之后,在app inner gradle.build中:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'javax.mail:mail:1.5.0-b01'
    compile 'javax.activation:activation:1.1.1'
}

But even this way, I don't get it to work. 但即使这样,我也无法让它发挥作用。 It starts and seems to go fine, but when going to send the email, the app crashes and shows this: 它开始并且似乎很好,但是当要发送电子邮件时,应用程序崩溃并显示:

java.lang.NoClassDefFoundError: javax.activation.DataHandler
        at com.myapp.mail.MailSender.sendMail(MailSender.java:64)
        at com.myapp.TrackingService.sendEmail(TrackingService.java:341)
        at com.myapp.TrackingService.doLocationUpdate(TrackingService.java:281)
        at com.myapp.TrackingService.onStartCommand(TrackingService.java:82)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2726)
        at android.app.ActivityThread.access$2100(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5146)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
        at dalvik.system.NativeStart.main(Native Method)

Update- gradle.build file added: 更新了gradle.build文件:

top-level gradle.build: 顶级gradle.build:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
    }
}

app inner gradle.build: app inner gradle.build:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.irvvin.trackme"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets { main { res.srcDirs = ['src/main/res', 'src/main/res/values-v14'] } }

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

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

   //Now I take "mail" from maveen and "activation" from library, but I have tried diferent
   // combiations, such as selecting all from library (the imports where not showing) and 
   // all from maveen (works but shows this error).
   //With the actual combination, also throws this same error. 
}

As you have the Jar files. 因为你有Jar文件。 So put those jar files in your libs folder and add the following in your build.gradle(Module). 因此,将这些jar文件放在libs文件夹中,并在build.gradle(Module)中添加以下内容。

dependencies {
    compile files('libs/mail.jar')
    compile files('libs/activation.jar')
    compile files('libs/additionnal.jar')
}

Most of the time, if you will clean and rebuild then above issue resolved. 大多数情况下,如果你要清理和重建,那么上面的问题就解决了。

But there is little dependency on your ".jar" or maven dependency. 但是对你的“.jar”或maven依赖很少依赖。 Those are not compatible with current compiler. 那些与当前的编译器不兼容。 If your ".jar" is compatible with Java 1.6 and you are recompiling with Java 1.7 then above issue will occur. 如果您的“.jar”与Java 1.6兼容并且您使用Java 1.7重新编译,则会出现上述问题。

Please check exact message on top of above exception on Android Studio. 请在Android Studio上检查上述异常的确切消息。

Here is my issue: 这是我的问题:

:app:preDexRelease warning: Ignoring InnerClasses attribute for an anonymous inner class (com.amazonaws.org.apache.commons.logging.LogFactory$1) that doesn't come with an associated EnclosingMethod attribute. :app:preDexRelease警告:忽略没有关联的EnclosingMethod属性的匿名内部类(com.amazonaws.org.apache.commons.logging.LogFactory $ 1)的InnerClasses属性。 This class was probably produced by a compiler that did not target the modern .class file format. 这个类可能是由一个没有以现代.class文件格式为目标的编译器生成的。 The recommended solution is to recompile the class from source, using an up-to-date compiler and without specifying any "-target" type options. 建议的解决方案是使用最新的编译器从源代码重新编译类,而不指定任何“-target”类型选项。 The consequence of ignoring this warning is that reflective operations on this class will incorrectly indicate that it is not an inner class. 忽略此警告的后果是,对此类的反射操作将错误地指示它不是内部类。

I resolved by above approach. 我通过上述方法解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM