Before and after obfuscation of Android code

Source: Internet
Author: User

In order to facilitate the study of the source code on the arm of giants, I specially confuse the source code and compare it with the source code. Use the obfuscation method to add the [plain proguard. config = proguard. cfg] code to the project. properties file. For how to decompile the source code, see how to decompile the ghost APK file in a previous blog. I. code structure 1. Source Code 2. Code without obfuscation mechanism 3. the obfuscation Code shows that the code without obfuscation mechanism is basically the same as the source code structure, added the R file and android. annotation package. The obfuscated code deletes the TestCommon file, because no other file uses the method in this file, so it is deleted after the obfuscation mechanism is used. Ii. MyTabHost code analysis 1. MyTabHost source code [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; // test ifpublic void testIf () {if (str = null) {System. out. println ("null") ;}}/*** test IfElse */public void testIfElse () {if (str = null) {System. out. println ("null");} else {System. out. println ("not empty") ;}} public void testIfWhile () {if (str = null) {while (str = null) {System. out. println ("null") ;}} public void testIfDoWhile () {if (str = null) {do {System. out. println (" Null ") ;}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 ("null") ;}@ SuppressWarnings ("unused") private void testSwitch (int key) {switch (key) {case FLAG: break; default: break ;}} TestActivity 2. MyTabHost code [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 para MContext, 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 ("null");} public void testIfDoWhile () {if (this. str = null) doSystem. out. println ("null"); while (this. str = null);} public void testIfElse () {if (this. str = null) System. out. println ("null"); while (true) {return; System. out. println ("not empty") ;}} public void testIfWhile () {if (this. str = null); while (true) {if (this. str! = Null) return; System. out. println ("null") ;}} public void testWhile () {while (true) {if (this. str! = Null) return; System. out. println ("null") ;}}3. obfuscated MyTabHost code [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) ;}} can be seen from this, as long as the source code is not obfuscated, you can still understand it. Code without obfuscation mechanism, and the source code is modified accordingly. The purpose of the specific change is unknown, but it can still be understood. For the obfuscated code, the source code is further reduced, and all unused methods in the source code are deleted. [Chengdu Android training] III. TestActivity code analysis 1. TestActivity source code [java] package com. yang. confusion; import android. app. activity; import android. OS. bundle; public class TestActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); testIf (); testIfElse (); testIfWhile (); testIfDoWhile (); testFor (); testForIf (); testWhile (); testSwitch (2 );} private String str = null; priva Te static final int FLAG = 1; // test ifpublic void testIf () {if (str = null) {System. out. println ("null") ;}}/*** test IfElse */public void testIfElse () {if (str = null) {System. out. println ("null");} else {System. out. println ("not empty") ;}} public void testIfWhile () {if (str = null) {while (str = null) {System. out. println ("null") ;}} public void testIfDoWhile () {if (str = null) {do {System. out. println ("null");} 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 ("null") ;}} private void testSwitch (int key) {switch (key) {case FLAG: break; default: break ;}}} 2. TestActivity code [java] package com. Without obfuscation Mechanism. 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 (); testW Hile (); 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 ("null");} public void testIfDoWhile () {if (this. str = null) doSystem. out. println ("null"); while (this. str = null);} public void testIfElse () {if (this. str = null) System. out. println ("null"); while (true) {return; System. out. println ("not empty") ;}} public void testIfWhile () {if (this. str = null); while (true) {if (this. str! = Null) return; System. out. println ("null") ;}} public void testWhile () {while (true) {if (this. str! = Null) return; System. out. println ("null") ;}}3. obfuscated TestActivity code [Chengdu Android training] [java] 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 ("null"); label44: int j; if (this. a = Null) {System. out. println ("null"); if (this. a = null) if (this. a = null) break label106; if (this. a = null) doSystem. out. println ("null"); 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 ("not empty"); break; label106: System. out. println ("null"); break label44; label117: System. out. println ("asdf"); j ++; break label75; label131: if (I = 1) System. out. println ("asdf"); I ++; break label81;} System. out. println ("null") ;}}4. MainTabHostActivity code analysis 1. MainTabHostActivity source code [java] package com. yang. tabhost; import android. app. tabActivity; import android. content. intent; import android. gr Aphics. 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 **/public class MainTabHostActivity extends TabA Ctivity {private TabHost tabHost; private TabWidget tabWidget; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // No Title barrequestWindowFeature (Window. FEATURE_NO_TITLE); requestWindowFeature (Window. FEATURE_PROGRESS); setContentView (R. layout. main_layout); makeTab ();} private void makeTab () {if (this. tabHost = null) {tabHost = getTabHost (); tabWidget = ge TTabWidget (); 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. a DdRule (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. C ENTER_HORIZONTAL); ivp. addRule (RelativeLayout. ALIGN_PARENT_BOTTOM); iv. setLayoutParams (ivp); // obtain each tabView vvv = tabWidget. getChildAt (I); // set the background color to transparent vvv. setBackgroundColor (Color. TRANSPARENT); // set the font color and size of 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 = (Stri Ng) this. getResources (). getText (R. string. onepiece); tabHost. addTab (tabHost. newTabSpec (name ). setIndicator (name, getResources (). getDrawable (R. drawable. tab_selected )). setContent (intent);} 2. MainTabHostActivity code without obfuscation mechanism [Chengdu Android training] [java], package com. yang. tabhost; import android. app. tabActivity; import android. content. intent; import android. content. res. resources; import android. OS. bundle; import android. view. V Iew; 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 (); t His. 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 (1, 16908310); RelativeLayout. layoutParams localLayoutParams1 = new RelativeLayout. layoutParams (-2,-2); localLayoutParams1.addRule (10); localLayoutPar Ams1.addRule (14); localTextView. setLayoutParams (localLayoutParams1); ImageView localImageView = (ImageView) this. tabWidget. getChildAt (I ). findViewById (1, 16908294); RelativeLayout. layoutParams localLayoutParams2 = new RelativeLayout. layoutParams (-2,-2); localLayoutParams2.addRule (14); localLayoutParams2.addRule (12); localImageView. setLayoutParams (localLayoutParams2); this. tabWidget. getChildAt (I ). setBackgroun DColor (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 ). setIndica Tor (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 (1, 2130837 512 )). setContent (localIntent);} protected void onCreate (Bundle paramBundle) {super. onCreate (paramBundle); requestWindowFeature (1); requestWindowFeature (2); setContentView (2130903041); makeTab () ;}3. obfuscated MainTabHostActivity code [java] 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; im Port 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); re QuestWindowFeature (2); setcontentviews (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 ). setIndica Tor (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 (1, 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 (1, 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) ;}} shows that the mixed code is hard to read, We can find the rule from it. As for what rule, the ability of the blogger is limited. Let's see what we are looking ......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.