I'm using javax.mail to send mails from my activity. To develop this app, I'm using Android Studio.
To use this method, 3 .jar files are needed (mail.jar, activation.jar and 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.
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.
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:
Then I go to the top-level gradle.build and add:
allprojects {
repositories {
mavenCentral()
}
}
And after this, in the 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:
top-level 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:
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. So put those jar files in your libs folder and add the following in your build.gradle(Module).
dependencies {
compile files('libs/mail.jar')
compile files('libs/activation.jar')
compile files('libs/additionnal.jar')
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With