Android代码混淆前后分析

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

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

[plain] view plaincopyprint?
  1. proguard.config=proguard.cfg

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

一、代码结构

1、源码

2、未带混淆机制代码


3、混淆后的代码


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

二、MyTabHost代码分析

1、MyTabHost源码

[java] view plaincopyprint?
  1. package com.yang.confusion;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.widget.TabHost;
  5. public class MyTabHost extends TabHost {
  6. public MyTabHost(Context context) {
  7. super(context);
  8. }
  9. public MyTabHost(Context context, AttributeSet attrs) {
  10. super(context, attrs);
  11. }
  12. public TabSpec setIndicator(CharSequence label) {
  13. return null;
  14. }
  15. private String str = null;
  16. private static final int FLAG = 1;
  17. //测试if
  18. public void testIf() {
  19. if (str == null) {
  20. System.out.println("空");
  21. }
  22. }
  23. /**
  24. * 测试IfElse
  25. */
  26. public void testIfElse() {
  27. if (str == null) {
  28. System.out.println("空");
  29. else {
  30. System.out.println("不空");
  31. }
  32. }
  33. public void testIfWhile() {
  34. if (str == null) {
  35. while (str == null) {
  36. System.out.println("空");
  37. }
  38. }
  39. }
  40. public void testIfDoWhile() {
  41. if (str == null) {
  42. do {
  43. System.out.println("空");
  44. while (str == null);
  45. }
  46. }
  47. public void testFor() {
  48. for (int i = 0; i < 10; i++) {
  49. System.out.println("asdf");
  50. }
  51. }
  52. public void testForIf() {
  53. for (int i = 0; i < 10; i++) {
  54. if (i == 1) {
  55. System.out.println("asdf");
  56. }
  57. }
  58. }
  59. public void testWhile() {
  60. while (str == null) {
  61. System.out.println("空");
  62. }
  63. }
  64. @SuppressWarnings("unused")
  65. private void testSwitch(int key) {
  66. switch (key) {
  67. case FLAG:
  68. break;
  69. default:
  70. break;
  71. }
  72. }
  73. }TestActivity

2、未带混淆机制MyTabHost代码

[java] view plaincopyprint?
  1. package com.yang.confusion;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.widget.TabHost;
  5. import android.widget.TabHost.TabSpec;
  6. import java.io.PrintStream;
  7. public class MyTabHost extends TabHost
  8. {
  9. private static final int FLAG = 1;
  10. private String str = null;
  11. public MyTabHost(Context paramContext)
  12. {
  13. super(paramContext);
  14. }
  15. public MyTabHost(Context paramContext, AttributeSet paramAttributeSet)
  16. {
  17. super(paramContext, paramAttributeSet);
  18. }
  19. private void testSwitch(int paramInt)
  20. {
  21. switch (paramInt)
  22. {
  23. case 1:
  24. }
  25. }
  26. public TabHost.TabSpec setIndicator(CharSequence paramCharSequence)
  27. {
  28. return null;
  29. }
  30. public void testFor()
  31. {
  32. for (int i = 0; ; i++)
  33. {
  34. if (i >= 10)
  35. return;
  36. System.out.println("asdf");
  37. }
  38. }
  39. public void testForIf()
  40. {
  41. for (int i = 0; ; i++)
  42. {
  43. if (i >= 10)
  44. return;
  45. if (i != 1)
  46. continue;
  47. System.out.println("asdf");
  48. }
  49. }
  50. public void testIf()
  51. {
  52. if (this.str == null)
  53. System.out.println("空");
  54. }
  55. public void testIfDoWhile()
  56. {
  57. if (this.str == null)
  58. do
  59. System.out.println("空");
  60. while (this.str == null);
  61. }
  62. public void testIfElse()
  63. {
  64. if (this.str == null)
  65. System.out.println("空");
  66. while (true)
  67. {
  68. return;
  69. System.out.println("不空");
  70. }
  71. }
  72. public void testIfWhile()
  73. {
  74. if (this.str == null);
  75. while (true)
  76. {
  77. if (this.str != null)
  78. return;
  79. System.out.println("空");
  80. }
  81. }
  82. public void testWhile()
  83. {
  84. while (true)
  85. {
  86. if (this.str != null)
  87. return;
  88. System.out.println("空");
  89. }
  90. }
  91. }

3、混淆后的MyTabHost代码

[java] view plaincopyprint?
  1. package com.yang.confusion;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.widget.TabHost;
  5. public class MyTabHost extends TabHost
  6. {
  7. private String a = null;
  8. public MyTabHost(Context paramContext, AttributeSet paramAttributeSet)
  9. {
  10. super(paramContext, paramAttributeSet);
  11. }
  12. }
从中可以看出,只要源码没有进行混淆设置,还是可以读懂的。没有混淆机制的代码,对源码进行相应的改动,具体改动的目的不知道,但是还是可以看懂的。而对于混淆后的代码,对源代码进行了进一步的缩减,对源码当中没有使用到的方法全部删除。【成都安卓培训

三、TestActivity代码分析

1、TestActivity源码

[java] view plaincopyprint?
  1. package com.yang.confusion;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. public class TestActivity extends Activity {
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. testIf();
  9. testIfElse();
  10. testIfWhile();
  11. testIfDoWhile();
  12. testFor();
  13. testForIf();
  14. testWhile();
  15. testSwitch(2);
  16. }
  17. private String str = null;
  18. private static final int FLAG = 1;
  19. //测试if
  20. public void testIf() {
  21. if (str == null) {
  22. System.out.println("空");
  23. }
  24. }
  25. /**
  26. * 测试IfElse
  27. */
  28. public void testIfElse() {
  29. if (str == null) {
  30. System.out.println("空");
  31. else {
  32. System.out.println("不空");
  33. }
  34. }
  35. public void testIfWhile() {
  36. if (str == null) {
  37. while (str == null) {
  38. System.out.println("空");
  39. }
  40. }
  41. }
  42. public void testIfDoWhile() {
  43. if (str == null) {
  44. do {
  45. System.out.println("空");
  46. while (str == null);
  47. }
  48. }
  49. public void testFor() {
  50. for (int i = 0; i < 10; i++) {
  51. System.out.println("asdf");
  52. }
  53. }
  54. public void testForIf() {
  55. for (int i = 0; i < 10; i++) {
  56. if (i == 1) {
  57. System.out.println("asdf");
  58. }
  59. }
  60. }
  61. public void testWhile() {
  62. while (str == null) {
  63. System.out.println("空");
  64. }
  65. }
  66. private void testSwitch(int key) {
  67. switch (key) {
  68. case FLAG:
  69. break;
  70. default:
  71. break;
  72. }
  73. }
  74. }

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

[java] view plaincopyprint?
  1. package com.yang.confusion;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import java.io.PrintStream;
  5. public class TestActivity extends Activity
  6. {
  7. private static final int FLAG = 1;
  8. private String str = null;
  9. private void testSwitch(int paramInt)
  10. {
  11. switch (paramInt)
  12. {
  13. case 1:
  14. }
  15. }
  16. protected void onCreate(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. public void testFor()
  29. {
  30. for (int i = 0; ; i++)
  31. {
  32. if (i >= 10)
  33. return;
  34. System.out.println("asdf");
  35. }
  36. }
  37. public void testForIf()
  38. {
  39. for (int i = 0; ; i++)
  40. {
  41. if (i >= 10)
  42. return;
  43. if (i != 1)
  44. continue;
  45. System.out.println("asdf");
  46. }
  47. }
  48. public void testIf()
  49. {
  50. if (this.str == null)
  51. System.out.println("空");
  52. }
  53. public void testIfDoWhile()
  54. {
  55. if (this.str == null)
  56. do
  57. System.out.println("空");
  58. while (this.str == null);
  59. }
  60. public void testIfElse()
  61. {
  62. if (this.str == null)
  63. System.out.println("空");
  64. while (true)
  65. {
  66. return;
  67. System.out.println("不空");
  68. }
  69. }
  70. public void testIfWhile()
  71. {
  72. if (this.str == null);
  73. while (true)
  74. {
  75. if (this.str != null)
  76. return;
  77. System.out.println("空");
  78. }
  79. }
  80. public void testWhile()
  81. {
  82. while (true)
  83. {
  84. if (this.str != null)
  85. return;
  86. System.out.println("空");
  87. }
  88. }
  89. }

3、混淆后的TestActivity代码 【成都安卓培训

[java] view plaincopyprint?
  1. package com.yang.confusion;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import java.io.PrintStream;
  5. public class TestActivity extends Activity
  6. {
  7. private String a = null;
  8. protected void onCreate(Bundle paramBundle)
  9. {
  10. int i = 0;
  11. super.onCreate(paramBundle);
  12. if (this.a == null)
  13. System.out.println("空");
  14. label44: int j;
  15. if (this.a == null)
  16. {
  17. System.out.println("空");
  18. if (this.a == null)
  19. if (this.a == null)
  20. break label106;
  21. if (this.a == null)
  22. do
  23. System.out.println("空");
  24. while (this.a == null);
  25. j = 0;
  26. label75: if (j < 10)
  27. break label117;
  28. label81: if (i < 10)
  29. break label131;
  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. break label44;
  40. label117: System.out.println("asdf");
  41. j++;
  42. break label75;
  43. label131: if (i == 1)
  44. System.out.println("asdf");
  45. i++;
  46. break label81;
  47. }
  48. System.out.println("空");
  49. }
  50. }
  51. }

四、MainTabHostActivity代码分析

1、MainTabHostActivity源码

[java] view plaincopyprint?
  1. package com.yang.tabhost;
  2. import android.app.TabActivity;
  3. import android.content.Intent;
  4. import android.graphics.Color;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.Window;
  8. import android.widget.ImageView;
  9. import android.widget.RelativeLayout;
  10. import android.widget.TabHost;
  11. import android.widget.TabWidget;
  12. import android.widget.TextView;
  13. /**
  14. *
  15. * @Title: MainTabHostActivity.java
  16. * @Package com.yang.tabhost
  17. * @Description: MainTabHostActivity
  18. *
  19. */
  20. public class MainTabHostActivity extends TabActivity {
  21. private TabHost tabHost;
  22. private TabWidget tabWidget;
  23. @Override
  24. protected void onCreate(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. private void makeTab() {
  33. if (this.tabHost == null) {
  34. tabHost = getTabHost();
  35. tabWidget = getTabWidget();
  36. tabHost.setup();
  37. tabHost.bringToFront();
  38. setupChild1();
  39. setupChild2();
  40. }
  41. for (int i = 0; i < tabWidget.getChildCount(); i++) {
  42. TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(
  43. android.R.id.title);
  44. android.widget.RelativeLayout.LayoutParams tvp = new android.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 = new android.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. private void setupChild2() {
  69. Intent intent = new Intent();
  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. private void setupChild1() {
  80. Intent intent = new Intent();
  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代码 【成都安卓培训

[java] view plaincopyprint?
    ,
  1. package com.yang.tabhost;
  2. import android.app.TabActivity;
  3. import android.content.Intent;
  4. import android.content.res.Resources;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.ImageView;
  8. import android.widget.RelativeLayout.LayoutParams;
  9. import android.widget.TabHost;
  10. import android.widget.TabHost.TabSpec;
  11. import android.widget.TabWidget;
  12. import android.widget.TextView;
  13. public class MainTabHostActivity extends TabActivity
  14. {
  15. private TabHost tabHost;
  16. private TabWidget tabWidget;
  17. private void makeTab()
  18. {
  19. if (this.tabHost == null)
  20. {
  21. this.tabHost = getTabHost();
  22. this.tabWidget = getTabWidget();
  23. this.tabHost.setup();
  24. this.tabHost.bringToFront();
  25. setupChild1();
  26. setupChild2();
  27. }
  28. for (int i = 0; ; i++)
  29. {
  30. if (i >= this.tabWidget.getChildCount())
  31. return;
  32. TextView localTextView = (TextView)this.tabWidget.getChildAt(i).findViewById(16908310);
  33. RelativeLayout.LayoutParams localLayoutParams1 = new RelativeLayout.LayoutParams(-2, -2);
  34. localLayoutParams1.addRule(10);
  35. localLayoutParams1.addRule(14);
  36. localTextView.setLayoutParams(localLayoutParams1);
  37. ImageView localImageView = (ImageView)this.tabWidget.getChildAt(i).findViewById(16908294);
  38. RelativeLayout.LayoutParams localLayoutParams2 = new RelativeLayout.LayoutParams(-2, -2);
  39. localLayoutParams2.addRule(14);
  40. localLayoutParams2.addRule(12);
  41. localImageView.setLayoutParams(localLayoutParams2);
  42. this.tabWidget.getChildAt(i).setBackgroundColor(0);
  43. localTextView.setTextColor(getResources().getColorStateList(2130968578));
  44. localTextView.setTextSize(15.0F);
  45. }
  46. }
  47. private void setupChild1()
  48. {
  49. Intent localIntent = new Intent();
  50. localIntent.setAction("android.intent.action.MAIN");
  51. localIntent.setClass(this, ChildTabHostActivity.class);
  52. String str = (String)getResources().getText(2131034113);
  53. this.tabHost.addTab(this.tabHost.newTabSpec(str).setIndicator(str, getResources().getDrawable(2130837512)).setContent(localIntent));
  54. }
  55. private void setupChild2()
  56. {
  57. Intent localIntent = new Intent();
  58. localIntent.setAction("android.intent.action.MAIN");
  59. localIntent.setClass(this, AnotherChildActivity.class);
  60. String str = (String)getResources().getText(2131034114);
  61. this.tabHost.addTab(this.tabHost.newTabSpec(str).setIndicator(str, getResources().getDrawable(2130837512)).setContent(localIntent));
  62. }
  63. protected void onCreate(Bundle paramBundle)
  64. {
  65. super.onCreate(paramBundle);
  66. requestWindowFeature(1);
  67. requestWindowFeature(2);
  68. setContentView(2130903041);
  69. makeTab();
  70. }
  71. }
3、混淆后的MainTabHostActivity代码
[java] view plaincopyprint?
  1. package com.yang.tabhost;
  2. import android.app.TabActivity;
  3. import android.content.Intent;
  4. import android.content.res.Resources;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.ImageView;
  8. import android.widget.RelativeLayout.LayoutParams;
  9. import android.widget.TabHost;
  10. import android.widget.TabHost.TabSpec;
  11. import android.widget.TabWidget;
  12. import android.widget.TextView;
  13. public class MainTabHostActivity extends TabActivity
  14. {
  15. private TabHost a;
  16. private TabWidget b;
  17. protected void onCreate(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 = new Intent();
  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 = new Intent();
  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 (int i = 0; ; i++)
  41. {
  42. if (i >= this.b.getChildCount())
  43. return;
  44. TextView localTextView = (TextView)this.b.getChildAt(i).findViewById(16908310);
  45. RelativeLayout.LayoutParams localLayoutParams1 = new RelativeLayout.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 = new RelativeLayout.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. }

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


原文地址:https://www.cnblogs.com/jackrex/p/3001266.html