我们在开发时,经常会输出各种日志来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()方法依然被保留了,

Eclipse 打包过滤 Log.e的更多相关文章

  1. eclipse打包apk

    eclipse打包apk

  2. tar打包过滤某个文件及文件夹

    ip=ip add|grep eth0|grep -i inet|awk '{print $2}'|cut -d '/' -f 1 cd /data tar -zvcf `echo $ip`_`dat ...

  3. Eclipse打包出错——提示GC overhead limit exceeded

    版权声明:本文为博主原创文章,未经博主允许不得转载. 在Eclipse开发环境中打包发布apk安装包的时候,有时候会出现下面的错误: 原因 在打包的时候,Eclipse占用的内存会增大,当分配给Ecl ...

  4. Eclipse打包Egret App (Egret4.1.0)

    Egret官方提供eclipse和androidstudio打包. 这里使用eclipse. 1 下载配置android环境 2 Egret打包App 3 Eclipse设置 4 Eclipse调试 ...

  5. eclipse启动报错:An error has occurred.See the log file D:\eclipse\configuration\1552616709202.log

    如题,Eclipse崩了,只能按它留下的线索去看了1552616709202.log: !SESSION -- ::08.739 ----------------------------------- ...

  6. Eclipse打包java工程

    Eclipse打包java工程步骤如下: 1.选择预打包的工程->Export. 2.选择java->JAR file. 3.导出JAR文件设置. 这里有几个选项: Export gene ...

  7. eclipse打包插件net.sf.fjep.fatjar

    eclipse打包插件安装 1)将net.sf.fjep.fatjar_0.0.32.jar拷贝到eclipse安装目录中的plugins目录下,然后重启eclipse即可. 软件获取方式: 链接:h ...

  8. vue webpack build 打包过滤console.log()日志

    vue cli创建项目在 webpack.prod.conf.js文件 //打包时清除页面中所有打印及debugger断点 new webpack.optimize.UglifyJsPlugin({ ...

  9. webpack打包过滤console.log

    在webpack.prod.conf.js里面的plugins里面加上 drop_debugger: true, drop_console: true new webpack.optimize.Ugl ...

随机推荐

  1. css 实现评分效果

    css实现评分效果,其实是css sprites (css精灵)的延伸应用,效果的实现主要是由  background-position 属性移动图片位置.之前看到有前辈写过关于这方面的内容,在理解上 ...

  2. 2016年gift上线相关知识点记录

    1.图片初始化加载 2.如何判断横屏 function horAver() { if (window.orientation == 90 || window.orientation == -90) { ...

  3. asp.net页面压缩

    http压缩方法(IIS 6.0 与IIS 7.0的详解)   在网上看了有关这方面的博客,再加上自己的实践,整理了一下,希望对大家有所帮助 本片文章采用两种压缩方法:一种是在IIS上开启GZIP压缩 ...

  4. 如何调教Android Studio-Windows安装AS后的必备工作

    未完待续... 工欲善其事必先利其器,你已经抛弃被大众诟病的Eclipse投入Google亲儿子Android Studio的怀抱,可是不了解As的脾气,怎么让它服服帖帖的为提高开发效率做贡献呢. 关 ...

  5. struts2.3.15.3中动态方法调用默认是关闭的

    初学ssh,用的struts2.3.15.3,使用了如下表单: <form action="/spring3/index/login.action" method=" ...

  6. DEV GridControl导出到Excel或打印

    //方法1SaveFileDialog fileDialog = new SaveFileDialog(); fileDialog.Title = "导出Excel"; fileD ...

  7. HDU 1021 - Fibonacci Again

    找规律,分析让 F[N] 每一项对 3 取余的余数: 1,2,0, 2,2,1,0, 1,1,2,0, 2,2,1,0, 1,1,2,0, 2,2,1,0 ......... 显然循环了 #inclu ...

  8. WIFI无线adb调试android

    有个需求需要支持android插上键盘,鼠标等外设,但是这样就不能使用microusb口进行adb调试了. 研究了一番,发现可以利用wifi进行adb无线调试(adb应该本身已经支持无线调试). WI ...

  9. 汽车总线obd模拟器,obd仿真器,ecu模拟器,obd开发测试工具,可以模拟ecu中的obd协议 MRD-5050

    汽车总线OBD模拟器MRD-5050型号是在车辆越来越趋于网络化的趋势下研发的,是汽车产品开发.调试.生产必备的工具,能为为开发人员节省大量的时间.当前车辆上的总线设备越来越多,有的高端车上甚至多到有 ...

  10. 旧的VirtualBox News(从1.3.4开始)

    https://linuxtoy.org/archives.html https://linuxtoy.org/archives/virtualbox-134.html http://www.cnbl ...