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

  整了一天,感受坑挺多。html

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

 

 

 

 

 

 

 

 

 

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

 - Key store path: 若是是新APP則建立,若是已經有了選擇就行;算法

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

 - Key alias(別名): 自定義app

 - 若是是新建立的文件須要選擇時間,整個25年足夠用了,公司信息填上就OKide

 - 下一步後選擇release就開始打包測試

3. Android Studio的混淆功能須要在Gradle中添加: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. 打包時遇到的問題:ui

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 *;
}
相關文章
相關標籤/搜索