Eclipse 工程引用 与 Jar打包

来源:互联网 发布:php开源企业站 编辑:程序博客网 时间:2024/05/20 13:17

一 Eclipse 项目打成jar包

 

1.右键项目Export ---->Java目录下JAR file


 

2.选择打包的文件和输出的格式

 


①选择要打包的文件:

不用全选,全选只会额外占用空间,通常建议只选src目录或是其中需要的java文件或者可以加上res目录,右侧通常只选.classpath。这里说的只是常规做法,具体情况具体分析。

②选择要输出的内容格式:

 * Export generated class files and resources 表示只导出生成的.class文件和其他资源文件。

* Export all output folders for checked projects 表示导出选中项目的所有文件夹。

* Export java source file and resouces 表示导出的jar包中将包含你的源代码*.java,如果你不想泄漏源代码,那么就不要选这项了。

* Export refactorings for checked projects 把一些重构的信息文件也包含进去

通常选第一个就好。

3.其他根据提示设置即可。

 

注意:①若打包的项目又用到了其他的jar包,则该项目打成的包里不会包含引用的jar包。可以采用java打包命令把两个包再打成一个包对外提供。

jar包里不能包含manifest文件,因为同一项目中不能包含两个manifest文件。

 

4.打包命令参考:

@echo off::转到当前盘符%~d0::打开当前目录cd %~dp0::你做的主JAR包的路径,可将其中一个jar做主Jarset MainJar=my.jar::第三方JAR包的路径set jar1=android.jarset jar2=log4j.jar::第三方JAR包顶级包名称,可先变zip后解压获得:set packageName1=deset packageName2=orgecho =========== start combin ==============::解压第三方包,也可直接jar -xf android.jar,主jar包不需要解压命令jar -xf %jar1%jar -xf %jar2%echo =========== start combin jar -xf ==============::合并主JAR包,也可不使用%引用,"jar -uf android.jar de"jar -uf %MainJar%%packageName1%jar -uf %MainJar%%packageName2%::jar -uf %MainJar%%pachageName3%::如果还有别的顶级包可以接着合并,例如:::jar -uf %MainJar%%packageName2%::jar -uf %MainJar% org::jar -uf %MainJar% cn echo =========== over ==============echo 再点一下就结束了--pause



参考博文:
http://bbs.9ria.com/thread-160445-1-1.html
http://shadowkong.com/archives/1125



二 Eclipse引用工程

eclipse 引用工程的两种方法

方法一是直接property --> android 中添加 project library,前提是要引用的项目已经设置isLibrary属性。这时当前项目的android dependencies目录就会自动添加被引用项目。并且当前项目的project.properties文件会生成android.library.reference.1=XXXX。推荐

方法二是property --> java build path 的project选项卡中 add 工程,这个方法不需要被引用项目设置isLibrary属性,但是这个方法还需要设置java build path 的order and export 选项卡将被引用项目也输出。否则会报找不到类。方法一不需要设置order and export,是因为order and export默认已经勾选当前工程、android private libraries 和 android dependencies。不推荐,这种方法无法引用project的资源文件,并且无法导出引用项目的jar包,会导致程序崩溃。


Issue:

上述两种方法都可查看源码:

目前方法一当项目A引用项目B的类方法,ctrl+左键只能跳入.class文件,不能跳入java文件,无法直接对library project代码进行修改。(怀疑对library只有读的权限)。

方法二可以直接跳入library project java文件修改源码。


jar包冲突:

当前项目和引用项目引用的jar包发生重复冲突时,并且当前项目的jar包已经包含引用项目的jar包,就可以把当前项目的jar包直接移到引用项目的jar里,把引用项目的jar包删掉,则正常,也就是jar的位置不影响项目对jar包的引用。


三  Eclipse引用 jar 包

法1. 直接把jar包复制到当前项目的libs目录即可。(通常项目就会自动添加这个jar包,若还不行,可右键jar包--->build path---->add to build path)

法2. 也可以右键Properties---->Java Build path---->libraries---->add jar/add external jars。还需要设置 java build path 的order and export选项选中添加的jar包,这样jar包才能添加到apk中,才不会在运行时报错。

区别:法1不需要设置java build path 的order and export选项是因为order and export默认已经勾选当前工程、android private libraries 和 android dependencies。复制到libs目录相当于添加到android private libraries中。但是法2通过java build path 里的 add external jar 方法添加外部的jar包,不会添加上述的文件夹,会在当前工程视图中新建一个Referenced Libraries目录来存放jar包。


查看源码:

法1可以查看源码,法2目前没有找到查看源码的方法。

法1查看源码方法:来源http://stackoverflow.com/questions/9873152/how-to-attach-javadoc-or-sources-to-jars-in-libs-folder

Step by step guide

In order to link the sources and javadoc to a .jar library that is automatically linked by Eclipse you have to do the following:

  1. Place the library .jar file in the libs folder, and the associated source .jar and doc .jar files in separate subfolders such as libs/src and libs/docs. You can use a name other than src and docs if you want, but it's important that the .jar files aren't directly in the libs folder.
  2. Create a .properties file in the libs folder with the exact name of the actual library .jar (see example). Make sure you keep the .jar part.
  3. Specify the relative paths to the sources and javadoc .jar in the .properties file.
  4. Close and re-open the Eclipse project! Optionally, refresh the project by pressing F5.
  5. Select an object of the linked library in the source code.
  6. Open the Javadoc view in Eclipse to check the documentation (see screenshot).
  7. Open the source code declaration (default shortcut: F3) of the selected object.


Example

The example uses the Gson library.

Directory structure of the libs folder:


Contents of gson-2.2.2.jar.properties



补充说明

 jar包查看源码需要 有 源码的jar文件 source .jar。获取源码jar文件,可以通过eclipse生成。

生成方法同上,Export --> Java --> Java File

然后选择:




与上面打jar包的区别是,生成源码的jar文件,需要勾选第三个。并且文件类型可以只选java目录即可。




0 0
原创粉丝点击