源地址:
http://www.cnblogs.com/hopetribe/archive/2012/04/23/2467060.html
<wbr></wbr>
Android.mk添加第三方jar包
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_STATIC_JAVA_LIBRARIES:= xsocket jackson-mapper logging jackson-core javatar log4j
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := test
LOCAL_CERTIFICATE := platform
include $(BUILD_PACKAGE)
##################################################
include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := xsocket:lib/xSocket-2.8.14.jar \
jackson-mapper:lib/jackson-mapper-asl-1.6.2.jar \
logging:lib/commons-logging.jar \
jackson-core:lib/jackson-core-asl-1.6.2.jar \
javatar:lib/javatar-2.5.jar \
log4j:lib/log4j-1.2.15.jar
include $(BUILD_MULTI_PREBUILT)
# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))
<wbr></wbr>
以上是我的一个项目中所需要的第三方jar包,主要参考了Android源码中的Calculator应用,该应用也引用了一个第三方的jar包arity-2.1.2.jar。
需要注意的是,当你要引用的jar包不止一个时,有两个关键的地方需要注意的。
LOCAL_STATIC_JAVA_LIBRARIES<wbr>:= xsocket jackson-mapper logging jackson-core javatar log4j</wbr>
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES<wbr>:= xsocket:lib/xSocket-2.8.14.jar \<br style="padding:0px; margin:0px"> jackson-mapper:lib/jackson-mapper-asl-1.6.2.jar \<br style="padding:0px; margin:0px"> logging:lib/commons-logging.jar \<br style="padding:0px; margin:0px"> jackson-core:lib/jackson-core-asl-1.6.2.jar \<br style="padding:0px; margin:0px"> javatar:lib/javatar-2.5.jar \<br style="padding:0px; margin:0px"> log4j:lib/log4j-1.2.15.jar</wbr>
xsocket jackson-mapper logging jackson-core javatar log4j这几个只是名字,可以随便取。LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES后面才是真正其作用的地方。如:
xsocket:lib/xSocket-2.8.14.jar 引用的是lib目录中的xSocket-2.8.14.jar。
还要注意的是 := 不要写成了+=了哦。
<wbr></wbr>
附:编写各种类型的Android.mk,出处我忘记了,是以前浏览是拷贝下来的。如果侵犯了作者的权益,烦请联系我hopetribe@gmail.com。我及时删除。
<wbr></wbr>
一、编译一个简单的APK
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Build all java files in the java subdirectory
LOCAL_SRC_FILES := $(call all-subdir-java-files)
# Name of the APK to build
LOCAL_PACKAGE_NAME := LocalPackage
# Tell it to build an APK
include $(BUILD_PACKAGE)
二、编译一个依赖静态.jar文件的APK
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# List of static libraries to include in the package
LOCAL_STATIC_JAVA_LIBRARIES := static-library
# Build all java files in the java subdirectory
LOCAL_SRC_FILES := $(call all-subdir-java-files)
# Name of the APK to build
LOCAL_PACKAGE_NAME := LocalPackage
# Tell it to build an APK
include $(BUILD_PACKAGE)
注:LOCAL_STATIC_JAVA_LIBRARIES 后面应是你的APK程序所需要的JAVA库的JAR文件名。
三、编译一个需要platform key签名的APK
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Build all java files in the java subdirectory
LOCAL_SRC_FILES := $(call all-subdir-java-files)
# Name of the APK to build
LOCAL_PACKAGE_NAME := LocalPackage
LOCAL_CERTIFICATE := platform
# Tell it to build an APK
include $(BUILD_PACKAGE)
注:LOCAL_CERTIFICATE 后面应该是签名文件的文件名
四、编译一个需要特殊vendor key签名的APK
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Build all java files in the java subdirectory
LOCAL_SRC_FILES := $(call all-subdir-java-files)
# Name of the APK to build
LOCAL_PACKAGE_NAME := LocalPackage
LOCAL_CERTIFICATE := vendor/example/certs/app
# Tell it to build an APK
include $(BUILD_PACKAGE)
五、装载一个普通的第三方APK
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Module name should match apk name to be installed.
LOCAL_MODULE := LocalModuleName
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_CERTIFICATE := platform
include $(BUILD_PREBUILT)<wbr><br style="padding:0px; margin:0px"></wbr>
六、装载需要.so(动态库)的第三方apk
LOCAL_PATH := $(my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := baiduinput_android_v1.1_1000e
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_CERTIFICATE := platform
include $(BUILD_PREBUILT)
#################################################################
####### copy the library to /system/lib #########################
#################################################################
include $(CLEAR_VARS)
LOCAL_MODULE := libinputcore.so
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)
LOCAL_SRC_FILES := lib/$(LOCAL_MODULE)
OVERRIDE_BUILD_MODULE_PATH := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)
include $(BUILD_PREBUILT)
七、编译一个静态java库
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Build all java files in the java subdirectory
LOCAL_SRC_FILES := $(call all-subdir-java-files)
# Any libraries that this library depends on
LOCAL_JAVA_LIBRARIES := android.test.runner
# The name of the jar file to create
LOCAL_MODULE := sample
# Build a static jar file.
include $(BUILD_STATIC_JAVA_LIBRARY)
注:LOCAL_JAVA_LIBRARIES表示生成的java库的jar文件名。
<wbr></wbr>
******************************************************************************************************************
<wbr></wbr>
源地址:
http://blog.csdn.net/liushaogeng/article/details/6641704
<wbr></wbr>
android工程中引入第三方JAR包后安装APK时老是提示找不到库文件 || Android如何将程序打成jar包 || 运行java.lang.noclassdeffounderror错误
<wbr></wbr>
<wbr></wbr>
使用第三方jar包
除了我以下介绍的方法,别的方法我基本上都试验过,均会出现编译或运行错误。以下方法经测试编译和运行都完全没问题。
步骤:
方法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<wbr><wbr>–> Use as source folder。</wbr></wbr>
方法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 $(call all-makefiles-under,$(LOCAL_PATH))
解释:LOCAL_STATIC_JAVA_LIBRARIES :=<wbr><span style="word-wrap:normal; word-break:normal">libpowerjar</span>和LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES :=<wbr><span style="word-wrap:normal; word-break:normal">libpowerjar</span>:<span style="word-wrap:normal; word-break:normal">classes.jar</span>其实libarity是个名字可以随便取,两个属性的值要一样就好了,但是后面那个冒号里面就一定要写你的jar包名,你的jar包一定放在工程的根目录就好了。我这里是使用两个jar包的例子。</wbr></wbr>
3,源码目录下编译即可:# mmm package/app/testPowerjar,如果出错提示说找不到jar包所打的package,那么请#touch<wbr><span style="word-wrap:normal; word-break:normal">classes.jar</span><wbr>即可.<br></wbr></wbr>
生成jar包:
方法1:Eclipse下,
1. 创建java 或android project;
2. 工程属性java build path->Libraries中加入android.jar
3. 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<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> LOCAL_MODULE := powersave
include $(BUILD_JAVA_LIBRARY)
# additionally, build unit tests in a separate .apk
include $(call all-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(自己的模块)下,