简体   繁体   English

将库构建为jar文件

[英]Build library as jar file

I have a gradle project. 我有一个gradle项目。 This project depends on android sdk. 此项目取决于android sdk。 Now I want to generate a jar library for this project in order to use in other android applications. 现在,我想为此项目生成一个jar库,以便在其他android应用程序中使用。

I have such gradle file: 我有这样的gradle文件:

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. 它可以编译并生成一个jar文件。 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. 我可以将其导入到我的android应用程序项目中(同时尝试了eclipse和android studio),并且ide可以从我的库中查看类并将其编译为apk。 (This library is marked to output). (该库被标记为输出)。 Other libraries (for example TestFlightLib.jar) work fine. 其他库(例如TestFlightLib.jar)可以正常工作。

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". 我通过使用“ android-library” gradle插件而不是“ java”解决了这一问题。 It does not crash in runtime but the IDE (Android Studio) does not recognize classes from my library (however it is building fine) 它不会在运行时崩溃,但是IDE(Android Studio)无法识别我的库中的类(但是它可以正常运行)

I've added such task to gradle file: 我已经将这样的任务添加到gradle文件中:

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:- 如果您正确创建了jar,请执行以下操作:

  1. Copy the jar file in your project's libs folder. 将jar文件复制到项目的libs文件夹中。
  2. Add this project as jar by going in project's properties and selecting java build path. 通过进入项目的属性并选择java构建路径,将该项目添加为jar。
  3. Clean the project and build. 清理项目并生成。

For creating jar, you must only export the source code of the project and nothing else. 对于创建jar,您只需要导出项目的源代码即可。 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. 告诉我它是否有效,是否需要帮助,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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