【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 *;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值