问题
I am building an application and using Joda Time in it instead java built in Date/Time classes. but i am facing a problem that when i compile the app using Android API 22 it runs fine, but when i compile it with android API 23 it throws NoClassDefFoundError exception. My build.gradle is following
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
defaultConfig {
multiDexEnabled true
applicationId 'com.my.app'
minSdkVersion 11
targetSdkVersion 23
versionCode 6
versionName '5.0.1'
}
buildTypes {
release {
//runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
sourceSets {
main {
assets.srcDirs = ['src/main/assets', 'src/main/assets/downloadMusicMishari', 'src/main/assets/downloadmusicSaad']
}
}
dexOptions {
incremental true
javaMaxHeapSize "4g"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
//compile 'com.android.support:support-v4:20.0.0'
compile project(':facebook')
compile project(':library')
compile files('libs/PayPalAndroidSDK-2.9.10.jar')
compile files('libs/picasso-2.5.2.jar')
compile project(':justifiedTextViewmaster')
compile 'joda-time:joda-time:2.3'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.android.gms:play-services:8.3.0'
compile 'com.android.support:multidex:1.0.0'
}
repositories {
mavenCentral()
}
and here is the screen shot of error.
回答1:
NoClassDefFoundError
Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.
Please check Joda-Time library with Android specialization
Try with:
dependencies {
compile 'net.danlew:android.joda:2.9.2' // For API Level 23 or Above
}
Then Clean-Rebuild Your IDE
Edit
make sure you initialize JodaTimeAndroid.init(this);
Once that's done, you must initialize the library before using it by calling JodaTimeAndroid.init(). I suggest putting this code in Application.onCreate():
public class YourApplicationClass extends Application {
@Override
public void onCreate() {
super.onCreate();
JodaTimeAndroid.init(this);
}
}
Final Edit
Removed Joda plugin
回答2:
NoClassDefFoundError
is thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.
The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.
[See This]
So consider using a JODA library that is specifically designed for Android. You may ask why use this library instead of the other. There is one particular problem with the other. ie due to its usage of ClassLoader.getResourceAsStream()
, it greatly inflates its memory footprint on apps. (For more details, see this blog post.) This library avoids the problem for Android.
Also this library has extra utilities designed for Android. For example, see DateUtils, a port of Android's DateUtils.
Try changing compile 'joda-time:joda-time:2.3'
to: compile 'net.danlew:android.joda:2.9.1'
This works well. And I am using this in an app with SDK 23!
EDIT 1
If it's ISO8601 format, then you can parse it even without using Joda. This way:
try {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date result = format.parse("2015-05-19T04:00:00.000Z");
Calendar cal = Calendar.getInstance();
cal.setTime(result);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
output = day+" "+month+" "+year;
Log.w("THE DATE IS:",""+output);
}
catch (Exception e){e.printStackTrace();}
来源:https://stackoverflow.com/questions/35865127/noclassdeffounderror-exception-android-joda-time