release屏蔽Log代码
愿望
在RELEASE包中,代码中的指写的Log.v() Log.d() Log.i() Log.w() Log.e() 都优化删除掉.不打包进软件
原理
通过proguard删除各种日志输出代码。然后编译出apk时,将会删除掉日志代码。
通过配置proguard,将类android.util.Log的方法给置为无效代码。(proguard是一个代码优化的工具,也可以混淆代码)
assumenosideeffects
assumenosideeffects,assume no side effects;假定无效
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!
proguard添加以下配置:
-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不能有。
方法
打开项目build.gradle
proguard-android-optimize.txt 为 sdk>tools>proguard里的默认文件
proguard-android.txt中添加
且配置里不能有-dontoptimize。
打包运行看日志
效果检验
使用AS打开项目的app\build\outputs\apk的文件,解压classes.dex
使用工具dex2jar转成jar文件
再使用jd-gui查看jar的反编译代码,可以看到程序中的Log代码都消失了.
Last updated
Was this helpful?