Android code optimization: Turn off output log

Source: Internet
Author: User

Android Close 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 through Proguard. Then when you export the APK, the log code will be filtered out.

By configuring Proguard, the method of the class Android.util.Log is set to an invalid code. (Proguard is a code-optimized tool that can also confuse code)

Assumenosideeffects

Assumenosideeffects,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

How to turn off logging:

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;

}

-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.

Test source 1)

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



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

?
12345678 public class mainactivity extends activity {    protected void oncreate (Bundle parambundle)    {      super .oncreate (parambundle);      setcontentview ( 2130903040    } }


There is no output log in run Logcat.

Obviously LOG.E ("mainactivity", "Log");

SOURCE 2)

?
12345678910111213 public class MainActivity extends Activity {    @Override    protected 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 by the generated apk 2-1)

?
123456789101112 public class MainActivity extends Activity{  protected void onCreate(Bundle paramBundle)  {    super.onCreate(paramBundle);    setContentView(2130903040);    //如下是test()函数的代码    StringBuilder localStringBuilder = new StringBuilder("log ");    Toast.makeText(this, "test", 0).show();    localStringBuilder.append("jjyy").toString();  }}



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):

?
123456789101112131415 public class MainActivity extends Activity {    int i = 0;    @Override    protected 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 by the generated apk 3-1)

?
123456789101112131415 public class MainActivity extends Activity{  private int a = 0;    //proguard将代码混淆后变量i变为了a  protected void onCreate(Bundle paramBundle)  {    super.onCreate(paramBundle);    setContentView(2130903040);    //Log.e()代码被删除了,但是调用test()函数里的i++被直接优化到这里    StringBuilder localStringBuilder = new StringBuilder("log");        this.a = (1 + this.a);    localStringBuilder.append("test" + this.a).toString();    Toast.makeText(this, "i = " + this.a, 0).show();  }}

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,

From: http://www.2cto.com/kf/201403/287453.html

Android code optimization: Turn off output log

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.