Android Close Log

Source: Internet
Author: User
Tags log log

When we develop, we often output various logs to debug the code. But wait until the app release apk runs without expecting it to output the log.

Turn off output log log.v (), LOG.I (), LOG.W (), LOG.V (), LOG.E (), etc.

Principle:

Then we can delete various log output codes when we export the APK via Proguard. Then, the log code will be filtered out.

By configuring Proguard, the method of class Android.util.Log is set to be invalid code, and then in the exported apk is deleted log. (Proguard is a code-optimized tool that can also confuse code)

How to turn off the log: My project directory: 1) Open Proguard-------Modify the Project.Properties file.

In the last line of the Project.Properties file, add:proguard.config=proguard

such as my project.properties file:

Target=android-18

proguard.config=proguard-project.txt

2) Configure Proguard-------Modify the Proguard configuration file,

For example: My profile is: proguard-project.txt

Configured to:

-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 <fields>;

}

-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 to close the log

Proguard, when the apk is exported, the code is optimized to generate the optimized apk. (Complete code obfuscation is also generated after exporting Apk,proguard to confuse the code to generate the APK)

Configure the Project.Properties file and the Proguard.properties file with the previous two steps, then the project is configured. Can directly export the signature apk, the APK does not output the log, we use Logcat is not see the APK log.

Assumenosideeffects

Assumenosideeffects,proguard the parameters in the configuration file. Assume no side effects; This property is also an invalid code. This is the parameter that we use to let Proguard delete the log code.

Assumenosideeffects's official explanation:

In the optimization step, Proguard would then remove calls to such methods, if it can determine that the return values aren ' t used. Proguard'll analyze your program code to find such methods automatically. It won't analyze library code, for which this option can therefore is useful.

In general, making assumptions can be dangerous; You can easily break the processed code. Only with this option if you know the What ' 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 note the-dontoptimize, configuration.

Don ' t optimize optimization; The optimizations will be turned off, resulting in the log statements not being optimized. So you can't have this configuration

Test source 1) Verify that you can really optimize the log.

[Java]View Plaincopy
  1. Public class Mainactivity extends Activity {
  2. @Override
  3. protected void OnCreate (Bundle savedinstancestate) {
  4. super.oncreate (savedinstancestate);
  5. Setcontentview (R.layout.activity_main);
  6. LOG.E ("mainactivity", "log");
  7. }
  8. }



Decompile the following code by the generated apk 1-1)

[Java]View Plaincopy
    1. Public class Mainactivity extends Activity
    2. {
    3. protected void OnCreate (Bundle parambundle)
    4. {
    5. super.oncreate (Parambundle);
    6. Setcontentview (2130903040);
    7. }
    8. }


There is no output log in run Logcat.

Obviously LOG.E ("mainactivity","Log"); It's been optimized.

SOURCE 2) LOG.E ("jiese1990", Test ()); Will the test () function be optimized together?

[Java]View Plaincopy
  1. Public class Mainactivity extends Activity {
  2. @Override
  3. protected void OnCreate (Bundle savedinstancestate) {
  4. super.oncreate (savedinstancestate);
  5. Setcontentview (R.layout.activity_main);
  6. LOG.E ("mainactivity", "log" + Test ());
  7. }
  8. Private String Test () {
  9. Toast.maketext (This, "test", Toast.length_short). Show ();
  10. return "Jjyy";
  11. }
  12. }



Decompile the following code by the generated apk 2-1)

[Java]View Plaincopy
  1. Public class Mainactivity extends Activity
  2. {
  3. protected void OnCreate (Bundle parambundle)
  4. {
  5. super.oncreate (Parambundle);
  6. Setcontentview (2130903040);
  7. //The Code for the test () function is as follows
  8. StringBuilder Localstringbuilder = new StringBuilder ("log");
  9. Toast.maketext (This, "test", 0). Show ();
  10. Localstringbuilder.append ("Jjyy"). ToString ();
  11. }
  12. }



There is no output log in run Logcat. But the toast pops up.

Obviously LOG.E (); is optimized, but the test () method is still preserved,

SOURCE 3): LOG.E ("jiese1990", Test ()); Will the test () function be optimized together?

[Java]View Plaincopy
  1. Public class Mainactivity extends Activity {
  2. int i = 0;
  3. @Override
  4. protected void OnCreate (Bundle savedinstancestate) {
  5. super.oncreate (savedinstancestate);
  6. Setcontentview (R.layout.activity_main);
  7. LOG.E ("mainactivity", "log" + Test ());
  8. Toast.maketext (This, "i =" + I, Toast.length_short). Show ();  //i = = 1;
  9. }
  10. Private String Test () {
  11. i++;
  12. return "test" + i;
  13. }
  14. }

Decompile the following code by the generated apk 3-1)

[Java]View Plaincopy
  1. Public class Mainactivity extends Activity
  2. {
  3. private int a = 0; //proguard The code is confused, the variable i changes to a
  4. protected void OnCreate (Bundle parambundle)
  5. {
  6. super.oncreate (Parambundle);
  7. Setcontentview (2130903040);
  8. The //LOG.E () code is removed, but the i++ in the call to the test () function is directly optimized here
  9. StringBuilder Localstringbuilder = new StringBuilder ("log");
  10. THIS.A = (1 + THIS.A);
  11. Localstringbuilder.append ("test" + THIS.A). toString ();
  12. Toast.maketext (This, "i =" + This.a, 0). Show ();
  13. }
  14. }

There is no output log in run Logcat. But the toast display string pops up: "I = 1"

Obviously LOG.E (); is optimized, but the test () method is still preserved,

Android Close Log

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.