首页 > 技术文章 > 在android中,编译的项目使用到第三方jar的导入方法 终极版!

cynchanpin 2017-06-22 17:07 原文

       1,在android系统环境中编译自己的项目时,往往会用到第三方jar包。这些jar包在eclipse中加入编译,一路畅通,由于eclipse已经帮助你配置好了。可是当把这个项目复制到系统环境中编译时,jar包就会无论用。

以下是自己遇到的问题。通过查找网上的资料,遇到各种问题。最后最终解决。通过博客总结一下,给大家分享。


条件:比如:先在eclipse中开发的应用。用到support-v4包和第三方pinyin4j-2.5.0.jar。
移植到系统项目中,编译不通过。以系统的music应用为例。


1,首先之是增加Android.mk文件
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-java-files-under, src) \
    src/com/android/music/IMediaPlaybackService.aidl
LOCAL_PACKAGE_NAME := Music
LOCAL_PROGUARD_FLAG_FILES := proguard.flags
include $(BUILD_PACKAGE)
编译会找不到引用的包中对应的类和方法。
2,然后在声明包,在.mk中加入
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
#声明包名
LOCAL_STATIC_JAVA_LIBRARIES := android-support-v4 pinyin4j-2.5.0
LOCAL_SRC_FILES := $(call all-java-files-under, src) \
    src/com/android/music/aidl/IMediaService.aidl
LOCAL_PACKAGE_NAME := Music
include $(BUILD_PACKAGE)
include $(CLEAR_VARS)
#指明包的位置
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := pinyin4j-2.5.0:lib/pinyin4j-2.5.0.jar
include $(BUILD_MULTI_PREBUILT)
3,这时编译有可能还会报错。(...can't find superclass or interface...)
Warning: demo.Pinyin4jAppletDemo$1: can't find superclass or interface java.awt.event.WindowAdapter
Warning: demo.Pinyin4jAppletDemo$3: can't find superclass or interface java.awt.event.ActionListener
Warning: demo.Pinyin4jAppletDemo$2: can't find superclass or interface java.awt.event.ActionListener
Warning: demo.Pinyin4jAppletDemo: can't find superclass or interface javax.swing.JApplet
Warning: demo.Pinyin4jAppletDemo$1: can't find referenced class java.awt.event.WindowAdapter
Warning: demo.Pinyin4jAppletDemo$1: can't find referenced method 'void stop()' in class demo.Pinyin4jAppletDemo
Warning: demo.Pinyin4jAppletDemo$1: can't find referenced method 'void destroy()' in class demo.Pinyin4jAppletDemo
Warning: demo.Pinyin4jAppletDemo$1: can't find referenced class java.awt.event.WindowAdapter
Warning: demo.Pinyin4jAppletDemo$1: can't find referenced class java.awt.event.WindowEvent
Warning: demo.Pinyin4jAppletDemo$3: can't find referenced class javax.swing.JComboBox
Warning: demo.Pinyin4jAppletDemo$3: can't find referenced class javax.swing.JComboBox
这好像的混淆编译造成的错误,然后在应用的根文件夹下建立proguard.cfg这个文件,在里面输入:
-dontwarn demo.** 
-keep class demo.** { *;} 

查看编译报的错误,有几个包出错。就在里面加几个这种声明。这里是有demo.下的java报错,仅仅加这个即可。然后在Android.mk中加入此文件的标识。
LOCAL_PROGUARD_FLAG_FILES := proguard.cfg

然后在编译。应该就能够通过。


大功告成。。


      2,然后后来又用到还有一个jar包,出现了新问题:下面是出现的warring:

Warning: org.opencv.android.CameraBridgeViewBase: can't find referenced class org.opencv.R$styleable
Warning: org.opencv.android.CameraBridgeViewBase: can't find referenced class org.opencv.R$styleable
Warning: org.opencv.android.CameraBridgeViewBase: can't find referenced class org.opencv.R

×××××

Warning: there were 3 unresolved references to classes or interfaces.
         You may need to specify additional library jars (using '-libraryjars'),
         or perhaps the '-dontskipnonpubliclibraryclasses' option.


使用上面的方法就无论用。

可是在proguard.cfg文件里加入一句这个就能够忽略warring。

-ignorewarnings



解决的方法都是在网上找了,自己总结一下。谢谢各位在网上的分享。


推荐阅读