为了方便站在巨人臂膀上研读源码,特地将自己写的源码进行混淆之后与源码进行比较。
使用混淆的方法在project.properties文件上加入
- proguard.config=proguard.cfg
这一条代码。关于如何反编译源码,请看之前的一篇博客如何反编绎APK文件。
一、代码结构
1、源码
2、未带混淆机制代码
3、混淆后的代码
从图中可以看出未带混淆机制的代码基本上与源码的结构相同,同时加入了R文件和android.annotation包。而混淆之后的代码删除了TestCommon文件,因为没有任何其他的文件使用到了这个文件当中的方法,因此使用混淆机制后其被删除。
二、MyTabHost代码分析
1、MyTabHost源码
- packagecom.yang.confusion;
- importandroid.content.Context;
- importandroid.util.AttributeSet;
- importandroid.widget.TabHost;
- publicclassMyTabHostextendsTabHost {
- publicMyTabHost(Context context) {
- super(context);
- }
- publicMyTabHost(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- publicTabSpec setIndicator(CharSequence label) {
- returnnull;
- }
- privateString str =null;
- privatestaticfinalintFLAG =1;
- //测试if
- publicvoidtestIf() {
- if(str ==null) {
- System.out.println("空");
- }
- }
- /**
- * 测试IfElse
- */
- publicvoidtestIfElse() {
- if(str ==null) {
- System.out.println("空");
- }else{
- System.out.println("不空");
- }
- }
- publicvoidtestIfWhile() {
- if(str ==null) {
- while(str ==null) {
- System.out.println("空");
- }
- }
- }
- publicvoidtestIfDoWhile() {
- if(str ==null) {
- do{
- System.out.println("空");
- }while(str ==null);
- }
- }
- publicvoidtestFor() {
- for(inti =0; i <10; i++) {
- System.out.println("asdf");
- }
- }
- publicvoidtestForIf() {
- for(inti =0; i <10; i++) {
- if(i ==1) {
- System.out.println("asdf");
- }
- }
- }
- publicvoidtestWhile() {
- while(str ==null) {
- System.out.println("空");
- }
- }
- @SuppressWarnings("unused")
- privatevoidtestSwitch(intkey) {
- switch(key) {
- caseFLAG:
- break;
- default:
- break;
- }
- }
- }TestActivity
2、未带混淆机制MyTabHost代码
- packagecom.yang.confusion;
- importandroid.content.Context;
- importandroid.util.AttributeSet;
- importandroid.widget.TabHost;
- importandroid.widget.TabHost.TabSpec;
- importjava.io.PrintStream;
- publicclassMyTabHostextendsTabHost
- {
- privatestaticfinalintFLAG =1;
- privateString str =null;
- publicMyTabHost(Context paramContext)
- {
- super(paramContext);
- }
- publicMyTabHost(Context paramContext, AttributeSet paramAttributeSet)
- {
- super(paramContext, paramAttributeSet);
- }
- privatevoidtestSwitch(intparamInt)
- {
- switch(paramInt)
- {
- case1:
- }
- }
- publicTabHost.TabSpec setIndicator(CharSequence paramCharSequence)
- {
- returnnull;
- }
- publicvoidtestFor()
- {
- for(inti =0; ; i++)
- {
- if(i >=10)
- return;
- System.out.println("asdf");
- }
- }
- publicvoidtestForIf()
- {
- for(inti =0; ; i++)
- {
- if(i >=10)
- return;
- if(i !=1)
- continue;
- System.out.println("asdf");
- }
- }
- publicvoidtestIf()
- {
- if(this.str ==null)
- System.out.println("空");
- }
- publicvoidtestIfDoWhile()
- {
- if(this.str ==null)
- do
- System.out.println("空");
- while(this.str ==null);
- }
- publicvoidtestIfElse()
- {
- if(this.str ==null)
- System.out.println("空");
- while(true)
- {
- return;
- System.out.println("不空");
- }
- }
- publicvoidtestIfWhile()
- {
- if(this.str ==null);
- while(true)
- {
- if(this.str !=null)
- return;
- System.out.println("空");
- }
- }
- publicvoidtestWhile()
- {
- while(true)
- {
- if(this.str !=null)
- return;
- System.out.println("空");
- }
- }
- }
3、混淆后的MyTabHost代码
- packagecom.yang.confusion;
- importandroid.content.Context;
- importandroid.util.AttributeSet;
- importandroid.widget.TabHost;
- publicclassMyTabHostextendsTabHost
- {
- privateString a =null;
- publicMyTabHost(Context paramContext, AttributeSet paramAttributeSet)
- {
- super(paramContext, paramAttributeSet);
- }
- }
三、TestActivity代码分析
1、TestActivity源码
- packagecom.yang.confusion;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- publicclassTestActivityextendsActivity {
- @Override
- protectedvoidonCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- testIf();
- testIfElse();
- testIfWhile();
- testIfDoWhile();
- testFor();
- testForIf();
- testWhile();
- testSwitch(2);
- }
- privateString str =null;
- privatestaticfinalintFLAG =1;
- //测试if
- publicvoidtestIf() {
- if(str ==null) {
- System.out.println("空");
- }
- }
- /**
- * 测试IfElse
- */
- publicvoidtestIfElse() {
- if(str ==null) {
- System.out.println("空");
- }else{
- System.out.println("不空");
- }
- }
- publicvoidtestIfWhile() {
- if(str ==null) {
- while(str ==null) {
- System.out.println("空");
- }
- }
- }
- publicvoidtestIfDoWhile() {
- if(str ==null) {
- do{
- System.out.println("空");
- }while(str ==null);
- }
- }
- publicvoidtestFor() {
- for(inti =0; i <10; i++) {
- System.out.println("asdf");
- }
- }
- publicvoidtestForIf() {
- for(inti =0; i <10; i++) {
- if(i ==1) {
- System.out.println("asdf");
- }
- }
- }
- publicvoidtestWhile() {
- while(str ==null) {
- System.out.println("空");
- }
- }
- privatevoidtestSwitch(intkey) {
- switch(key) {
- caseFLAG:
- break;
- default:
- break;
- }
- }
- }
2、未带混淆机制的TestActivity代码
- packagecom.yang.confusion;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importjava.io.PrintStream;
- publicclassTestActivityextendsActivity
- {
- privatestaticfinalintFLAG =1;
- privateString str =null;
- privatevoidtestSwitch(intparamInt)
- {
- switch(paramInt)
- {
- case1:
- }
- }
- protectedvoidonCreate(Bundle paramBundle)
- {
- super.onCreate(paramBundle);
- testIf();
- testIfElse();
- testIfWhile();
- testIfDoWhile();
- testFor();
- testForIf();
- testWhile();
- testSwitch(2);
- }
- publicvoidtestFor()
- {
- for(inti =0; ; i++)
- {
- if(i >=10)
- return;
- System.out.println("asdf");
- }
- }
- publicvoidtestForIf()
- {
- for(inti =0; ; i++)
- {
- if(i >=10)
- return;
- if(i !=1)
- continue;
- System.out.println("asdf");
- }
- }
- publicvoidtestIf()
- {
- if(this.str ==null)
- System.out.println("空");
- }
- publicvoidtestIfDoWhile()
- {
- if(this.str ==null)
- do
- System.out.println("空");
- while(this.str ==null);
- }
- publicvoidtestIfElse()
- {
- if(this.str ==null)
- System.out.println("空");
- while(true)
- {
- return;
- System.out.println("不空");
- }
- }
- publicvoidtestIfWhile()
- {
- if(this.str ==null);
- while(true)
- {
- if(this.str !=null)
- return;
- System.out.println("空");
- }
- }
- publicvoidtestWhile()
- {
- while(true)
- {
- if(this.str !=null)
- return;
- System.out.println("空");
- }
- }
- }
3、混淆后的TestActivity代码 【成都安卓培训】
- packagecom.yang.confusion;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importjava.io.PrintStream;
- publicclassTestActivityextendsActivity
- {
- privateString a =null;
- protectedvoidonCreate(Bundle paramBundle)
- {
- inti =0;
- super.onCreate(paramBundle);
- if(this.a ==null)
- System.out.println("空");
- label44:intj;
- if(this.a ==null)
- {
- System.out.println("空");
- if(this.a ==null)
- if(this.a ==null)
- breaklabel106;
- if(this.a ==null)
- do
- System.out.println("空");
- while(this.a ==null);
- j =0;
- label75:if(j <10)
- breaklabel117;
- label81:if(i <10)
- breaklabel131;
- }
- while(true)
- {
- if(this.a !=null)
- {
- return;
- System.out.println("不空");
- break;
- label106: System.out.println("空");
- breaklabel44;
- label117: System.out.println("asdf");
- j++;
- breaklabel75;
- label131:if(i ==1)
- System.out.println("asdf");
- i++;
- breaklabel81;
- }
- System.out.println("空");
- }
- }
- }
四、MainTabHostActivity代码分析
1、MainTabHostActivity源码
- packagecom.yang.tabhost;
- importandroid.app.TabActivity;
- importandroid.content.Intent;
- importandroid.graphics.Color;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.Window;
- importandroid.widget.ImageView;
- importandroid.widget.RelativeLayout;
- importandroid.widget.TabHost;
- importandroid.widget.TabWidget;
- importandroid.widget.TextView;
- /**
- *
- * @Title: MainTabHostActivity.java
- * @Package com.yang.tabhost
- * @Description: MainTabHostActivity
- *
- */
- publicclassMainTabHostActivityextendsTabActivity {
- privateTabHost tabHost;
- privateTabWidget tabWidget;
- @Override
- protectedvoidonCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // No Title bar
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- requestWindowFeature(Window.FEATURE_PROGRESS);
- setContentView(R.layout.main_layout);
- makeTab();
- }
- privatevoidmakeTab() {
- if(this.tabHost ==null) {
- tabHost = getTabHost();
- tabWidget = getTabWidget();
- tabHost.setup();
- tabHost.bringToFront();
- setupChild1();
- setupChild2();
- }
- for(inti =0; i < tabWidget.getChildCount(); i++) {
- TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(
- android.R.id.title);
- android.widget.RelativeLayout.LayoutParams tvp =newandroid.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 =newandroid.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);
- }
- }
- privatevoidsetupChild2() {
- Intent intent =newIntent();
- 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));
- }
- privatevoidsetupChild1() {
- Intent intent =newIntent();
- 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));
- }
- }
2、未带混淆机制的MainTabHostActivity代码 【成都安卓培训】
- ,
- packagecom.yang.tabhost;
- importandroid.app.TabActivity;
- importandroid.content.Intent;
- importandroid.content.res.Resources;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.widget.ImageView;
- importandroid.widget.RelativeLayout.LayoutParams;
- importandroid.widget.TabHost;
- importandroid.widget.TabHost.TabSpec;
- importandroid.widget.TabWidget;
- importandroid.widget.TextView;
- publicclassMainTabHostActivityextendsTabActivity
- {
- privateTabHost tabHost;
- privateTabWidget tabWidget;
- privatevoidmakeTab()
- {
- if(this.tabHost ==null)
- {
- this.tabHost = getTabHost();
- this.tabWidget = getTabWidget();
- this.tabHost.setup();
- this.tabHost.bringToFront();
- setupChild1();
- setupChild2();
- }
- for(inti =0; ; i++)
- {
- if(i >=this.tabWidget.getChildCount())
- return;
- TextView localTextView = (TextView)this.tabWidget.getChildAt(i).findViewById(16908310);
- RelativeLayout.LayoutParams localLayoutParams1 =newRelativeLayout.LayoutParams(-2, -2);
- localLayoutParams1.addRule(10);
- localLayoutParams1.addRule(14);
- localTextView.setLayoutParams(localLayoutParams1);
- ImageView localImageView = (ImageView)this.tabWidget.getChildAt(i).findViewById(16908294);
- RelativeLayout.LayoutParams localLayoutParams2 =newRelativeLayout.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);
- }
- }
- privatevoidsetupChild1()
- {
- Intent localIntent =newIntent();
- 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));
- }
- privatevoidsetupChild2()
- {
- Intent localIntent =newIntent();
- 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));
- }
- protectedvoidonCreate(Bundle paramBundle)
- {
- super.onCreate(paramBundle);
- requestWindowFeature(1);
- requestWindowFeature(2);
- setContentView(2130903041);
- makeTab();
- }
- }
- packagecom.yang.tabhost;
- importandroid.app.TabActivity;
- importandroid.content.Intent;
- importandroid.content.res.Resources;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.widget.ImageView;
- importandroid.widget.RelativeLayout.LayoutParams;
- importandroid.widget.TabHost;
- importandroid.widget.TabHost.TabSpec;
- importandroid.widget.TabWidget;
- importandroid.widget.TextView;
- publicclassMainTabHostActivityextendsTabActivity
- {
- privateTabHost a;
- privateTabWidget b;
- protectedvoidonCreate(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 =newIntent();
- 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 =newIntent();
- 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(inti =0; ; i++)
- {
- if(i >=this.b.getChildCount())
- return;
- TextView localTextView = (TextView)this.b.getChildAt(i).findViewById(16908310);
- RelativeLayout.LayoutParams localLayoutParams1 =newRelativeLayout.LayoutParams(-2, -2);
- localLayoutParams1.addRule(10);
- localLayoutParams1.addRule(14);
- localTextView.setLayoutParams(localLayoutParams1);
- ImageView localImageView = (ImageView)this.b.getChildAt(i).findViewById(16908294);
- RelativeLayout.LayoutParams localLayoutParams2 =newRelativeLayout.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);
- }
- }
- }
从中可以看出,混淆后的代码难以阅读,但是可以从中找到规律,至于什么规律,博主能力有限,看大家去找了……