本篇为您介绍如何将Android开发的组件打包成JAR安装包,通过封闭成JAR包,可以重复利用,非常有利于扩展和减少工作重复性,下面开始:
首先Android 工程的基本面貌是这样:
![eclipse中android工程打成jar包 eclipse中android工程打成jar包](https://i-blog.csdnimg.cn/blog_migrate/791ac62a9e058fb9d692fdba7065c68b.jpeg)
当然对应的Activity 文件被我删除了,因为当编译成jar 包我们并不需要Activity 文件。
以上面这个工程为例,我们将它打包成JAR包步骤为:
右键工程选择导出:
![eclipse中android工程打成jar包 eclipse中android工程打成jar包](https://i-blog.csdnimg.cn/blog_migrate/18f4489a1aa77eddafae5da8655cdbc0.gif)
选择导出目标为:java->JAR文件:
![eclipse中android工程打成jar包 eclipse中android工程打成jar包](https://i-blog.csdnimg.cn/blog_migrate/e5da47d4bc62001928c8750cd2a74db6.jpeg)
把一些不必要的文件勾选掉,如下图:
![eclipse中android工程打成jar包 eclipse中android工程打成jar包](https://i-blog.csdnimg.cn/blog_migrate/42bbaf8deeac7370c230aa6a84ede453.jpeg)
一般,不需要打入jar包的文件有:
1、/src/目录下的activity文件
2、根目录下的文件,.classpath, .project, manifest.xml, .properties, .cfg等
3、/bin/目录下的文件,如:manifest.xml, .dex, .apk, resources.ap_等
4、/bin/res目录
5、/res/drawable 相关的目录
6、/res/layout/ 目录
7、/res/menu/ 目录
到了这一步,己经基本完成,浏览选择jar 文件导出路径即可。
导出完成后,我们就可以像使用其他JAR文件一样使用我们自己的自定义控件包了。下面给出一个小DEMO介绍如何使用这个JAR包。
步骤一:
新建文件夹lib,将jar 包放入。
步骤二:
关联JAR包,如下图:
步骤三,使用JAR包:
如下代码:
<</span>LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:gif
="http://schemas.android.com/apk/res/com.terry.jarTest"
android:id
="@+id/layout"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
<</span>Button android:text="停止" android:id="@+id/Button01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></</span>Button>
<</span>Button android:text="开始" android:id="@+id/Button02"
android:layout_width="wrap_content" android:layout_height="wrap_content"></</span>Button>
<</span>com.terry.gif.TypegifView
android:layout_width="fill_parent" android:id="@+id/gifView1"
gif:stop="true" android:layout_height="wrap_content"
gif:delay="1"></</span>com.terry.gif.TypegifView>
</</span>LinearLayout>
有一个不好的就是如果你项目中存在使用属性,必须也把属性一起复制到你要使用的项目中,要不然会识别不了。