我怎样才能在NetBeans中的Andr​​oid项目使用外部罐子? [英] How can I use an external jar in an Android project in NetBeans?

查看:164
本文介绍了我怎样才能在NetBeans中的Andr​​oid项目使用外部罐子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个Android库,我可以包括在任何Android应用一个罐子。我使用NetBeans 6.8,该nbandroid插件,和Android SDK。

I need to create an Android library which I can include as a jar in any Android application. I use NetBeans 6.8, the nbandroid plugin, and the Android SDK.

我花了这么远的步骤如下:

The steps I took so far are:

1)创建库项目,用的android.jar包括有机会获得到Android类(库使用android.util.Log和其他Android类)。

1) Create the library project, with android.jar included to have access to Android classes (the library uses android.util.Log and other Android classes).

2)编译图书馆作为一个罐子。

2) Compile the library as a jar.

3)添加库的罐子到Android应用程序(在库中的项目节点下,右键单击并添加JAR)。

3) Add the library's jar to the Android application (right-click on Libraries under the project node and add the jar).

4)添加<使用库>到Android清单。(错误和不必要的)

步骤3,至少可以让我引用该库的类应用程序的源$ C ​​$ C,但类似乎并没有真正包括在编译的时候。当我运行的应用程序,我得到的日志中出现以下错误。

Step 3 at least allows me to reference the library's classes in the application's source code, but the classes don't seem to actually be included at compile time. When I run the application, I get the following error in the log.

I/dalvikvm(  349): Could not find method mylibrarypackage.MyClass.myMethod, referenced from method myapplicationpackage.HomeActivity.onCreate
W/dalvikvm(  349): VFY: unable to resolve static method 985: Lmylibrarypackage/MyClass;.myMethod ()V
D/dalvikvm(  349): VFY: replacing opcode 0x71 at 0x000a
D/dalvikvm(  349): Making a copy of Lmyapplicationpackage/HomeActivity;.onCreate code (160 bytes)
D/AndroidRuntime(  349): Shutting down VM
W/dalvikvm(  349): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
E/AndroidRuntime(  349): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(  349): java.lang.NoClassDefFoundError: mylibrarypackage.MyClass
E/AndroidRuntime(  349):    at myapplicationpackage.HomeActivity.onCreate(HomeActivity.java:58)
E/AndroidRuntime(  349):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(  349):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
E/AndroidRuntime(  349):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
E/AndroidRuntime(  349):    at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime(  349):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
E/AndroidRuntime(  349):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  349):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  349):    at android.app.ActivityThread.main(ActivityThread.java:4310)
E/AndroidRuntime(  349):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  349):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  349):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime(  349):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime(  349):    at dalvik.system.NativeStart.main(Native Method)

我必须将库添加到构建路径别的地方?我失去了一些东西?

Do I have to add the library to the build path somewhere else? Am I missing something?

推荐答案

看来我解决了这个问题。

It appears I solved the problem.

我不知道,如果我阅读有关如何使用nbandroid插件的时候,但生成的build-impl.xml中似乎并没有包含我创建APK时,增加与NetBeans任何图书馆错过了一步。

I don't know if I missed a step when reading about how to use the nbandroid plugin, but the generated build-impl.xml doesn't seem to include any libraries I add with NetBeans when creating the APK.

这是罪证一块ant脚本的:

This is the incriminating piece of ant script:

<target depends="init,compile,-pre-pre-jar,-pre-jar" name="-dex">
    <exec executable="${dx}" failonerror="true">
        <arg value="--dex"/>
        <arg value="--output=${basedir}/${intermediate.dex}"/>
        <arg value="--positions=lines"/>
        <arg path="${build.classes.dir}"/>
    </exec>
</target>

该库包括在内,如果我加入这最后的说法在我的问题提到的错误消失:

The library is included and the error mentioned in my question disappears if I add this last argument:

<arg path="${external.libs.dir}"/>

使用 external.libs.dir 指向包含库的JAR的目录。

With external.libs.dir pointing to the directory containing the library's jar.

感谢克里斯托弗让我看的是Android命令行工具生成的(在NetBeans的nbandroid插件创建一个Android项目时生成的脚本是完全不同的)的build.xml文件。

Thanks to Christopher for making me look at the build.xml generated by the Android command line tools (the script generated when creating an Android project in NetBeans with the nbandroid plugin is quite different).

附录:自从我说的是NetBeans的,这样做的另一种方式是通过重写 - pre-JAR 目标项目的build.xml,代替上述改变build-impl.xml中。这是通过添加以下到build.xml完成​​的:

Addendum: Since I'm talking about NetBeans, another way of doing this is by overriding the -pre-jar target in the project's build.xml, instead of the above change to build-impl.xml. This is done by adding the following to build.xml:

<target name="-pre-jar">
  <copy todir="${build.classes.dir}">
    <fileset dir="${external.libs.dir}" />
  </copy>
</target>

这样一来,图书馆的罐子是present以及内置类,并自动列入。

This way, the library's jar is present along with the built classes, and automatically included.

这篇关于我怎样才能在NetBeans中的Andr​​oid项目使用外部罐子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆