为程序增加代码混淆

概述

在2.3版本的sdk中可以看到在ANDROID_SDK_PATH/tools/下面多了一个proguard文件夹,google把proguard技术放在了android sdk里面,现在已经可以通过正常的编译方式实现代码混淆了。

在2.3版本以后,无论开发用的是哪一个版本的SDK,新建一个工程里面都会有default.properties和proguard.cfg两个文件。

默认的default.properties代码如下:

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system use,
# "build.properties", and override values to adapt the script to your
# project structure.
# Project target.
target=android-9

默认的proguard.cfg中也已经有了混淆代码的脚本:

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-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 com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
    native <methods>;
}
-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

从脚本中可以看到:

  • 混淆中保留了继承自Activity、Service、Application、BroadcastReceiver、 ContentProvider等基本组件以及com.android.vending.licensing.ILicensingService。
  • 保留了所有的Native变量名及类名,所有类中部分已设定了固定参数格式的构造函数,枚举等等。

详细信息参考<proguard_path>/examples中的例子及注释。

使用代码混淆

依据官方文档所说:

To enable ProGuard so that it runs as part of an Ant or Eclipse build, set the proguard.config property in the <project_root>/default.properties file. The path can be an absolute path or a path relative to the project's root.

修改default.properties文件,加上一句:

proguard.config=proguard.cfg

加完后如下面的样子:

proguard.config=proguard.cfg  
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system use,
# "build.properties", and override values to adapt the script to your
# project structure.
# Project target.
target=android-9
proguard.config=proguard.cfg

然后正常的编译签名即可生成一个经过代码混淆的apk。

转载于:https://www.cnblogs.com/santry/archive/2011/10/25/2224547.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值