【Android】Android Studio 進行代碼混淆,打包release APK


  整了一天,感覺坑挺多。

1. 選擇如圖中的選項Android Studio進行簽名打包:

 

 

 

 

 

 

 

 

 

2. 填寫APP對應的信息:(最好用個文本記下來放在項目中同步給Team)

 - Key store path: 如果是新APP則創建,如果已經有了選擇就行;

 - Key store password: *******

 - Key alias(別名): 自定義

 - 如果是新創建的文件需要選擇時間,整個25年足夠用了,公司信息填上就OK

 - 下一步后選擇release就開始打包

3. Android Studio的混淆功能需要在Gradle中添加:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion '23.0.1'

    defaultConfig {
        applicationId "com.rayclear.juchang"
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 4
        versionName "1.03"
    }
    buildTypes {
        release {
            //代碼混淆開關,一定要注意現在已經使用minifyEnabled代替runProguard了
            minifyEnabled true
            // proguard-rules.pro 是當前使用的混淆文件(Eclipse中的proguard.cfg)
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

4. 打包時遇到的問題:

1.一直處於Gradle Build Running...
被坑了兩次,一直以為是真的在打包,IDE也木有個具體提示什么的,就干等了好久。
遇到這種情況,直接關掉Android Studio重新開始吧。正常來說一個普通的APP打包混淆時長也就是1~3分鍾吧。

2.關於cache.properties.lock的錯:
Error:Timeout waiting to lock cp_proj class cache for build file '/Users/raomengyang/work/app/build.gradle' (/Users/raomengyang/.gradle/caches/2.8/scripts/build_aldsk0ke4ednnd4pf0nybqlhq/cp_proj). It is currently in use by another Gradle instance.
Owner PID: unknown
Our PID: 20080
Owner Operation: unknown
Our operation: Initialize cache
Lock file: /Users/raomengyang/.gradle/caches/2.8/scripts/build_aldsk0ke4ednnd4pf0nybqlhq/cp_proj/cache.properties.lock
之前遇到過同樣的情況,是將帶lock的文件給恢復權限解決的,可是這次不管用,於是退出AS重新來,每次遇到這個問題然后退出AS都會提示后台還有任務沒有結束,所以這個錯誤個人感覺是AS還有后台任務沒有被結束,以至於一直hold在這兒。

3.各種specified twice:
java.io.IOException: The same input jar [/Users/raomengyang/work/app/libs/**] is specified twice.
刪掉proguard文件中的重復聲明引用的jar文件。

4.滿天的Warning: Note: there were 157 duplicate class definitions.
      (http://proguard.sourceforge.net/manual/troubleshooting.html#duplicateclass)
Warning:library class org.apache.http.auth.AuthenticationException extends or implements program class org.apache.http.ProtocolException
Warning:library class org.apache.http.auth.MalformedChallengeException extends or implements program class org.apache.http.ProtocolException
Warning:library class org.apache.http.auth.params.AuthParamBean extends or implements program class org.apache.http.params.HttpAbstractParamBean
Warning:library class org.apache.http.client.NonRepeatableRequestException extends or implements program class org.apache.http.ProtocolException
Warning:library class org.apache.http.client.RedirectException extends or implements program class org.apache.http.ProtocolException
Warning:library class org.apache.http.client.entity.UrlEncodedFormEntity extends or implements program class org.apache.http.entity.StringEntity
Warning:library class org.apache.http.client.methods.HttpEntityEnclosingRequestBase extends or implements program class org.apache.http.HttpEntityEnclosingRequest
Warning:library class org.apache.http.client.methods.HttpRequestBase extends or implements program class org.apache.http.message.AbstractHttpMessage
Warning:library class org.apache.http.client.methods.HttpUriRequest extends or implements program class org.apache.http.HttpRequest
Warning:library class org.apache.http.client.params.AllClientPNames extends or implements program class org.apache.http.params.CoreConnectionPNames
………………………………………………………………………………………………Warning……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………Warning……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………Warning……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………Warning……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………Warning……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………Warning……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………Warning……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
Warning:library class org.apache.http.client.params.AllClientPNames extends or implements program class org.apache.http.params.CoreProtocolPNames
Warning:library class org.apache.http.client.params.ClientParamBean extends or implements program class org.apache.http.params.HttpAbstractParamBean
Warning:library class org.apache.http.client.protocol.RequestAddCookies extends or implements program class org.apache.http.HttpRequestInterceptor
Warning:library class org.apache.http.client.protocol.RequestDefaultHeaders extends or implements program class org.apache.http.HttpRequestInterceptor
Warning:library class org.apache.http.client.protocol.RequestProxyAuthentication extends or implements program class org.apache.http.HttpRequestInterceptor

該類問題都可以根據我們的class文件來進行以下聲明:
例如:現在提示的是org.apache.http.* 這個包里的類有問題,那么就加入下述代碼:
-keep class org.apache.http.** { *; }
-dontwarn org.apache.http.**
依此類推,注意一點,混淆完后,就算打包成功也需要再次測試,因為APP內部可能會出現上述問題。

5.包含JNI時,可能會將JNI的方法給混淆掉,詳細情況參照:http://blog.csdn.net/qinlicang/article/details/6115804

 5.關於proguard.pro文件里面的詳細介紹:

#Author:未知,別的網友總結的,這里拿來給各位參考吧:
#在proguard-rules.pro中加入以下代碼,基本可以涵蓋所有
-optimizationpasses 5          # 指定代碼的壓縮級別
-dontusemixedcaseclassnames   # 是否使用大小寫混合
-dontpreverify           # 混淆時是否做預校驗
-verbose                # 混淆時是否記錄日志

-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*  # 混淆時所采用的算法

-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    # 保持哪些類不被混淆

-keepclasseswithmembernames class * {  # 保持 native 方法不被混淆
    native <methods>;
}
-keepclasseswithmembers class * {   # 保持自定義控件類不被混淆
    public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {# 保持自定義控件類不被混淆
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.app.Activity { # 保持自定義控件類不被混淆   
    public void *(android.view.View);
}
-keepclassmembers enum * {     # 保持枚舉 enum 類不被混淆    
    public static **[] values();    
    public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable { # 保持 Parcelable 不被混淆  
    public static final android.os.Parcelable$Creator *;
}

 


免責聲明!

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



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