使用ProGuard删除varargs对象数组

时间:2019-04-16 17:38:49

标签: java android logging proguard android-proguard

我正在尝试使用ProGuard从我的应用程序中删除日志。我跟进了这个documentation page,但是很遗憾,我无法删除通过使用varargs创建的Object []。


Java代码

我的自定义记录器示例如下:

public final class MyLogger {

    private static final StringBuilder V_STRING_BUILDER = new StringBuilder(0);

    private MyLogger() {
        //no instance
    }

    public static void v(@NonNull String tag, @NonNull String method, @NonNull Object... message) {
        final String processedMessage;

        synchronized (V_STRING_BUILDER) {
            processedMessage = createMessage(V_STRING_BUILDER, message);
        }

        Log.v(tag.concat(method), processedMessage);
    }

    @NonNull
    private static String createMessage(@NonNull final StringBuilder builder,
                                        @NonNull final Object... messages) {

        builder.setLength(0);

        //noinspection ForLoopReplaceableByForEach
        for (int i = 0, size = messages.length; i < size; i++) {
            builder.append(messages[i]);
        }

        return builder.toString();
    }
}

我记录如下信息:

MyLogger.v(TAG, "myMethod", "The list of parameter");

Proguard文件

-optimizations code/removal/advanced
-optimizationpasses 5
-allowaccessmodification

-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(...);
    public static int wtf(...);
}

-assumenoexternalsideeffects class java.lang.StringBuilder {
    public java.lang.StringBuilder();
    public java.lang.StringBuilder(int);
    public java.lang.StringBuilder(java.lang.String);
    public java.lang.StringBuilder append(java.lang.Object);
    public java.lang.StringBuilder append(java.lang.String);
    public java.lang.StringBuilder append(java.lang.StringBuffer);
    public java.lang.StringBuilder append(char[]);
    public java.lang.StringBuilder append(char[], int, int);
    public java.lang.StringBuilder append(boolean);
    public java.lang.StringBuilder append(char);
    public java.lang.StringBuilder append(int);
    public java.lang.StringBuilder append(long);
    public java.lang.StringBuilder append(float);
    public java.lang.StringBuilder append(double);
    public java.lang.String toString();
}

-assumenoexternalreturnvalues public final class java.lang.StringBuilder {
    public java.lang.StringBuilder append(java.lang.Object);
    public java.lang.StringBuilder append(java.lang.String);
    public java.lang.StringBuilder append(java.lang.StringBuffer);
    public java.lang.StringBuilder append(char[]);
    public java.lang.StringBuilder append(char[], int, int);
    public java.lang.StringBuilder append(boolean);
    public java.lang.StringBuilder append(char);
    public java.lang.StringBuilder append(int);
    public java.lang.StringBuilder append(long);
    public java.lang.StringBuilder append(float);
    public java.lang.StringBuilder append(double);
}

-assumenosideeffects class com.asousa.testing.proguard.MyLogger {
    public static void v(...);
}


最终结果

当我对我的APK进行反向工程时,我可以从示例中检查以下指令是否保留在我的源代码中:

new Object[1][0] = "The list of parameter";

尽管日志未写入设备的logcat中,但对象数组得以保留。

还有什么办法可以告诉ProGuard这些阵列吗?

1 个答案:

答案 0 :(得分:0)

您使用的是MyLogger而不是Log,因此首先需要为MyLogger添加规则。