当前位置: 动力学知识库 > 问答 > 编程问答 >

android studio: NoClassDefFoundError exception on runtime with Java Library Module

问题描述:

I have a problem with running my app. The issue seems to be that any java library modules in android studio 0.4.2 create NoClassDefFoundError when classes in these modules are referenced at runtime. The app compiles without errors or warnings.

My project contains these 4 modules:

  1. SimpleMessenger (Java Library module)
  2. Nfc.Benchmark.Domain (Java Library module)
  3. Nfc.Benchmark.View (Android Library module)
  4. Nfc.Benchmark.View.Gui (Android application module)

When the app is running and the simplemessenger.MessengerService class is referenced, the execution fails with a NoClassDefFoundError exception.

Stacktrace:

01-09 14:40:13.819 1152-1152/no.as.gold.nfc.benchmark.view.gui E/AndroidRuntime﹕ FATAL EXCEPTION: main

java.lang.NoClassDefFoundError: no.as.gold.simplemessenger.MessengerService

at no.as.gold.nfc.benchmark.view.BaseTagFragment.registerMessageListeners(BaseTagFragment.java:37)

at no.as.gold.nfc.benchmark.view.BaseTagFragment.<init>(BaseTagFragment.java:26)

at no.as.gold.nfc.benchmark.view.ReadTagFragment.<init>(ReadTagFragment.java:27)

at no.as.gold.nfc.benchmark.view.gui.activities.MainActivity$SectionsPagerAdapter.<init>(MainActivity.java:317)

at no.as.gold.nfc.benchmark.view.gui.activities.MainActivity.refreshActionBar(MainActivity.java:194)

at no.as.gold.nfc.benchmark.view.gui.activities.MainActivity.onCreate(MainActivity.java:72)

at android.app.Activity.performCreate(Activity.java:5255)

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2049)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2110)

at android.app.ActivityThread.access$600(ActivityThread.java:138)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)

at android.os.Handler.dispatchMessage(Handler.java:99)

at android.os.Looper.loop(Looper.java:137)

at android.app.ActivityThread.main(ActivityThread.java:4940)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:511)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)

at dalvik.system.NativeStart.main(Native Method)

Source code where the exception is thrown (MessengerService.Default.Register(...)):

public abstract class BaseTagFragment extends Fragment {

//region fields

private Tag mTag;

//endregion

//region Constructors

/**

* Constructor that initiates the BaseTagFragment

*/

public BaseTagFragment() {

// Add message listeners

registerMessageListeners();

}

//endregion

//region Properties

public Tag GetTag() {return mTag;}

//endregion

//region Private methods

private void registerMessageListeners() {

// Extract tags from new intents

MessengerService.Default.Register(this, NewIntentMessage.class, new MessageHandler<NewIntentMessage>() {

@Override

public void handler(NewIntentMessage msg) {

Tag tag = msg.Intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

if(tag != null) mTag = tag;

}

});

}

//endregion

}

Before i upgraded to Android Studio 0.4.2, i removed the .gradle cache as described here: Android Studio 0.3 Java Library Module doesnt work and that worked fine until the upgrade. Now i'm at a loss, i've searched for hours without finding anything useful.

Any help is greatly appreciated!

Modified build.gradle with SimpleMessenger.jar added to the libs\ folder (This did not solve the issue):

buildscript {

repositories {

mavenCentral()

}

dependencies {

classpath 'com.android.tools.build:gradle:0.7.+'

}

}

apply plugin: 'android-library'

repositories {

mavenCentral()

}

android {

compileSdkVersion 17

buildToolsVersion '17.0.0'

defaultConfig {

minSdkVersion 16

targetSdkVersion 16

}

release {

runProguard false

proguardFile 'proguard-rules.txt'

proguardFile getDefaultProguardFile('proguard-android.txt')

}

}

dependencies {

compile project(':Nfc.Communication')

compile 'org.apache.commons:commons-lang3:3.1'

compile 'com.google.android:support-v4:r6'

compile project(':Nfc.Benchmark.Domain')

//compile project(':SimpleMessenger')

compile files('libs/SimpleMessenger.jar')

}

网友答案:

This issue is resolved in Android Studio 0.4.4 :D (http://tools.android.com/recent/androidstudio044released)

网友答案:

You need to include SimpleMessenger.jar in your main project's dependencies, not just from your library project. Gradle doesn't transitively link in dependencies from libraries; they're only used to compile the library code itself.

网友答案:

I downgraded to Android Studio 0.3.2. Once this was done, i deleted the .gradle cache and built the solution(ctrl + F9), and then it works. It's a workaround, so if anyone knows of any other solution, please post :)

分享给朋友:
您可能感兴趣的文章:
随机阅读: