1

首先,我知道有几个针对这个主题的问题,但没有一个对我有帮助。


我正在使用 Android/ADT 插件开发我的第一个 Android 项目,我想使用这个库。我在 SO 和官方文档中搜索了如何完成此操作,并找到了如何引用库项目

我运行了以下命令:

$ android update project --target 8 /path/to/workspace/myproject /path/to/workspace/numberpicker/lib
Resolved location of library project to: /path/to/workspace/numberpicker/lib
Updated project.properties
Updated local.properties
No project name specified, using Activity name 'SplashActivity'.
If you wish to change it, edit the first line of build.xml.
Added file /path/to/workspace/myproject/build.xml
Updated file /path/to/workspace/myproject/proguard-project.txt

显然这一步工作正常。但是当我尝试com.michaelnovakjr.numberpicker.NumberPicker在 xml 布局中引用时,Eclipse 抱怨它找不到这个类。

然后我找到了这个答案并改变了我的project.properties

# Before
android.library.reference.1=/path/to/workspace/numberpicker/lib
# After
android.library.reference.1=../numberpicker/lib

但没有解决我的问题。

或许值得一提的是,这个库不是 Eclipse 项目,所以我无法在 Eclipse 中打开它。

4

1 回答 1

1

库项目必须存在于同一个工作区中才能让 Eclipse 正确构建依赖项目,有关更多详细信息,请参阅开发指南管理项目 - 库项目

但是,库项目与标准 Android 应用程序项目的不同之处在于,您不能将其直接编译为自己的 .apk 并在 Android 设备上运行。同样,您不能将库项目导出到自包含的 JAR 文件,就像对真正的库所做的那样。相反,您必须通过在依赖应用程序中引用库并构建该应用程序来间接编译库。

When you build an application that depends on a library project, the SDK tools compile the library into a temporary JAR file and uses it in the main project, then uses the result to generate the .apk. In cases where a resource ID is defined in both the application and the library, the tools ensure that the resource declared in the application gets priority and that the resource in the library project is not compiled into the application .apk. This gives your application the flexibility to either use or redefine any resource behaviors or values that are defined in any library.

In order to use numberpicker library, you need imported it as an Android Project in Eclipse, choose File -> New -> Other ... -> Android -> Android project, in the wizard window, check Create projet from existing source and choose the location.

于 2012-05-25T21:28:50.337 回答