Android code optimization: Disable log output

Source: Internet
Author: User

Android disabled logs


During development, we often output various logs to debug code. However, when the application publishes an apk, it does not want to output logs.

Disable output logs such as Log. v (), Log. I (), Log. w (), Log. v (), and Log. e ().

Principle:

Then we can use proguard to delete various log output codes. Then, the log code will be filtered out when the apk is exported.

Configure proguard to set the android. util. Log-like method to invalid code. (Proguard is a code optimization tool and can also confuse code)

Assumenosideeffects

Assumenosideeffects, assume no side effects; it is assumed invalid; this attribute is the invalid code of the identity. We use this parameter to allow proguard to delete the log code.

Official explanations of assumenosideeffects:

In the optimization step, ProGuard will then remove callto such methods, if it can determine that the return values aren't used. proGuard will analyze your program code to find such methods automatically. it will not analyze library code, for which this option can therefore be useful.

In general, making assumptions can be dangerous; you can easily break the processed code. Only use this option if you know what you're re doing!

As follows:

-Assumenosideeffects class android. util. Log {
Public static boolean isLoggable (java. lang. String, int );
Public static int v (...);
Public static int I (...);
Public static int w (...);
Public static int d (...);
Public static int e (...);
}

When using this configuration, be sure to configure-dontoptimize.

Don't optimize. Optimization will be disabled, so the log statement will not be optimized. Therefore, this configuration is not available.



How to disable log: My project directory:

1) Open proguard ------- to modify the project. properties file.

Add proguard. config = proguard in the last line of the project. properties file.

For example, my project. properties file:

Target = android-18

Proguard.configpolicproguard-project.txt

2) Configure proguard ------- modify the proguard configuration file,

For example, my profile is: proguard-project.txt

Configuration:

-Keepclassmembers class * extends android. app. Activity {

Public void * (android. view. View );

}


-Keep class * implements android. OS. Parcelable {

Public static final android. OS. Parcelable $ Creator *;

}


-Dontwarn android. support .**


-Keepclassmembers class **. R$ *{

Public static ;

}

-Assumenosideeffects class android. util. Log {

Public static boolean isLoggable (java. lang. String, int );

Public static int v (...);

Public static int I (...);

Public static int w (...);

Public static int d (...);

Public static int e (...);

}


3) export the apk for disabled logs

Proguard, the code is optimized only when the apk is exported, and the optimized apk is generated. (After code obfuscation is completed, the apk is also exported, and proguard obfuscated the code to generate the apk)

Configure the project. properties file and proguard. properties file through the above two steps. Then the project is configured. You can export the signature apk directly. This apk does not output logs. We cannot see the apk log with LogCat.





Test source code 1)

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.e("MainActivity", "log" );}}


Decompile the following code using the generated apk:1-1)

public class MainActivity extends Activity{  protected void onCreate(Bundle paramBundle)  {    super.onCreate(paramBundle);    setContentView(2130903040);  }}

No logs are output during LogCat running.

Obviously Log. e ("MainActivity", "log"); optimized


Source code 2)

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.e("MainActivity", "log " + test());}private String test(){Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();return "jjyy";}}


Decompile the following code using the generated apk:2-1)

Public class MainActivity extends Activity {protected void onCreate (Bundle paramBundle) {super. onCreate (paramBundle); setContentView (2130903040); // the code for the test () function is StringBuilder localStringBuilder = new StringBuilder ("log"); Toast. makeText (this, "test", 0 ). show (); localStringBuilder. append ("jjyy "). toString ();}}



No logs are output during LogCat running. However, toast is displayed.

Obviously, Log. e (); is optimized, but the test () method is retained,


Source code 3 ):

public class MainActivity extends Activity {int i = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.e("MainActivity", "log" + test() );Toast.makeText(this, "i = " + i, Toast.LENGTH_SHORT).show();//i == 1;}private String test(){i++;return "test" + i;}}


Decompile the following code using the generated apk:3-1)

Public class MainActivity extends Activity {private int a = 0; // After proguard obfuscated the code, variable I changed to a protected void onCreate (Bundle paramBundle) {super. onCreate (paramBundle); setContentView (2130903040); // Log. e () code is deleted, but I ++ in the test () function is directly optimized here StringBuilder localStringBuilder = new StringBuilder ("log"); this. a = (1 + this. a); localStringBuilder. append ("test" + this. a ). toString (); Toast. makeText (this, "I =" + this. a, 0 ). show ();}}

No logs are output during LogCat running. However, the toast display string is displayed: "I = 1"

Obviously, Log. e (); is optimized, but the test () method is retained,




For more information, see jiese1990.


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.