我们在开发时,经常会输出各种日志来debug代码。但是等到应用发布的apk运行时不希望它输出日志。
关闭输出日志Log.v(),Log.i(),Log.w(),Log.v(),Log.e()等
原理:
那么我们可以通过proguard来删除各种日志输出代码。然后导出apk时,将会过滤掉日志代码。
通过配置proguard,将类android.util.Log的方法给置为为无效代码。(proguard是一个代码优化的工具,也可以混淆代码)
assumenosideeffects
assumenosideeffects,assume no side effects;假定无效;该属性也就是标识无效代码。我们就是通过这个参数来让proguard删除日志代码。
assumenosideeffects的官方解释:
In the optimization step, ProGuard will then remove calls to 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 doing!
如下:
-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(...);
}
使用这个配置时,一定要注意-dontoptimize,配置。
don‘t optimize 不要优化;将会会关闭优化,导致日志语句不会被优化掉。所以不能有这个配置
如何关闭日志:
我的项目目录:
1)打开proguard-------修改project.properties文件。
在project.properties文件最后行添加:proguard.config=proguard
如我的project.properties文件:
target=android-18
proguard.config=proguard-project.txt
2)配置proguard-------修改proguard配置文件,
如:我的配置文件是:proguard-project.txt
配置为:
-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)导出关闭日志的apk
proguard,在导出apk的时候才会优化代码,生成优化后的apk。(完成代码混淆也是在导出apk,proguard将代码混淆后生成apk)
通过如上两个步骤,配置project.properties文件和proguard.properties文件;那么项目就配置好了。可以直接导出签名apk,该apk不会输出日志,我们用LogCat是看不到该apk的日志。
测试
源码1)
1
2
3
4
5
6
7
8
|
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e( "MainActivity" , "log" );
}
} |
通过生成的apk反编译出如下代码1-1)
1
2
3
4
5
6
7
8
|
public class MainActivity extends Activity
{ protected void onCreate(Bundle paramBundle)
{
super .onCreate(paramBundle);
setContentView( 2130903040 );
}
} |
运行LogCat中没有输出日志。
很明显Log.e("MainActivity","log" );被优化掉了
源码2)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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" ;
}
} |
通过生成的apk反编译出如下代码2-1)
1
2
3
4
5
6
7
8
9
10
11
12
|
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();
}
} |
运行LogCat中没有输出日志。但是弹出toast。
很明显Log.e();被优化掉了,但是test()方法依然被保留了,
源码3):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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;
}
} |
通过生成的apk反编译出如下代码3-1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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();
}
} |
运行LogCat中没有输出日志。但是弹出toast 显示字符串 : "i = 1"
很明显Log.e();被优化掉了,但是test()方法依然被保留了,