如何在Android应用的发布版本中删除Log.d()调用?
我注意到我的发布版本是通过以下方式在eclipse中制作的:
I have noticed that my release build, which i made in eclipse via:
Android工具->导出已签名的应用程序包...
Android Tools -> Export signed application package ...
我的proguard-project.txt包含:
My proguard-project.txt contains:
-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(...);
}
但是,如果我在设备上运行该应用程序,然后使用CatLog查看日志,就会发现所有Log.d()和Log.v()消息都不应该存在. 还有什么我应该设置的吗? 我花了很多时间在谷歌上搜索,但是没有关于该主题的明确说明.好吧,我发现的大多数人都是有类似问题但没有解决方案的人.
Yet, if I run the app on a device und use CatLog to look at logs I see that there are all the Log.d(), Log.v() messages that shouldn't be there. Is there something else I should set? I have spent many hours googling but there are no clear instructions on that subject. Well most of the things i find are people with similar problems but no solutions.
确保您的proguard配置中没有-dontoptimize
.这是Proguard优化程序,它删除了无副作用"的方法调用.
Make sure your proguard configuration does not have -dontoptimize
in it. It's the proguard optimizer that removes method calls that have "no side effects".
SDK默认proguard-android.txt
中包含-dontoptimize
.请改用proguard-android-optimize.txt
,并且不要在特定于项目的proguard配置中添加-dontoptimize
.
The SDK default proguard-android.txt
has -dontoptimize
in it. Use proguard-android-optimize.txt
instead, and don't add -dontoptimize
in your project-specific proguard configuration.
能否请您说明一些我可以阅读的资料来源.如果存在的话.
Could you please state some source where I could read up on the subject. If it exists of course.
好吧,对于初学者来说,-assumenosideeffects
在文档的优化选项下列出.
Well, for starters -assumenosideeffects
is listed under optimization options in the documentation.
还有一件令人烦恼的事情,我读到使用proguard-android-optimize.txt可能会在某些设备上引起问题.因此,这是一个22类情况.
One more thing bothers is that, I have read that using proguard-android-optimize.txt can cause problems on some devices. So, it is a sort od catch 22 situation.
Proguard通常会产生副作用.您需要彻底测试由Proguard处理的应用程序.
Proguard in general can have side effects. You need to test your proguard-processed application thoroughly.
如果您想完全不使用proguard从发行版本中删除日志记录,则可以例如添加
If you want to remove logging from release builds without using proguard at all, you can e.g. add
if (BuildConfig.DEBUG)
在每个日志记录调用之前
,其中BuildConfig
是Gradle Android插件生成的构建配置.由于DEBUG
有一个编译时常量,因此Java编译器认为该条件在发布配置中永远不可能为真,并且不会在条件块内发出字节码.
before every logging call where BuildConfig
is the build configuration generated by the Gradle Android plugin. Since the DEBUG
there is a compile-time constant, the Java compiler sees the condition can never be true on a release configuration and won't emit the bytecode inside the conditional block.