Proguard 不会删除 Log 调用

发布于 2025-01-06 21:52:19 字数 1691 浏览 0 评论 0原文

当使用 Ant 编译我的应用程序时,我可以看到详细的 Proguard 输出,并且我进行了设置以删除日志语句(见下文),但是当我运行发布 apk 时,我试图删除的所有日志语句都在那里。

我有 2 个项目,每个项目都包含一个公共项目。 2个主项目和公共项目各有一个proguard.cfg文件,所有文件都包含用于删除日志语句的代码片段。

我有什么遗漏的吗?

** 我所有的日志语句都是 Log.d(...)

proguard.cfg

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-dontobfuscate
-forceprocessing
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}

When compiling my app using Ant I can see the verbose Proguard output, and I have things setup to remove the log statements (see below), but when I run the release apk all the log statements I was trying to remove are there.

I have 2 projects each of which include a common project. The 2 main projects and the common project each have a proguard.cfg file, all of which contain the snippet to remove log statements.

Is there something that I am missing?

** All my log statements are Log.d(...)

proguard.cfg

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-dontobfuscate
-forceprocessing
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖乖公主 2025-01-13 21:52:19

这里你的 proguard 文件没问题,但有一个细节你可能会丢失。

在你的 gradle 文件中,你可能有这样的内容:

buildTypes {
    release {
        minifyEnabled true

        proguardFiles getDefaultProguardFile('proguard-android.txt), 'proguard-rules.pro'
    }
}

检查 getDefaultProguardFile('proguard-android'),所以如果你打算优化它,你应该将其更改为 getDefaultProguardFile('proguard- android-optimize.txt')

我花了一些时间才意识到这一点,看到了您的问题,但仍然无法删除代码上的 Log 调用。所以我发现这个链接解释了有必要更改优化文件以使 proguard 能够优化。

https://developer.android.com/tools/help/proguard。 html#enabling-gradle

Here your proguard file is ok, but there is a detail that you may be missing.

In your gradle file you probably have something like this:

buildTypes {
    release {
        minifyEnabled true

        proguardFiles getDefaultProguardFile('proguard-android.txt), 'proguard-rules.pro'
    }
}

Check the getDefaultProguardFile('proguard-android'), so if you intent to optimize it, you should change it to getDefaultProguardFile('proguard-android-optimize.txt').

I spent some time to realize it, saw your question but still not able to remove my Log calls on my code. So I've found this link that explains that is necessary to change to the optimize file to proguard be able to optimize.

https://developer.android.com/tools/help/proguard.html#enabling-gradle

无远思近则忧 2025-01-13 21:52:19

如果没有 Proguard 优化,则无法使用 assumenosideeffects

You cannot use assumenosideeffects without Proguard optimization.

梦里梦着梦中梦 2025-01-13 21:52:19

您可以执行以下操作,而不是使用 pro-guard 删除或注释掉所有日志消息。创建一个实用程序类,它基本上是 Android 日志系统的包装

public class Util{

    public static boolean showLogs = true;
    public static String myTag = 'My Tag';

    public static void logD(String message){
        if (Util.showLogs)      
            Log.d(myTag, message);
    }
}

编译我的应用程序进行分发之前,我只需调用 showLogs = true; 然后所有日志消息都会被

抑制可以轻松地扩展此类,以允许您指定标记并生成除调试消息之外的更多内容。

Instead of deleting or commenting out all of your log messages using pro-guard you could do something like this. Create a utility class that is basically a wrapper around the android logging system

public class Util{

    public static boolean showLogs = true;
    public static String myTag = 'My Tag';

    public static void logD(String message){
        if (Util.showLogs)      
            Log.d(myTag, message);
    }
}

Before I compile my app for distribution I simply call showLogs = true; and then all log messages are supressed

You can easily extend this class to allow you to specify the tag and produce more than debugging messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文