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意思忽略所有的警告,因此打出的包可能會出現必要代碼被混淆導致項目奔潰而無法正常運行,因此個人建議,最好不要加這句代碼,遇到什么錯誤什么警告,對應去解決即可,解決完了自然可以打包成功了。

 

關注我的技術公眾號"程序員驛站",每天都有優質技術文章推送,微信掃一掃下方二維碼即可關注:


 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM