---
title: "Android代码混淆前后分析"
author: "mmdev"
date: "2013-01-24T16:11:00Z"
tags: []
slug: "android"
draft: false
lastmod: "2013-02-18T12:14:50Z"
id: 1807023
url: "https://mmdev.iteye.com/blog/1807023"
---

为了方便站在巨人臂膀上研读源码，特地将自己写的源码进行混淆之后与源码进行比较。

使用混淆的方法在project.properties文件上加入

\[plain]view plaincopyprint?

1. proguard.config=proguard.cfg

这一条代码。关于如何反编译源码，请看之前的一篇博客如何反编绎APK文件。

# 一、代码结构

## 1、源码

![](http://www.cdtarena.com/Upfiles/Article/2012090814315340996.png)

## 2、未带混淆机制代码

![](http://www.cdtarena.com/Upfiles/Article/2012090814315345283.png)

## 3、混淆后的代码

![](http://www.cdtarena.com/Upfiles/Article/2012090814315353502.png)

从图中可以看出未带混淆机制的代码基本上与源码的结构相同，同时加入了R文件和android.annotation包。而混淆之后的代码删除了TestCommon文件，因为没有任何其他的文件使用到了这个文件当中的方法，因此使用混淆机制后其被删除。

# 二、MyTabHost代码分析

## 1、MyTabHost源码

\[java]view plaincopyprint?

01. packagecom.yang.confusion;
02. importandroid.content.Context;
03. importandroid.util.AttributeSet;
04. importandroid.widget.TabHost;
05. publicclassMyTabHostextendsTabHost {
06. publicMyTabHost(Context context) {
07. super(context);
08. }
09. publicMyTabHost(Context context, AttributeSet attrs) {
10. super(context, attrs);
11. }
12. publicTabSpec setIndicator(CharSequence label) {
13. returnnull;
14. }
15. privateString str =null;
16. privatestaticfinalintFLAG =1;
17. //测试if
18. publicvoidtestIf() {
19. if(str ==null) {
20. System.out.println("空");
21. }
22. }
23. /\**
24. \* 测试IfElse
25. \*/
26. publicvoidtestIfElse() {
27. if(str ==null) {
28. System.out.println("空");
29. }else{
30. System.out.println("不空");
31. }
32. }
33. publicvoidtestIfWhile() {
34. if(str ==null) {
35. while(str ==null) {
36. System.out.println("空");
37. }
38. }
39. }
40. publicvoidtestIfDoWhile() {
41. if(str ==null) {
42. do{
43. System.out.println("空");
44. }while(str ==null);
45. }
46. }
47. publicvoidtestFor() {
48. for(inti =0; i &lt;10; i++) {
49. System.out.println("asdf");
50. }
51. }
52. publicvoidtestForIf() {
53. for(inti =0; i &lt;10; i++) {
54. if(i ==1) {
55. System.out.println("asdf");
56. }
57. }
58. }
59. publicvoidtestWhile() {
60. while(str ==null) {
61. System.out.println("空");
62. }
63. }
64. @SuppressWarnings("unused")
65. privatevoidtestSwitch(intkey) {
66. switch(key) {
67. caseFLAG:
68. break;
69. default:
70. break;
71. }
72. }
73. }TestActivity

## 2、未带混淆机制MyTabHost代码

\[java]view plaincopyprint?

01. packagecom.yang.confusion;
02. importandroid.content.Context;
03. importandroid.util.AttributeSet;
04. importandroid.widget.TabHost;
05. importandroid.widget.TabHost.TabSpec;
06. importjava.io.PrintStream;
07. publicclassMyTabHostextendsTabHost
08. {
09. privatestaticfinalintFLAG =1;
10. privateString str =null;
11. publicMyTabHost(Context paramContext)
12. {
13. super(paramContext);
14. }
15. publicMyTabHost(Context paramContext, AttributeSet paramAttributeSet)
16. {
17. super(paramContext, paramAttributeSet);
18. }
19. privatevoidtestSwitch(intparamInt)
20. {
21. switch(paramInt)
22. {
23. case1:
24. }
25. }
26. publicTabHost.TabSpec setIndicator(CharSequence paramCharSequence)
27. {
28. returnnull;
29. }
30. publicvoidtestFor()
31. {
32. for(inti =0; ; i++)
33. {
34. if(i &gt;=10)
35. return;
36. System.out.println("asdf");
37. }
38. }
39. publicvoidtestForIf()
40. {
41. for(inti =0; ; i++)
42. {
43. if(i &gt;=10)
44. return;
45. if(i !=1)
46. continue;
47. System.out.println("asdf");
48. }
49. }
50. publicvoidtestIf()
51. {
52. if(this.str ==null)
53. System.out.println("空");
54. }
55. publicvoidtestIfDoWhile()
56. {
57. if(this.str ==null)
58. do
59. System.out.println("空");
60. while(this.str ==null);
61. }
62. publicvoidtestIfElse()
63. {
64. if(this.str ==null)
65. System.out.println("空");
66. while(true)
67. {
68. return;
69. System.out.println("不空");
70. }
71. }
72. publicvoidtestIfWhile()
73. {
74. if(this.str ==null);
75. while(true)
76. {
77. if(this.str !=null)
78. return;
79. System.out.println("空");
80. }
81. }
82. publicvoidtestWhile()
83. {
84. while(true)
85. {
86. if(this.str !=null)
87. return;
88. System.out.println("空");
89. }
90. }
91. }

## 3、混淆后的MyTabHost代码

\[java]view plaincopyprint?

01. packagecom.yang.confusion;
02. importandroid.content.Context;
03. importandroid.util.AttributeSet;
04. importandroid.widget.TabHost;
05. publicclassMyTabHostextendsTabHost
06. {
07. privateString a =null;
08. publicMyTabHost(Context paramContext, AttributeSet paramAttributeSet)
09. {
10. super(paramContext, paramAttributeSet);
11. }
12. }

从中可以看出，只要源码没有进行混淆设置，还是可以读懂的。没有混淆机制的代码，对源码进行相应的改动，具体改动的目的不知道，但是还是可以看懂的。而对于混淆后的代码，对源代码进行了进一步的缩减，对源码当中没有使用到的方法全部删除。【[成都安卓培训](http://www.cdtarena.com/)】

# 三、TestActivity代码分析

## 1、TestActivity源码

\[java]view plaincopyprint?

01. packagecom.yang.confusion;
02. importandroid.app.Activity;
03. importandroid.os.Bundle;
04. publicclassTestActivityextendsActivity {
05. @Override
06. protectedvoidonCreate(Bundle savedInstanceState) {
07. super.onCreate(savedInstanceState);
08. testIf();
09. testIfElse();
10. testIfWhile();
11. testIfDoWhile();
12. testFor();
13. testForIf();
14. testWhile();
15. testSwitch(2);
16. }
17. privateString str =null;
18. privatestaticfinalintFLAG =1;
19. //测试if
20. publicvoidtestIf() {
21. if(str ==null) {
22. System.out.println("空");
23. }
24. }
25. /\**
26. \* 测试IfElse
27. \*/
28. publicvoidtestIfElse() {
29. if(str ==null) {
30. System.out.println("空");
31. }else{
32. System.out.println("不空");
33. }
34. }
35. publicvoidtestIfWhile() {
36. if(str ==null) {
37. while(str ==null) {
38. System.out.println("空");
39. }
40. }
41. }
42. publicvoidtestIfDoWhile() {
43. if(str ==null) {
44. do{
45. System.out.println("空");
46. }while(str ==null);
47. }
48. }
49. publicvoidtestFor() {
50. for(inti =0; i &lt;10; i++) {
51. System.out.println("asdf");
52. }
53. }
54. publicvoidtestForIf() {
55. for(inti =0; i &lt;10; i++) {
56. if(i ==1) {
57. System.out.println("asdf");
58. }
59. }
60. }
61. publicvoidtestWhile() {
62. while(str ==null) {
63. System.out.println("空");
64. }
65. }
66. privatevoidtestSwitch(intkey) {
67. switch(key) {
68. caseFLAG:
69. break;
70. default:
71. break;
72. }
73. }
74. }

## 2、未带混淆机制的TestActivity代码

\[java]view plaincopyprint?

01. packagecom.yang.confusion;
02. importandroid.app.Activity;
03. importandroid.os.Bundle;
04. importjava.io.PrintStream;
05. publicclassTestActivityextendsActivity
06. {
07. privatestaticfinalintFLAG =1;
08. privateString str =null;
09. privatevoidtestSwitch(intparamInt)
10. {
11. switch(paramInt)
12. {
13. case1:
14. }
15. }
16. protectedvoidonCreate(Bundle paramBundle)
17. {
18. super.onCreate(paramBundle);
19. testIf();
20. testIfElse();
21. testIfWhile();
22. testIfDoWhile();
23. testFor();
24. testForIf();
25. testWhile();
26. testSwitch(2);
27. }
28. publicvoidtestFor()
29. {
30. for(inti =0; ; i++)
31. {
32. if(i &gt;=10)
33. return;
34. System.out.println("asdf");
35. }
36. }
37. publicvoidtestForIf()
38. {
39. for(inti =0; ; i++)
40. {
41. if(i &gt;=10)
42. return;
43. if(i !=1)
44. continue;
45. System.out.println("asdf");
46. }
47. }
48. publicvoidtestIf()
49. {
50. if(this.str ==null)
51. System.out.println("空");
52. }
53. publicvoidtestIfDoWhile()
54. {
55. if(this.str ==null)
56. do
57. System.out.println("空");
58. while(this.str ==null);
59. }
60. publicvoidtestIfElse()
61. {
62. if(this.str ==null)
63. System.out.println("空");
64. while(true)
65. {
66. return;
67. System.out.println("不空");
68. }
69. }
70. publicvoidtestIfWhile()
71. {
72. if(this.str ==null);
73. while(true)
74. {
75. if(this.str !=null)
76. return;
77. System.out.println("空");
78. }
79. }
80. publicvoidtestWhile()
81. {
82. while(true)
83. {
84. if(this.str !=null)
85. return;
86. System.out.println("空");
87. }
88. }
89. }

## 3、混淆后的TestActivity代码 【[成都安卓培训](http://www.cdtarena.com/)】

\[java]view plaincopyprint?

01. packagecom.yang.confusion;
02. importandroid.app.Activity;
03. importandroid.os.Bundle;
04. importjava.io.PrintStream;
05. publicclassTestActivityextendsActivity
06. {
07. privateString a =null;
08. protectedvoidonCreate(Bundle paramBundle)
09. {
10. inti =0;
11. super.onCreate(paramBundle);
12. if(this.a ==null)
13. System.out.println("空");
14. label44:intj;
15. if(this.a ==null)
16. {
17. System.out.println("空");
18. if(this.a ==null)
19. if(this.a ==null)
20. breaklabel106;
21. if(this.a ==null)
22. do
23. System.out.println("空");
24. while(this.a ==null);
25. j =0;
26. label75:if(j &lt;10)
27. breaklabel117;
28. label81:if(i &lt;10)
29. breaklabel131;
30. }
31. while(true)
32. {
33. if(this.a !=null)
34. {
35. return;
36. System.out.println("不空");
37. break;
38. label106: System.out.println("空");
39. breaklabel44;
40. label117: System.out.println("asdf");
41. j++;
42. breaklabel75;
43. label131:if(i ==1)
44. System.out.println("asdf");
45. i++;
46. breaklabel81;
47. }
48. System.out.println("空");
49. }
50. }
51. }

# 四、MainTabHostActivity代码分析

## 1、MainTabHostActivity源码

\[java]view plaincopyprint?

01. packagecom.yang.tabhost;
02. importandroid.app.TabActivity;
03. importandroid.content.Intent;
04. importandroid.graphics.Color;
05. importandroid.os.Bundle;
06. importandroid.view.View;
07. importandroid.view.Window;
08. importandroid.widget.ImageView;
09. importandroid.widget.RelativeLayout;
10. importandroid.widget.TabHost;
11. importandroid.widget.TabWidget;
12. importandroid.widget.TextView;
13. /\**
14. \*
15. \* @Title: MainTabHostActivity.java
16. \* @Package com.yang.tabhost
17. \* @Description: MainTabHostActivity
18. \*
19. \*/
20. publicclassMainTabHostActivityextendsTabActivity {
21. privateTabHost tabHost;
22. privateTabWidget tabWidget;
23. @Override
24. protectedvoidonCreate(Bundle savedInstanceState) {
25. super.onCreate(savedInstanceState);
26. // No Title bar
27. requestWindowFeature(Window.FEATURE\_NO\_TITLE);
28. requestWindowFeature(Window.FEATURE\_PROGRESS);
29. setContentView(R.layout.main\_layout);
30. makeTab();
31. }
32. privatevoidmakeTab() {
33. if(this.tabHost ==null) {
34. tabHost = getTabHost();
35. tabWidget = getTabWidget();
36. tabHost.setup();
37. tabHost.bringToFront();
38. setupChild1();
39. setupChild2();
40. }
41. for(inti =0; i &lt; tabWidget.getChildCount(); i++) {
42. TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(
43. android.R.id.title);
44. android.widget.RelativeLayout.LayoutParams tvp =newandroid.widget.RelativeLayout.LayoutParams(
45. RelativeLayout.LayoutParams.WRAP\_CONTENT,
46. RelativeLayout.LayoutParams.WRAP\_CONTENT);
47. tvp.addRule(RelativeLayout.ALIGN\_PARENT\_TOP);
48. tvp.addRule(RelativeLayout.CENTER\_HORIZONTAL);
49. tv.setLayoutParams(tvp);
50. ImageView iv = (ImageView) tabWidget.getChildAt(i).findViewById(
51. android.R.id.icon);
52. android.widget.RelativeLayout.LayoutParams ivp =newandroid.widget.RelativeLayout.LayoutParams(
53. RelativeLayout.LayoutParams.WRAP\_CONTENT,
54. RelativeLayout.LayoutParams.WRAP\_CONTENT);
55. ivp.addRule(RelativeLayout.CENTER\_HORIZONTAL);
56. ivp.addRule(RelativeLayout.ALIGN\_PARENT\_BOTTOM);
57. iv.setLayoutParams(ivp);
58. // 得到每个tab
59. View vvv = tabWidget.getChildAt(i);
60. // 设置背景色为透明
61. vvv.setBackgroundColor(Color.TRANSPARENT);
62. // 设置字体颜色和大小
63. tv.setTextColor(this.getResources()
64. .getColorStateList(R.color.white));
65. tv.setTextSize(15);
66. }
67. }
68. privatevoidsetupChild2() {
69. Intent intent =newIntent();
70. intent.setAction(Intent.ACTION\_MAIN);
71. intent.setClass(this, AnotherChildActivity.class);
72. String name = (String)this.getResources().getText(R.string.allimage);
73. tabHost.addTab(tabHost
74. .newTabSpec(name)
75. .setIndicator(name,
76. getResources().getDrawable(R.drawable.tab\_selected))
77. .setContent(intent));
78. }
79. privatevoidsetupChild1() {
80. Intent intent =newIntent();
81. intent.setAction(Intent.ACTION\_MAIN);
82. intent.setClass(this, ChildTabHostActivity.class);
83. String name = (String)this.getResources().getText(R.string.onepiece);
84. tabHost.addTab(tabHost
85. .newTabSpec(name)
86. .setIndicator(name,
87. getResources().getDrawable(R.drawable.tab\_selected))
88. .setContent(intent));
89. }
90. }

## 2、未带混淆机制的MainTabHostActivity代码 【[成都安卓培训](http://www.cdtarena.com/)】

\[java]view plaincopyprint?

01. ,
02. packagecom.yang.tabhost;
03. importandroid.app.TabActivity;
04. importandroid.content.Intent;
05. importandroid.content.res.Resources;
06. importandroid.os.Bundle;
07. importandroid.view.View;
08. importandroid.widget.ImageView;
09. importandroid.widget.RelativeLayout.LayoutParams;
10. importandroid.widget.TabHost;
11. importandroid.widget.TabHost.TabSpec;
12. importandroid.widget.TabWidget;
13. importandroid.widget.TextView;
14. publicclassMainTabHostActivityextendsTabActivity
15. {
16. privateTabHost tabHost;
17. privateTabWidget tabWidget;
18. privatevoidmakeTab()
19. {
20. if(this.tabHost ==null)
21. {
22. this.tabHost = getTabHost();
23. this.tabWidget = getTabWidget();
24. this.tabHost.setup();
25. this.tabHost.bringToFront();
26. setupChild1();
27. setupChild2();
28. }
29. for(inti =0; ; i++)
30. {
31. if(i &gt;=this.tabWidget.getChildCount())
32. return;
33. TextView localTextView = (TextView)this.tabWidget.getChildAt(i).findViewById(16908310);
34. RelativeLayout.LayoutParams localLayoutParams1 =newRelativeLayout.LayoutParams(-2, -2);
35. localLayoutParams1.addRule(10);
36. localLayoutParams1.addRule(14);
37. localTextView.setLayoutParams(localLayoutParams1);
38. ImageView localImageView = (ImageView)this.tabWidget.getChildAt(i).findViewById(16908294);
39. RelativeLayout.LayoutParams localLayoutParams2 =newRelativeLayout.LayoutParams(-2, -2);
40. localLayoutParams2.addRule(14);
41. localLayoutParams2.addRule(12);
42. localImageView.setLayoutParams(localLayoutParams2);
43. this.tabWidget.getChildAt(i).setBackgroundColor(0);
44. localTextView.setTextColor(getResources().getColorStateList(2130968578));
45. localTextView.setTextSize(15.0F);
46. }
47. }
48. privatevoidsetupChild1()
49. {
50. Intent localIntent =newIntent();
51. localIntent.setAction("android.intent.action.MAIN");
52. localIntent.setClass(this, ChildTabHostActivity.class);
53. String str = (String)getResources().getText(2131034113);
54. this.tabHost.addTab(this.tabHost.newTabSpec(str).setIndicator(str, getResources().getDrawable(2130837512)).setContent(localIntent));
55. }
56. privatevoidsetupChild2()
57. {
58. Intent localIntent =newIntent();
59. localIntent.setAction("android.intent.action.MAIN");
60. localIntent.setClass(this, AnotherChildActivity.class);
61. String str = (String)getResources().getText(2131034114);
62. this.tabHost.addTab(this.tabHost.newTabSpec(str).setIndicator(str, getResources().getDrawable(2130837512)).setContent(localIntent));
63. }
64. protectedvoidonCreate(Bundle paramBundle)
65. {
66. super.onCreate(paramBundle);
67. requestWindowFeature(1);
68. requestWindowFeature(2);
69. setContentView(2130903041);
70. makeTab();
71. }
72. }

3、混淆后的MainTabHostActivity代码

\[java]view plaincopyprint?

01. packagecom.yang.tabhost;
02. importandroid.app.TabActivity;
03. importandroid.content.Intent;
04. importandroid.content.res.Resources;
05. importandroid.os.Bundle;
06. importandroid.view.View;
07. importandroid.widget.ImageView;
08. importandroid.widget.RelativeLayout.LayoutParams;
09. importandroid.widget.TabHost;
10. importandroid.widget.TabHost.TabSpec;
11. importandroid.widget.TabWidget;
12. importandroid.widget.TextView;
13. publicclassMainTabHostActivityextendsTabActivity
14. {
15. privateTabHost a;
16. privateTabWidget b;
17. protectedvoidonCreate(Bundle paramBundle)
18. {
19. super.onCreate(paramBundle);
20. requestWindowFeature(1);
21. requestWindowFeature(2);
22. setContentView(2130903041);
23. if(this.a ==null)
24. {
25. this.a = getTabHost();
26. this.b = getTabWidget();
27. this.a.setup();
28. this.a.bringToFront();
29. Intent localIntent1 =newIntent();
30. localIntent1.setAction("android.intent.action.MAIN");
31. localIntent1.setClass(this, ChildTabHostActivity.class);
32. String str1 = (String)getResources().getText(2131034113);
33. this.a.addTab(this.a.newTabSpec(str1).setIndicator(str1, getResources().getDrawable(2130837512)).setContent(localIntent1));
34. Intent localIntent2 =newIntent();
35. localIntent2.setAction("android.intent.action.MAIN");
36. localIntent2.setClass(this, AnotherChildActivity.class);
37. String str2 = (String)getResources().getText(2131034114);
38. this.a.addTab(this.a.newTabSpec(str2).setIndicator(str2, getResources().getDrawable(2130837512)).setContent(localIntent2));
39. }
40. for(inti =0; ; i++)
41. {
42. if(i &gt;=this.b.getChildCount())
43. return;
44. TextView localTextView = (TextView)this.b.getChildAt(i).findViewById(16908310);
45. RelativeLayout.LayoutParams localLayoutParams1 =newRelativeLayout.LayoutParams(-2, -2);
46. localLayoutParams1.addRule(10);
47. localLayoutParams1.addRule(14);
48. localTextView.setLayoutParams(localLayoutParams1);
49. ImageView localImageView = (ImageView)this.b.getChildAt(i).findViewById(16908294);
50. RelativeLayout.LayoutParams localLayoutParams2 =newRelativeLayout.LayoutParams(-2, -2);
51. localLayoutParams2.addRule(14);
52. localLayoutParams2.addRule(12);
53. localImageView.setLayoutParams(localLayoutParams2);
54. this.b.getChildAt(i).setBackgroundColor(0);
55. localTextView.setTextColor(getResources().getColorStateList(2130968578));
56. localTextView.setTextSize(15.0F);
57. }
58. }
59. }

从中可以看出，混淆后的代码难以阅读，但是可以从中找到规律，至于什么规律，博主能力有限，看大家去找了……