简体   繁体   中英

Build library as jar file

I have a gradle project. This project depends on android sdk. Now I want to generate a jar library for this project in order to use in other android applications.

I have such gradle file:

apply plugin: 'java'

dependencies {
    Properties props = new Properties()
    props.load(new FileInputStream("./local.properties"))
    compile files(props.get("sdk.dir")+'/platforms/android-18/android.jar')
}

it is compilig fine and a jar file is generated. I can import it to my android app project (tried both eclipse and android studio) and the ide can see classes from my library and compile evryting to apk. (This library is marked to output). Other libraries (for example TestFlightLib.jar) work fine.

The problem appears in runtime, it cannot find the classes from my libraries:

java.lang.NoClassDefFoundError: com/solvek/test/MyClass
        at com.example.jartest.MainActivity.onCreate(MainActivity.java:29)
        at android.app.Activity.performCreate(Activity.java:5133)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
        at android.app.ActivityThread.access$600(ActivityThread.java:153)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5289)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.solvek.test.MyClass" on path: DexPathList[[zip file "/data/app/com.example.jartest-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.jartest-2, /vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:461)                

I resolved this by using "android-library" gradle plugin instead of "java". It does not crash in runtime but the IDE (Android Studio) does not recognize classes from my library (however it is building fine)

I've added such task to gradle file:

task jar(type: Jar) {
    from android.sourceSets.main.java
}

The solution was found here .

For eclipse:-

If you have rightly created the jar, do following:-

  1. Copy the jar file in your project's libs folder.
  2. Add this project as jar by going in project's properties and selecting java build path.
  3. Clean the project and build.

For creating jar, you must only export the source code of the project and nothing else. All the rest of the resources like - drawables, values, layouts must be copied in your project resources accordingly.

Tell me if it works and if need help do tell me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM