Proguard 不会删除 Log 调用
当使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里你的 proguard 文件没问题,但有一个细节你可能会丢失。
在你的 gradle 文件中,你可能有这样的内容:
检查
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:
Check the
getDefaultProguardFile('proguard-android')
, so if you intent to optimize it, you should change it togetDefaultProguardFile('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
如果没有 Proguard 优化,则无法使用
assumenosideeffects
。You cannot use
assumenosideeffects
without Proguard optimization.您可以执行以下操作,而不是使用 pro-guard 删除或注释掉所有日志消息。创建一个实用程序类,它基本上是 Android 日志系统的包装
在编译我的应用程序进行分发之前,我只需调用
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
Before I compile my app for distribution I simply call
showLogs = true;
and then all log messages are supressedYou can easily extend this class to allow you to specify the tag and produce more than debugging messages.