当前位置:操作系统 > 安卓/Android >>

Android代码混淆前后分析

为了方便站在巨人臂膀上研读源码,特地将自己写的源码进行混淆之后与源码进行比较。
使用混淆的方法在project.properties文件上加入
[plain 
proguard.config=proguard.cfg
这一条代码。关于如何反编译源码,请看之前的一篇博客如何反编绎APK文件。
一、代码结构
1、源码
 
2、未带混淆机制代码
 
3、混淆后的代码
 
从图中可以看出未带混淆机制的代码基本上与源码的结构相同,同时加入了R文件和android.annotation包。而混淆之后的代码删除了TestCommon文件,因为没有任何其他的文件使用到了这个文件当中的方法,因此使用混淆机制后其被删除。
二、MyTabHost代码分析
1、MyTabHost源码
[java]  
package com.yang.confusion;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TabHost;
public class MyTabHost extends TabHost {
public MyTabHost(Context context) {
super(context);
}
public MyTabHost(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TabSpec setIndicator(CharSequence label) {
return null;
}
private String str = null;
private static final int FLAG = 1;
//测试if
public void testIf() {
if (str == null) {
System.out.println("空");
}
}
/**
* 测试IfElse
*/
public void testIfElse() {
if (str == null) {
System.out.println("空");
} else {
System.out.println("不空");
}
}
public void testIfWhile() {
if (str == null) {
while (str == null) {
System.out.println("空");
}
}
}
public void testIfDoWhile() {
if (str == null) {
do {
System.out.println("空");
} while (str == null);
}
}
public void testFor() {
for (int i = 0; i < 10; i++) {
System.out.println("asdf");
}
}
public void testForIf() {
for (int i = 0; i < 10; i++) {
if (i == 1) {
System.out.println("asdf");
}
}
}
public void testWhile() {
while (str == null) {
System.out.println("空");
}
}
@SuppressWarnings("unused")
private void testSwitch(int key) {
switch (key) {
case FLAG:
break;
default:
break;
}
}
}TestActivity
2、未带混淆机制MyTabHost代码
[java] 
package com.yang.confusion;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import java.io.PrintStream;
public class MyTabHost extends TabHost
{
private static final int FLAG = 1;
private String str = null;
public MyTabHost(Context paramContext)
{
super(paramContext);
}
public MyTabHost(Context paramContext, AttributeSet paramAttributeSet)
{
super(paramContext, paramAttributeSet);
}
private void testSwitch(int paramInt)
{
switch (paramInt)
{
case 1:
}
}
public TabHost.TabSpec setIndicator(CharSequence paramCharSequence)
{
return null;
}
public void testFor()
{
for (int i = 0; ; i++)
{
if (i >= 10)
return;
System.out.println("asdf");
}
}
public void testForIf()
{
for (int i = 0; ; i++)
{
if (i >= 10)
return;
if (i != 1)
continue;
System.out.println("asdf");
}
}
public void testIf()
{
if (this.str == null)
System.out.println("空");
}
public void testIfDoWhile()
{
if (this.str == null)
do
System.out.println("空");
while (this.str == null);
}
public void testIfElse()
{
if (this.str == null)
System.out.println("空");
while (true)
{
return;
System.out.println("不空");
}
}
public void testIfWhile()
{
if (this.str == null);
while (true)
{
if (this.str != null)
return;
System.out.println("空");
}
}
public void testWhile()
{
while (true)
{
if (this.str != null)
return;
System.out.println("空");
}
}
}
3、混淆后的MyTabHost代码
[java] 
package com.yang.confusion;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TabHost;
public class MyTabHost extends TabHost
{
private String a = null;
public MyTabHost(Context paramContext, AttributeSet paramAttributeSet)
{
super(paramContext, paramAttributeSet);
}
}
从中可以看出,只要源码没有进行混淆设置,还是可以读懂的。没有混淆机制的代码,对源码进行相应的改动,具体改动的目的不知道,但是还是可以看懂的。而对于混淆后的代码,对源代码进行了进一步的缩减,对源码当中没有使用到的方法全部删除。【成都安卓培训】
三、TestActivity代码分析
1、TestActivity源码
[java]  
package com.yang.confusion;
import android.app.Activity;
import android.os.Bundle;
public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
testIf();
testIfElse();
testIfWhile();
testIfDoWhile();
testFor();
testForIf();
testWhile();
testSwitch(2);
}
private String str = null;
private static final int FLAG = 1;
//测试if
public void testIf() {
if (str == null) {
System.out.println("空");
}
}
/**
* 测试IfElse
*/
public void testIfElse() {
if (str == null) {
System.out.println("空");
} else {
System.out.println("不空");
}
}
public void testIfWhile() {
if (str == null) {
while (str == null) {
System.out.println("空");
}
}
}
public void testIfDoWhile() {
if (str == null) {
do {
System.out.println("空");
} while (str == null);
}
}
public void testFor() {
for (int i = 0; i < 10; i++) {
System.out.println("asdf");
}
}
public void testForIf() {
for (int i = 0; i < 10; i++) {
if (i == 1) {
System.out.println("asdf");
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,