使用proguard时出现异常行为

Eld*_*abu 5 java obfuscation android proguard

我的原始代码是:

private String hello;
private int i = 0;

public void test() {
    if (i == 0) {
        hello = "asdas";
    } else {
        hello = "asasvfasfas";
    }
}
Run Code Online (Sandbox Code Playgroud)

用proguard混淆之后:

private String a;
private int c = 0;

public void a()
  {
    if (this.c == 0);
    for (this.a = "asdas"; ; this.a = "asasvfasfas")
      return;
  }
Run Code Online (Sandbox Code Playgroud)

在项目属性中:

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
Run Code Online (Sandbox Code Playgroud)

我的proguard-project.txt文件是空的,所以我猜它应该使用默认的配置文件:proguard-android.txt.

为什么会这样?如何防止这种代码优化?请帮忙.

Kos*_*nae 2

因为您的代码只是您输入的片段,所以我认为您的代码将很容易优化为:

private String hello;

public void test() {
        hello = "asdas";
}
Run Code Online (Sandbox Code Playgroud)

Proguard 只是不会删除原始但无法访问的源代码行,只是将它们放入无法访问的位置。它将您的代码转换为等效但不太人性化的格式。

因此,生成的代码与您的代码一样,只是被混淆了。如果您不喜欢它,请不要使用混淆器。