为了方便站在巨人臂膀上研读源码,特地将自己写的源码进行混淆之后与源码进行比较。
使用混淆的方法在project.properties文件上加入
proguard.config=proguard.cfg这一条代码。关于如何反编译源码,请看之前的一篇博客 如何反编绎APK文件。
从图中可以看出未带混淆机制的代码基本上与源码的结构相同,同时加入了R文件和android.annotation包。而混淆之后的代码删除了TestCommon文件,因为没有任何其他的文件使用到了这个文件当中的方法,因此使用混淆机制后其被删除。
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
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("空"); } } }
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); } }从中可以看出,只要源码没有进行混淆设置,还是可以读懂的。没有混淆机制的代码,对源码进行相应的改动,具体改动的目的不知道,但是还是可以看懂的。而对于混淆后的代码,对源代码进行了进一步的缩减,对源码当中没有使用到的方法全部删除。
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"); } } } public void testWhile() { while (str == null) { System.out.println("空"); } } private void testSwitch(int key) { switch (key) { case FLAG: break; default: break; } } }
package com.yang.confusion; import android.app.Activity; import android.os.Bundle; import java.io.PrintStream; public class TestActivity extends Activity { private static final int FLAG = 1; private String str = null; private void testSwitch(int paramInt) { switch (paramInt) { case 1: } } protected void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); testIf(); testIfElse(); testIfWhile(); testIfDoWhile(); testFor(); testForIf(); testWhile(); testSwitch(2); } 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("空"); } } }
package com.yang.confusion; import android.app.Activity; import android.os.Bundle; import java.io.PrintStream; public class TestActivity extends Activity { private String a = null; protected void onCreate(Bundle paramBundle) { int i = 0; super.onCreate(paramBundle); if (this.a == null) System.out.println("空"); label44: int j; if (this.a == null) { System.out.println("空"); if (this.a == null) if (this.a == null) break label106; if (this.a == null) do System.out.println("空"); while (this.a == null); j = 0; label75: if (j < 10) break label117; label81: if (i < 10) break label131; } while (true) { if (this.a != null) { return; System.out.println("不空"); break; label106: System.out.println("空"); break label44; label117: System.out.println("asdf"); j++; break label75; label131: if (i == 1) System.out.println("asdf"); i++; break label81; } System.out.println("空"); } } }
package com.yang.tabhost; import android.app.TabActivity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TabHost; import android.widget.TabWidget; import android.widget.TextView; /** * * @Title: MainTabHostActivity.java * @Package com.yang.tabhost * @Description: MainTabHostActivity * @author Yang * @date 2012-8-13 下午4:35:30 * @version V1.0 */ public class MainTabHostActivity extends TabActivity { private TabHost tabHost; private TabWidget tabWidget; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // No Title bar requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.main_layout); makeTab(); } private void makeTab() { if (this.tabHost == null) { tabHost = getTabHost(); tabWidget = getTabWidget(); tabHost.setup(); tabHost.bringToFront(); setupChild1(); setupChild2(); } for (int i = 0; i < tabWidget.getChildCount(); i++) { TextView tv = (TextView) tabWidget.getChildAt(i).findViewById( android.R.id.title); android.widget.RelativeLayout.LayoutParams tvp = new android.widget.RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); tvp.addRule(RelativeLayout.ALIGN_PARENT_TOP); tvp.addRule(RelativeLayout.CENTER_HORIZONTAL); tv.setLayoutParams(tvp); ImageView iv = (ImageView) tabWidget.getChildAt(i).findViewById( android.R.id.icon); android.widget.RelativeLayout.LayoutParams ivp = new android.widget.RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); ivp.addRule(RelativeLayout.CENTER_HORIZONTAL); ivp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); iv.setLayoutParams(ivp); // 得到每个tab View vvv = tabWidget.getChildAt(i); // 设置背景色为透明 vvv.setBackgroundColor(Color.TRANSPARENT); // 设置字体颜色和大小 tv.setTextColor(this.getResources() .getColorStateList(R.color.white)); tv.setTextSize(15); } } private void setupChild2() { Intent intent = new Intent(); intent.setAction(Intent.ACTION_MAIN); intent.setClass(this, AnotherChildActivity.class); String name = (String) this.getResources().getText(R.string.allimage); tabHost.addTab(tabHost .newTabSpec(name) .setIndicator(name, getResources().getDrawable(R.drawable.tab_selected)) .setContent(intent)); } private void setupChild1() { Intent intent = new Intent(); intent.setAction(Intent.ACTION_MAIN); intent.setClass(this, ChildTabHostActivity.class); String name = (String) this.getResources().getText(R.string.onepiece); tabHost.addTab(tabHost .newTabSpec(name) .setIndicator(name, getResources().getDrawable(R.drawable.tab_selected)) .setContent(intent)); } }
package com.yang.tabhost; import android.app.TabActivity; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.RelativeLayout.LayoutParams; import android.widget.TabHost; import android.widget.TabHost.TabSpec; import android.widget.TabWidget; import android.widget.TextView; public class MainTabHostActivity extends TabActivity { private TabHost tabHost; private TabWidget tabWidget; private void makeTab() { if (this.tabHost == null) { this.tabHost = getTabHost(); this.tabWidget = getTabWidget(); this.tabHost.setup(); this.tabHost.bringToFront(); setupChild1(); setupChild2(); } for (int i = 0; ; i++) { if (i >= this.tabWidget.getChildCount()) return; TextView localTextView = (TextView)this.tabWidget.getChildAt(i).findViewById(16908310); RelativeLayout.LayoutParams localLayoutParams1 = new RelativeLayout.LayoutParams(-2, -2); localLayoutParams1.addRule(10); localLayoutParams1.addRule(14); localTextView.setLayoutParams(localLayoutParams1); ImageView localImageView = (ImageView)this.tabWidget.getChildAt(i).findViewById(16908294); RelativeLayout.LayoutParams localLayoutParams2 = new RelativeLayout.LayoutParams(-2, -2); localLayoutParams2.addRule(14); localLayoutParams2.addRule(12); localImageView.setLayoutParams(localLayoutParams2); this.tabWidget.getChildAt(i).setBackgroundColor(0); localTextView.setTextColor(getResources().getColorStateList(2130968578)); localTextView.setTextSize(15.0F); } } private void setupChild1() { Intent localIntent = new Intent(); localIntent.setAction("android.intent.action.MAIN"); localIntent.setClass(this, ChildTabHostActivity.class); String str = (String)getResources().getText(2131034113); this.tabHost.addTab(this.tabHost.newTabSpec(str).setIndicator(str, getResources().getDrawable(2130837512)).setContent(localIntent)); } private void setupChild2() { Intent localIntent = new Intent(); localIntent.setAction("android.intent.action.MAIN"); localIntent.setClass(this, AnotherChildActivity.class); String str = (String)getResources().getText(2131034114); this.tabHost.addTab(this.tabHost.newTabSpec(str).setIndicator(str, getResources().getDrawable(2130837512)).setContent(localIntent)); } protected void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); requestWindowFeature(1); requestWindowFeature(2); setContentView(2130903041); makeTab(); } }3、混淆后的MainTabHostActivity代码
package com.yang.tabhost; import android.app.TabActivity; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.RelativeLayout.LayoutParams; import android.widget.TabHost; import android.widget.TabHost.TabSpec; import android.widget.TabWidget; import android.widget.TextView; public class MainTabHostActivity extends TabActivity { private TabHost a; private TabWidget b; protected void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); requestWindowFeature(1); requestWindowFeature(2); setContentView(2130903041); if (this.a == null) { this.a = getTabHost(); this.b = getTabWidget(); this.a.setup(); this.a.bringToFront(); Intent localIntent1 = new Intent(); localIntent1.setAction("android.intent.action.MAIN"); localIntent1.setClass(this, ChildTabHostActivity.class); String str1 = (String)getResources().getText(2131034113); this.a.addTab(this.a.newTabSpec(str1).setIndicator(str1, getResources().getDrawable(2130837512)).setContent(localIntent1)); Intent localIntent2 = new Intent(); localIntent2.setAction("android.intent.action.MAIN"); localIntent2.setClass(this, AnotherChildActivity.class); String str2 = (String)getResources().getText(2131034114); this.a.addTab(this.a.newTabSpec(str2).setIndicator(str2, getResources().getDrawable(2130837512)).setContent(localIntent2)); } for (int i = 0; ; i++) { if (i >= this.b.getChildCount()) return; TextView localTextView = (TextView)this.b.getChildAt(i).findViewById(16908310); RelativeLayout.LayoutParams localLayoutParams1 = new RelativeLayout.LayoutParams(-2, -2); localLayoutParams1.addRule(10); localLayoutParams1.addRule(14); localTextView.setLayoutParams(localLayoutParams1); ImageView localImageView = (ImageView)this.b.getChildAt(i).findViewById(16908294); RelativeLayout.LayoutParams localLayoutParams2 = new RelativeLayout.LayoutParams(-2, -2); localLayoutParams2.addRule(14); localLayoutParams2.addRule(12); localImageView.setLayoutParams(localLayoutParams2); this.b.getChildAt(i).setBackgroundColor(0); localTextView.setTextColor(getResources().getColorStateList(2130968578)); localTextView.setTextSize(15.0F); } } }