转:https://blog.csdn.net/ylyuanlu/article/details/45744003
步骤:
方法1:Eclipse下,
右键工程,
Build path,
java build path,选择libraries
在右边的按钮中点击“Add Library”
选择“User library”,点击“下一步”
点击“User librarys”按钮
在出现的界面中点击“New…”按钮
在弹出的界面中随便起一个名字,点击“确定”
点击“Add jars”按钮选择第三方jar包,点击“确定”完成。
后面的步骤很重要,如果不进行后面的操作。在Eclipse里显示编译通过,不会有错误,但在模拟器或真机上运行的时候可能就会出现java.lang.noclassdeffounderror之类的错误:
1、在Android项目根目录下新建一个libs文件夹;
2、把你需要的导入的第三方Jar包复制进这个目录;
3、在libs目录上点右键,选Bulid path –> Use as source folder。
方法2,android源码环境下,
1,将out/target/common/obj/JAVA_LIBRARIES/powersave_intermediates下的classess.jar文件放在自己的工程目录下。out/target/product/ventana/system/framework目录下的jar包用以下方法经验证是不好使的,因为他已经打成dex文件了,在工程里引用其包是不行的;
2,修改android.mk文件,如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_STATIC_JAVA_LIBRARIES := libpowerjar libpowersave
LOCAL_PACKAGE_NAME := testPowerjar
LOCAL_CERTIFICATE := platform
include $(BUILD_PACKAGE)
##############################################
include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := libpowerjar:classes.jar libpowersave:testlily.jar
include $(BUILD_MULTI_PREBUILT)
Use the folloing include to make our test apk.
include ( c a l l a l l − m a k e f i l e s − u n d e r , (call all-makefiles-under, (callall−makefiles−under,(LOCAL_PATH))
解释:LOCAL_STATIC_JAVA_LIBRARIES := libpowerjar和LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := libpowerjar:classes.jar其实libarity是个名字可以随便取,两个属性的值要一样就好了,但是后面那个冒号里面就一定要写你的jar包名,你的jar包一定放在工程的根目录就好了。我这里是使用两个jar包的例子。
3,源码目录下编译即可:# mmm package/app/testPowerjar,如果出错提示说找不到jar包所打的package,那么请#touch classes.jar 即可.
生成jar包:
方法1:Eclipse下,
- 创建java 或android project;
- 工程属性java build path->Libraries中加入android.jar
- package explorer中右键点击工程,export java->jar file.
方法2:Android源码环境下
1,修改Android.mk文件,
LOCAL_PATH:= $(call my-dir)
the library
============================================================
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := powersave.java
LOCAL_MODULE := powersave
include $(BUILD_JAVA_LIBRARY)
additionally, build unit tests in a separate .apk
include ( c a l l a l l − m a k e f i l e s − u n d e r , (call all-makefiles-under, (callall−makefiles−under,(LOCAL_PATH))
2,编译 # mmm powersave(自己的模块)
其中,BUILD_JAVA_LIBRARY代表生成jar包,包的位置放在out/target/product/ventana/system/framework和out/target/common/obj/JAVA_LIBRARIES/powersave_intermediates(自己的模块)下,