在android中,編譯的項目使用到第三方jar的匯入方法 終極版!

來源:互聯網
上載者:User

標籤:java   android   第三方jar包   proguard.flags   

       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



解決辦法都是在網上找了,自己總結一下。謝謝各位在網上的分享。

在android中,編譯的項目使用到第三方jar的匯入方法 終極版!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.