0

This is utterly frustrating...Any help is greatly appreciated.

Please don't assume that I haven't referred to previous posts on this issue. I did and tried all the suggestions made in those posts to fix this issue several times, but to no avail, though others have posted it worked for them.

Here is the problem.

I added two jar files that are the implementation of json-rpc2 protocol. They are downloaded from the following location.

http://software.dzhuvinov.com//download.html#download-jsonrpc2client

The library files are:

jsonrpc2-base-1.35.jar jsonrpc2-client-1.14.4.jar

I added them to libs folder and then added those libraries by right clicking on them within the studio. I added the dependencies in build.gradle of my project. I cleaned and built the project. Please note all this time the library references were recognized by the compiler and build was successful. Now comes the problem... The same libraries that were properly recognized during coding and during compilation, WOULD NOT WORK DURING RUN TIME. I get the following No Class Found error. Please save the remaining hair on my head. Thanks.

--------- beginning of crash

com.example.admin.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
            Process: com.example.admin.myapplication, PID: 1877
            java.lang.NoClassDefFoundError: com.thetransactioncompany.jsonrpc2.JSONRPC2Request
                    at com.example.admin.myapplication.MainActivity.accessWebService(MainActivity.java:154)
                    at com.example.admin.myapplication.MainActivity$1.onClick(MainActivity.java:55)
                    at android.view.View.performClick(View.java:4756)
                    at android.view.View$PerformClick.run(View.java:19749)
                    at android.os.Handler.handleCallback(Handler.java:739)
                    at android.os.Handler.dispatchMessage(Handler.java:95)
                    at android.os.Looper.loop(Looper.java:135)
                    at android.app.ActivityThread.main(ActivityThread.java:5221)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at java.lang.reflect.Method.invoke(Method.java:372)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Below is the content of build.gradle file.

apply plugin: 'com.android.application'
android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.admin.myapplication"
        minSdkVersion 5
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile files('libs/jsonrpc2-base-1.35.jar')
    compile files('libs/jsonrpc2-client-1.14.4.jar')
}
RamLogic
  • 53
  • 10
  • Need `build.gradle` file content. – songchenwen Mar 18 '15 at 01:01
  • Here is the build.gradle content: – RamLogic Mar 18 '15 at 01:07
  • Have you verified that the class com.thetransactioncompany.jsonrpc2.JSONRPC2Request is in the libraries in question? Is this a class you use directly? – mattm Mar 18 '15 at 02:07
  • @mattm Sure, yes...I verified the contents of the jar file and all required classes are in there. In fact, that is why the compilation is error free...don't you think? – RamLogic Mar 18 '15 at 02:10
  • I think it's possible to have error free compilation if you use Class A in a jar, and Class A requires Class B that is another jar. But if all of the classes you are using are verified to be in the jar, that is not the issue. – mattm Mar 18 '15 at 03:54

2 Answers2

1

@Jacky Thank you for your offer to analyze my code. Luckily, between the time I posted my last response and you posted your subsequent offer, I painstakingly went through Google and Stackoverflow search spree and figured out the solution.

3 updates were required to my code that I posted above to overcome that stubborn NoClassDefFound error.

  1. Change the "dependencies" script to the following:

    dependencies { compile fileTree(dir: 'libs', include: '*.jar')

Key difference, I believe is that unassuming "include" keyword.

  1. Use the "gradlew clean" and "gradlew build" commands from command line in your project home directory.

  2. If you get "lint" errors, add the following code into build.gradle and retry step 2 again.

    lintOptions { abortOnError false }

Of course, credit in resolving this issue goes to the following prior Stackoverflow posts:

android studio java.lang.NoClassDefFoundError:

gradle build fails on lint task

Community
  • 1
  • 1
RamLogic
  • 53
  • 10
0

This error java.lang.NoClassDefFoundError not java.lang.ClassNotFoundException therefore it only happens during the run time, NOT in the build time, like some comments suspected.

The main reason could be your application classpath is ruined.

To make it simple, use the build in tool in android studio to add your jar file:

1) Put jar files in libs directory.

2) File >> Project Structure... (or Ctrl+Alt+Shift+S) >> Dependencies >> Add two jar file inside libs >> OK

Jacky
  • 2,924
  • 3
  • 22
  • 34
  • Under Project Structure, I checked and the dependecies tab already shows the required jar files. I don't have to add anything new there as you mentioned in steps 1 and 2 above...and they are in specified with lib/ folder prefix too... – RamLogic Mar 18 '15 at 03:48
  • Do you mind zip the whole package and send to me? I will give a try – Jacky Mar 18 '15 at 05:29