Android編程之解決android-support-v4打包問題

來源:互聯網
上載者:User

如果工程引入了android-support-v4的jar類庫,那麼在工程打包混淆時,就會出現報錯提示。例如提示你:You may need to specify additional library jars (using '-libraryjars')。

這裡先給出解決方案,稍後我們再來解釋如何處理類似情況:

在proguard.cfg裡的後面,添加如下內容:
[java]
-libraryjars /android-support-v4.jar 
-dontwarn android.support.v4.** 
-keep class android.support.v4.** { *; } 
-keep public class * extends android.support.v4.** 
-keep public class * extends android.app.Fragment 

然後你再打包看看,應該可以正常產生apk安裝包了。

打包出錯:
情況一:
 "類1 can't find referenced class 類2" 字面上的意思就是類1找不到類2的引用;它會建議你:"You may need to specify additional library jars (using '-libraryjars').";
需要使用-libraryjars加上項目中使用到的第三方庫就OK了。
例如:-libraryjars /android-support-v4.jar
注意:這裡引用方式是當前工程的根目錄(也可以配置其他目錄),也就是說,你要把第三方jar放到目前的目錄下,否則就會警告說找不到jar檔案!

情況二:
例如: can't find superclass or interface android.os.Parcelable$ClassLoaderCreator,碰到這樣的情況,可以使用-dontwarn com.xx.yy.**,不對錯誤提出警告。
注意:使用這個方式的話,要確保自己沒有用到這個庫裡面的類!否則就會拋ClassNotFoundException!

情況三:
在工程中確實用到了該類,採用上面方式還是不行。這個時候就要再增加一項:-keep class com.xx.yy.** { *;},讓當前類不混淆。

小結:
對於引用第三方包的情況,可以採用下面方式避免打包出錯:
-libraryjars /aaa.jar
-dontwarn com.xx.yy.**
-keep class com.xx.yy.** { *;}

最後打包成功,還要在機子上跑跑,看看有沒有問題。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.