android studio混淆打包:transformClassesAndResourcesWithProguardForRelease'.错误

本文已同步发表到我的微信公众号,扫一扫文章底部的二维码或在微信搜索 “程序员驿站”即可关注,每天都会更新优质技术文章。

在android打包发布的时候,往往需要对app进行压缩,混淆,去除无效文件等,以保证发布出去的app占用资源尽可能的小。因此需要我们对gradle进行必要的配置(以android studio打包为例)。

1.gradle配置

    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 truezipAlignEnabled 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'.错误

签名混淆打包的时候通常会遇到此错误,往往出现此错误的原因是因为在错误之前提示了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.**即可解决,然后在打包,当解决到不在出现警告的时候,自然可以打包成功了。比如以下就是我们期待的了

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意思忽略所有的警告,因此打出的包可能会出现必要代码被混淆导致项目奔溃而无法正常运行,因此个人建议,最好不要加这句代码,遇到什么错误什么警告,对应去解决即可,解决完了自然可以打包成功了。

 

关注我的技术公众号"程序员驿站",每天都有优质技术文章推送,微信扫一扫下方二维码即可关注:



posted @ 2017-06-15 16:02  伯兮  阅读(20470)  评论(0编辑  收藏  举报