1

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: https://stackoverflow.com/questions/19521764/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')
}
Community
  • 1
  • 1
Aage Dahl
  • 25
  • 8
  • It may because your Build settings , if you use, add your libraries (.jar files) also in your libs folder . Check the projects in Build Path Libraries and Projects . no class def is from not linking well – Raluca Lucaci Jan 09 '14 at 14:12
  • Did you see this thread: http://stackoverflow.com/questions/16608135/android-studio-add-jar-as-library ? – viplezer Jan 09 '14 at 14:34
  • Did you see this thread: http://stackoverflow.com/questions/16608135/android-studio-add-jar-as-library ? – viplezer Jan 09 '14 at 14:35
  • I saw and tried the suggestions in this post coelho, still got the same error. – Aage Dahl Jan 09 '14 at 14:48
  • @RalucaGurau: I have added the jars of the referenced library to the project, and i still have the same issue. I'll add the modified build.gradle file to the description. – Aage Dahl Jan 09 '14 at 15:00
  • the liobrary has to be also copied into your libs folder , your project -> libs ,this + added in build path . For me this was the error many times. – Raluca Lucaci Jan 09 '14 at 15:03
  • If you have some doubt about the Android Studio, Please have a try with IntelliJ IDEA 13 which I am using and I am waiting for the 1.0 version of Android Studio. :) – herbertD Jan 09 '14 at 15:41
  • @RalucaGurau: The library was added to the libs folder of my project and added to the dependencies of the build.gradle file of the desired project (print of the build.gradle file is supplied above). Should i add it in any other places? – Aage Dahl Jan 09 '14 at 16:03
  • Hey have you solved this issue.... this issue is killing me ..i am using android studio 1.0 still i am getting this error – GangaNaidu Jan 24 '15 at 19:34

3 Answers3

1

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

Aage Dahl
  • 25
  • 8
0

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.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • I tried this too just now, but with no success, it fails durign runtime with the same exception. In any case, SimpleMessenger is under development and i would like the changes made to SimpleMessenger to be directly reflected at compilation time, rather than having to copy and paste the library every time i make changes to it. – Aage Dahl Jan 09 '14 at 17:22
0

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 :)

Aage Dahl
  • 25
  • 8