简体   繁体   English

即使使用ProGuard(或R8)删除了Log.d()中的语句,仍然可以执行吗?

[英]Are statements in Log.d() still executed even if stripped out with ProGuard (or R8)?

I have the following statement in my proguard-rules.pro to strip out logging in the production apk: 我在proguard-rules.pro有以下语句,以删除生产apk中的日志记录:

-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(...);
}

This has the effect of removing statements like: 这具有删除以下语句的作用:

Log.d(TAG, "setup finished");

Now let's consider the following statement: 现在让我们考虑以下语句:

Log.d(TAG, "setup finished with status " + client.getStatus());

My assumption until now was that this Log.d() statement would be stripped out and therefore that client.getStatus() would never get called. 到目前为止,我的假设是该Log.d()语句将被删除,因此client.getStatus()永远不会被调用。

Specifically, if client happens to be null then my assumption had been that there would be no NullPointerException . 具体来说,如果client恰好为null那么我的假设是不会有NullPointerException

But now I'm not so sure. 但是现在我不太确定。 Perhaps the compiler looks at the above statement more like the following: 也许编译器看上面的语句更像下面这样:

int status = client.getStatus();
Log.d(TAG, "setup finished with status " + status);

Now the client.getStatus() statement is executed and NullPointerException may result. 现在执行了client.getStatus()语句并且可能会导致NullPointerException

What is the actual situation? 实际情况如何?

Note: I'm actually now using R8 rather than ProGuard. 注意:实际上,我现在正在使用R8而不是ProGuard。

OK so this came as a surprise to me, and possibly to others (@Antoniossss), but it seems that statements executed inside a Log.d() are executed despite ProGuard/R8 being configured to remove all Log.d() statements. 好的,这使我和其他人(@Antoniossss)感到惊讶,但是,尽管ProGuard / R8配置为删除所有Log.d()语句,但似乎仍执行了Log.d() 内部执行的语句。

So, even with ProGuard/R8 being configured to remove all Log.d() statements, the following will result in a NullPointerException : 因此,即使将ProGuard / R8配置为删除所有Log.d()语句,以下内容也会导致NullPointerException

String myString = null;
Log.d(TAG, "myString length: " + myString.length());

So, in future I will remember to check these Log statements more carefully when reviewing my code to find the source of a NullPointerException from my Developer Console, and not just pass over them assuming that they cannot have been the cause. 因此,将来,我将记得在检查代码以从开发人员控制台中找到NullPointerException的源时,请更仔细地检查这些Log语句,而不是仅假设它们不可能是原因,而不要跳过它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM