android 项目打包以及混淆步骤,注意事项
一 打包签名
在android studio插件栏中找到build,展开后找到下图中黄色图标
点击之后弹出一个窗口
绿色部分是你的证书直接导进来就行,橙色部分是证书密码,白色部分是你的Alias签名,搞定后Next输入你自己设置的as密码,到这里打包配置基本可以了。
配置后在build.gradle中可以看到你的配置信息,下图:
二 代码混淆
代码混淆是个很容易出错的事情,有可能你的有些功能在混淆之前可以用但是混淆之后程序就崩掉了,造成这种情况的原因我分析了一下大致有这么几种情况:
1.你的某些公共类没有被保留
一些公共的方法或者变量在混淆后项目无法识别导致应用炸掉
2.一些常见的组件或者自定义控件被混淆
3.一些枚举,Activity被混淆
4.网络框架,图片处理框架等被混淆
5.一些第三方的sdk的文件被混淆
下面是我项目混淆的代码
———————————基本指令区———————————-
-optimizationpasses 5
-dontskipnonpubliclibraryclassmembers
-printmapping proguardMapping.txt
-optimizations !code/simplification/cast,!field/,!class/merging/
-keepattributes Annotation,InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
-ignorewarnings
—————————————————————————-
———————————默认保留区———————————
-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 * extends android.view.View
-keep public class com.android.vending.licensing.ILicensingService
-keep class android.support.* {;}
-keep public class * extends android.os.IInterface-keep public class * extends android.view.View{
* get*();
void set*(*);
public (android.content.Context);
public (android.content.Context, android.util.AttributeSet);
public (android.content.Context, android.util.AttributeSet, int);
}
-keepclasseswithmembers class * {
public (android.content.Context, android.util.AttributeSet);
public (android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
-keep class *.R$ {
*;
}
-keepclassmembers class * {
void *(**On*Event);
}-keepclasseswithmembernames class * {
native ;
}-keepclasseswithmembers class * {
public (android.content.Context, android.util.AttributeSet);
}-keepclasseswithmembers class * {
public (android.content.Context, android.util.AttributeSet, int);
}-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}———————————-
UIL相关
———————————-
-keep class com.nostra13.universalimageloader.* { ; }
-keepclassmembers class com.nostra13.universalimageloader.* {;}
-dontwarn com.nostra13.universalimageloader.**———————————-
Glide相关
———————————-
-keep class com.bumptech.glide.Glide { *; }
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser** {
**[]VALUES;
public *;
}
-dontwarn com.bumptech.glide.**———————————-
Picasso相关
———————————-
-keep class com.squareup.picasso.Picasso { *; }
-dontwarn com.squareup.okhttp.**
-dontwarn com.squareup.picasso.**———————————-
xUtils3相关
———————————-
-keepattributes Signature,Annotation
-keep public class org.xutils.** {
public protected *;
}
-keep public interface org.xutils.** {
public protected *;
}
-keepclassmembers class * extends org.xutils.** {
public protected *;
}
-keepclassmembers @org.xutils.db.annotation.* class * {*;}
-keepclassmembers @org.xutils.http.annotation.* class * {*;}
-keepclassmembers class * {
@org.xutils.view.annotation.Event ;
}
-dontwarn org.xutils.**retrofit2
-dontwarn retrofit2.**
-keep class retrofit2.* { ; }
-keepattributes Signature
-keepattributes Exceptions-dontwarn org.robovm.**
-keep class org.robovm.* { ; }okhttp3
-dontwarn com.squareup.okhttp3.**
-keep class com.squareup.okhttp3.* { ;}
-keep class okhttp3.* { ;}
-keep class okio.* { ;}
-dontwarn sun.security.**
-keep class sun.security.* { ;}
-dontwarn okio.**
-dontwarn okhttp3.**rxjava
-dontwarn rx.**
-keep class rx.* { ; }-dontwarn sun.misc.**
-keepclassmembers class rx.internal.util.unsafe.ArrayQueue*Field {
long producerIndex;
long consumerIndex;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
rx.internal.util.atomic.LinkedQueueNode producerNode;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
rx.internal.util.atomic.LinkedQueueNode consumerNode;
}包明不混合大小写
-dontusemixedcaseclassnames
混淆时是否记录日志
-verbose
保护注解
-keepattributes Annotation
如果有引用v4包可以添加下面这行
-keep public class * extends android.support.v4.app.Fragment
如果引用了v4或者v7包
-dontwarn android.support.v4
-dontwarn android.support.v7如果用用到Gson解析包的,直接添加下面这几行就能成功混淆,不然会报错。
-keepattributes Signature
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.examples.android.model.* { ; }保持自定义控件类不被混淆
-keepclasseswithmembers class * {
public (android.content.Context, android.util.AttributeSet);
}保持自定义控件类不被混淆
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}JPush混淆
-dontoptimize
-dontpreverify
-dontwarn cn.jpush.**
-keep class cn.jpush.* { ; }
-dontwarn com.google.**
-keep class com.google.gson.* {;}
-keep class com.google.protobuf.* {;}Gson解析的JavaBean类即实体类不被混淆。
-keep class com.matrix.app.entity.json.* { ; }
-keep class com.matrix.appsdk.network.model.* { ; }保持 Parcelable 不被混淆
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}保持 Serializable 不被混淆并且enum 类也不被混淆
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
!static !transient ;
!private ;
!private ;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}-keepclassmembers class * {
public void *ButtonClicked(android.view.View);
}不混淆资源类
-keepclassmembers class *.R$ {
public static ;
}
以上就是安卓项目打包以及混淆的步骤了,如有不足欢迎指正。