本文已同步發表到個人微信公衆號,掃一掃文章底部的二維碼或在微信搜索 「程序員驛站」便可關注,天天都會更新優質技術文章。javascript
在android打包發佈的時候,每每須要對app進行壓縮,混淆,去除無效文件等,以保證發佈出去的app佔用資源儘量的小。所以須要咱們對gradle進行必要的配置(以android studio打包爲例)。html
1.gradle配置java
buildTypes { release { minifyEnabled true zipAlignEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.release } debug { signingConfig signingConfigs.debug debuggable true zipAlignEnabled false } }
release版本爲發佈版本,所以設置了minifyEnabled true,zipAlignEnabled true,shrinkResources true。其中proguard-rules.pro是須要咱們本身根據項目編寫的混淆文件。
2.proguard-rules.pro混淆文件編寫(只貼上公共的部分)
# Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in C:\Users\chendx\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt # You can edit the include path and order by changing the proguardFiles # directive in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # Add any project specific keep options here: # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: -keepclassmembers class fqcn.of.javascript.interface.for.webview { public *; } -keep public class android.net.http.SslError -keep public class android.webkit.WebViewClient -dontwarn android.webkit.WebView -dontwarn android.net.http.SslError -dontwarn android.webkit.WebViewClient # Uncomment this to preserve the line number information for # debugging stack traces. -keepattributes SourceFile,LineNumberTable # If you keep the line number information, uncomment this to # hide the original source file name. -renamesourcefileattribute SourceFile -keepattributes Exceptions, Signature, InnerClasses # Keep - Library. Keep all public and protected classes, fields, and methods. -keep public class * { public protected <fields>; public protected <methods>; } # Also keep - Enumerations. Keep the special static methods that are required in # enumeration classes. -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); } # Keep names - Native method names. Keep all native class/method names. -keepclasseswithmembers,allowshrinking class * { native <methods>; } # 不作預校驗 -dontpreverify ### 忽略警告 #-ignorewarning #若是引用了v4或者v7包 -dontwarn android.support.** -keepattributes EnclosingMethod #若是有其它包有warning,在報出warning的包加入下面相似的-dontwarn 報名 -dontwarn com.fengmap.*.** ## 註解支持 -keepclassmembers class *{ void *(android.view.View); } #保護註解 -keepattributes *Annotation*
3.常見transformClassesAndResourcesWithProguardForRelease'.錯誤android
簽名混淆打包的時候一般會遇到此錯誤,每每出現此錯誤的緣由是由於在錯誤以前提示了n多警告和錯誤,所以首先要作的就是把全部的警告和錯誤都解決和消除,而後神奇發現該問題迎刃而解了。如下爲一警告:程序員
Warning:library class org.apache.http.conn.ssl.SSLSocketFactory depends on program class org.apache.http.conn.scheme.HostNameResolver Warning:library class org.apache.http.conn.ssl.SSLSocketFactory depends on program class org.apache.http.params.HttpParams Warning:library class org.apache.http.params.HttpConnectionParams depends on program class org.apache.http.params.HttpParams Warning:there were 15 instances of library classes depending on program classes. Warning:Exception while processing task java.io.IOException: Please correct the above warnings first. Error:Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease'. > Job failed, see logs for details
以上面出現的警告爲例,只須要在proguard-rules.pro添加-dontwarn org.apache.http.**便可解決,而後在打包,當解決到不在出現警告的時候,天然能夠打包成功了。好比如下就是咱們期待的了web
Information:Gradle tasks [:app:assembleRelease] Information:BUILD SUCCESSFUL Information:Total time: 29.157 secs Information:0 errors Information:0 warnings Information:See complete output in console
注:若是你想驗證我所說的是否正確,你能夠在proguard-rules.pro添加-ignorewarning,而後簽名打包便可。由於ignorewarning意思忽略全部的警告,所以打出的包可能會出現必要代碼被混淆致使項目奔潰而沒法正常運行,所以我的建議,最好不要加這句代碼,遇到什麼錯誤什麼警告,對應去解決便可,解決完了天然能夠打包成功了。apache
關注個人技術公衆號"程序員驛站",天天都有優質技術文章推送,微信掃一掃下方二維碼便可關注:微信