Problem with java module file system in Java java

I have an Android gradle project which consists of a main module and a clean java library. The Java library has a dependency on the jar file (Gson is shown here for demo purposes only):

tree structure

The project compiles fine, but when I try to run it, it fails with NoClassDefFoundError:

java.lang.NoClassDefFoundError: com.google.gson.Gson
    at com.example.JavaLibClass.javaMethod(JavaLibClass.java:7)
    at com.denisk.deptest.MainActivity.onCreate(MainActivity.java:15)
    at android.app.Activity.performCreate(Activity.java:5135)

If I replaced the dependency jarwith a regular dependency on Maven, it works.

If I move the dependency jarfrom javalibto appand add the dependency Gsonto the activity, it works.

app / build.gradle :

dependencies {
    compile project(':javalib')
}

javalib / build.gradle

apply plugin: 'java'

dependencies {
    compile files('libs/gson-2.2.4.jar')
}

MainActivity onCreate (inside app):

@Override
protected void onCreate(Bundle savedInstanceState) {

    JavaLibClass javaLibClass = new JavaLibClass();
    javaLibClass.javaMethod();

}

JavaLibClass (inside javalib):

public class JavaLibClass {
    public void javaMethod() {
        Gson gson = new Gson();
    }
}

Github :

https://github.com/denisk20/android-gradle-dependency-test

- ?


1: , Android Studio Gradle.

2: gradle 1.4.0-beta4 AOSP - .

+4
2

, , , . , VM Android 5.1 , . , Android 2.3 VM Dalvik, .

+1

, , jar . , Jar lib , . maven , . , "" > " ", "" "", jar " ", lib. , .

0

Source: https://habr.com/ru/post/1610438/


All Articles