Android4.4中通過mm編譯apk時,apk中jni 一些native方法找不到 ERROR: couldn't find native method

來源:互聯網
上載者:User

在Android4.4下通過mm編譯apk時,出現一些native類找不到的情況,原因是被produard最佳化掉了。在Android4.2中我並沒有遇到這個問題。

1. proguard是做什麼的,ProGuard是一個免費的java類檔案壓縮、最佳化、混淆器。它探測並刪除沒有使用的類、欄位、方法和屬性。它刪除沒有用的說明並使用位元組碼得到最大最佳化。它使用無意義的名字來重新命名類、欄位和方法.。

ProGuard的使用是為了:

(1) 建立緊湊的代碼文檔是為了更快的網路傳輸,快速裝載和更小的記憶體佔用;
(2) 建立的程式和程式庫很難使用反向工程;
(3) 所以它能刪除來自源檔案中的沒有調用的代碼;
(4) 充分利用java6的快速載入的優點來提前檢測和返回java6中存在的類檔案。

2. 因為proguard是混淆工具,所以android的mk檔案也對其支援為兩種方式:
1) 指定不需要混淆的native方法與變數的proguard.flags檔案,在mk檔案下增加:

如:LOCAL_PROGUARD_FLAG_FILES := $(LOCAL_PATH)/proguard.flags

proguard.flags的內容如下:

##---------------Begin: proguard configuration common for all Android apps ----------
-optimizationpasses 5


#混淆時不會產生形形色色的類名
-dontusemixedcaseclassnames

#指定不去忽略非公用的庫類
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose

-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*,!method/removal/*

#最佳化時允許訪問並修改有修飾符的類和類的成員
#-allowaccessmodification

#保留指定的屬性
-keepattributes *Annotation*
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-repackageclasses ''

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

#保護指定類的成員,如果此類受到保護他們會保護的更好
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}

-keepclasseswithmembernames class * {
native ;
}
-keepclasseswithmembers class * {
public (android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public (android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers class **.R$* {
public static ;
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

-keep public class * {
public protected *;
}

-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
##---------------End: proguard configuration common for all Android apps ----------

##---------------Begin: proguard configuration for Gson ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
#保護給定的可選屬性
-keepattributes Signature
# Gson specific classes
-keep class sun.misc.Unsafe { *; }

# Application classes that will be serialized/deserialized over Gson
# -keep public class net.unitepower.mcd.vo.** { *; }
##---------------End: proguard configuration for Gson ----------


2) 制定編譯的工程,不要使用代碼混淆的工具進行代碼混淆
如:LOCAL_PROGUARD_ENABLED := disabled
3) 不設定,預設使用LOCAL_PROGUARD_ENABLED := full.即將該工程代碼全部混淆

聯繫我們

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