eclipse 代码混淆的那点事

本文详细介绍了如何在Eclipse环境中进行代码混淆,包括配置proguard.cfg文件的具体参数,以及如何保留特定类或方法不被混淆的方法。此外,还提供了如何使用retrace工具解码混淆后的堆栈跟踪信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

eclipse 代码混淆的那点事
首先说说什么是代码混淆:
1.混淆器通过删除从未用过的代码
2.使用晦涩名字重命名类、字段和方法,对代码进行压缩,优化和混淆

具体步骤为:
一.启用并配置混淆器
主要注意一下几点:
(1).由于sdk版本不同导致默认生产的用来混淆代码的文件目录也不相同:
例如项目根目录下面的文件的文件名为:
1. proguard.cfg 和 project.properties 文件时(在较低版本上出现),则只需在 project.properties 文件末尾添加 proguard.config=proguard.cfg 再将项目 Export 即可。
2.proguard-project.txt 和 project.properties 文件,这时需要在 proguard-project.txt 文件中进行如下信息的配置,然后再将项目 Export 即可

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 edit

“ant.properties”, and override values to adapt the script to your

project structure.

#

To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

Project target.

target=android-16
以上的配置信息即是 project.properties 文件中内容,蓝色文字为我们在代码混淆过程中需要添加的配置信息,其中:sdk.dir 为你在当前机器上 SDK 的安装路径。如果想保留某个包下的文件不被混淆,可以在 proguard-project.txt文件中加入保留对应包名的语句即可。

To enable ProGuard in your project, edit project.properties

to define the proguard.config property as described in that file.

#

Add project specific ProGuard rules here.

By default, the flags in this file are appended to flags specified

in ${sdk.dir}/tools/proguard/proguard-android.txt

You can edit the include path and order by changing the ProGuard

include property in project.properties.

#

For more details, see

http://developer.android.com/guide/developing/tools/proguard.html

Add any project specific keep options here:

-dontwarn com.cnki.android.cnkireader.**
-keep class com.cnki.android.cnkireader.* { ; }

If your project uses WebView with JS, uncomment the following

and specify the fully qualified class name to the JavaScript interface

class:

-keepclassmembers class fqcn.of.javascript.interface.for.webview {

public *;

}

(2).混淆文件 proguard.cfg 参数详解
-optimizationpasses 5 # 指定代码的压缩级别
-dontusemixedcaseclassnames # 是否使用大小写混合
-dontskipnonpubliclibraryclasses # 是否混淆第三方jar
-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 ;
}

-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 * { # 保持枚举 enum 类不被混淆
public static **[] values();
public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable { # 保持 Parcelable 不被混淆
public static final android.os.Parcelable$Creator *;
}

-keep class MyClass; # 保持自己定义的类不被混淆

二.使用retrace工具对混淆的堆栈跟踪信息进行解码以及向多了解一下请看下面的文章
http://www.cnblogs.com/over140/archive/2011/04/22/2024528.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值