本文已同步发表到个人微信公众号,扫一扫文章底部的二维码或在微信搜索 “程序员驿站”便可关注,天天都会更新优质技术文章。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
关注个人技术公众号"程序员驿站",天天都有优质技术文章推送,微信扫一扫下方二维码便可关注:微信