1

I am trying to use Proguard to keep my private fields, but it won't work. I stole most of this from Proguard keep classmembers because that question is similar to what I'm asking, and also followed this link How to tell ProGuard to keep private fields without specifying each field

But it still doesn't work.

I want to make a library for another company and still keep my access level modifiers fields and methods.

Proguard:

-keepclassmembers class com.example.mylibrary.Bedika {
 private <fields>;
}
-keep class com.example.mylibrary.Bedika {
 *;
}

My AAR library

public class Bedika {

private String stam;

public Bedika(String stam) {
 this.stam = stam;
}

public void print() {
 System.out.println(stam);
}
}

output after Proguard:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.example.mylibrary;

public class Bedika {
public String stam;

public Bedika(String var1) {
 this.stam = var1;
}

public void print() {
 System.out.println(this.stam);
}
}
A P
  • 2,131
  • 2
  • 24
  • 36
Zion Aronov
  • 83
  • 1
  • 10

2 Answers2

4

It seems like R8 is causing this issue and not Proguard.

Go into gradle.properties, and set android.enableR8=false. Next time you build, it will use Proguard.

R8 is Google's answer to Proguard and in the recent versions of The Android Gradle Plugin (3.4.0+) it defaults to R8's code shrinker/obfuscator. There are some pros and cons to using Google's version instead of Guardsquare's technology.

For more information, look at the documentation

A P
  • 2,131
  • 2
  • 24
  • 36
0

You can usually add the @Keep annotation above the class or object (in Java or Kotlin). This is pretty self-explanatory, and will work.

A P
  • 2,131
  • 2
  • 24
  • 36
  • I did it, and i still get it public '// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.example.mylibrary; import androidx.annotation.Keep; @Keep public class Bedika { public String stam; public Bedika(String var1) { this.stam = var1; } public void print() { System.out.println(this.stam); } }' – Zion Aronov May 06 '20 at 10:46
  • Are you using consumerProguardFiles ? – A P May 06 '20 at 10:48
  • nope, just pro-guard-rules in the library – Zion Aronov May 06 '20 at 10:49
  • What about: https://stackoverflow.com/questions/30526173/obfuscate-private-fields-using-proguard – A P May 06 '20 at 10:51
  • i need to use the consumerProguardFiles instead of the regular? – Zion Aronov May 06 '20 at 10:53
  • i tried it and it still doesn't work – Zion Aronov May 06 '20 at 11:02