Status Update
Comments
ze...@google.com <ze...@google.com>
lu...@gmail.com <lu...@gmail.com> #2
Please check the attachment, it's a demo which can reproduce the problem
ze...@google.com <ze...@google.com> #3
Thanks for reaching out. The line of code:
Class[] interfaces = testB.getClass().getInterfaces();
is reflectively accessing the meta-info of the class structures, in this case its interfaces. For any use of reflection you need to add keep rules for the shrinker (here R8) to work. You will want to add the rules to the proguard-rules.pro
file in your app.
I'm not sure of your exact use case from the example. For simply ensuring that the interface remains you should be able to use the rule:
-keep class ITest
Now if your use case is that every class implementing the interface also needs its hierarchy kept you could additionally add:
-if class * extends ITest -keep class <1>
You can find more info on using a shrinker at:
Do any of the above suggestions work for your use?
lu...@gmail.com <lu...@gmail.com> #4
Sorry, it didn't work for me. I have upload my sample code, please check the attachment.
ch...@google.com <ch...@google.com> #5
Did you make sure to add the package to the above rules (something like -keep class com.example.ITest
)?
ze...@google.com <ze...@google.com> #6
Thanks. Having a look at your demo now.
ze...@google.com <ze...@google.com> #7
The keep rules are working as intended. This issue is that getInterfaces()
public class Test {
interface I {}
static class A implements I {}
static class B extends A {}
static class C extends A implements I {}
public static void main(String[] args) {
System.out.println("Java " + System.getProperty("java.version"));
for (Class<?> c : Arrays.asList(A.class, B.class, C.class)) {
System.out.println(c.getSimpleName());
for (Class<?> iface : c.getInterfaces()) {
System.out.println(" - " + iface.getSimpleName());
}
}
}
}
results in the output:
Java 11
A
- I
B
C
- I
As seen here, the result for class B
is empty.
You will need to manually implement the search in the extends hierarchy to obtain the result of 1
for B
.
ze...@google.com <ze...@google.com> #8
I hope the above helps. If not let me know. Closing this as WAI for now.
lu...@gmail.com <lu...@gmail.com> #9
I know that getInterfaces() returns the directly inherited interface. But this is also done in my demo. TestB inherits TestA and implements the ITest interface. I still have the same problem. Have you executed the demo?
ze...@google.com <ze...@google.com> #10
I'm sorry. You are right. I missed that the code was both extends and implements in your example, thus akin to the C
case in my example above.
(My run of the debug variant in studio had also cached the result so it looked like the D8 version was consistent with R8, but it is not).
This looks like a bug in part of R8's unneeded interface removal optimization.
ap...@google.com <ap...@google.com> #11
Branch: main
commit 61a7edcb0236dbedec2346fe4473d05ce2ab61b9
Author: Ian Zerny <zerny@google.com>
Date: Mon Jan 08 11:12:41 2024
Keep redundant interfaces if class and interface are kept.
Bug:
Change-Id: I417d3a914246dc066ff36b96ca64a77a17e0804e
M src/main/java/com/android/tools/r8/shaking/TreePruner.java
A src/test/java/com/android/tools/r8/ir/optimize/unusedinterfaces/UnusedButKeptInterfaceTest.java
ap...@google.com <ap...@google.com> #12
Branch: 8.0
commit d83b5b4f5ea5d90958efeda43a0cdda9f5b34584
Author: Ian Zerny <zerny@google.com>
Date: Tue Jan 09 08:47:40 2024
Keep redundant interfaces if class and interface are kept.
Bug:
Change-Id: I417d3a914246dc066ff36b96ca64a77a17e0804e
M src/main/java/com/android/tools/r8/shaking/TreePruner.java
A src/test/java/com/android/tools/r8/ir/optimize/unusedinterfaces/UnusedButKeptInterfaceTest.java
ze...@google.com <ze...@google.com> #13
ap...@google.com <ap...@google.com> #14
Branch: 8.3
commit 9a4bf8dd55f61af828baf50ee75fefa172989c8c
Author: Ian Zerny <zerny@google.com>
Date: Tue Jan 09 09:14:29 2024
Version 8.3.29
Bug:
Change-Id: I2b7beb19262db0b5f38d3532c50a2a35938fad1d
M src/main/java/com/android/tools/r8/Version.java
ap...@google.com <ap...@google.com> #15
Branch: 8.3
commit 49c39e588a830e54d791d33a9f2a7986cca163b9
Author: Ian Zerny <zerny@google.com>
Date: Tue Jan 09 09:14:07 2024
Keep redundant interfaces if class and interface are kept.
Bug:
Change-Id: I417d3a914246dc066ff36b96ca64a77a17e0804e
M src/main/java/com/android/tools/r8/shaking/TreePruner.java
A src/test/java/com/android/tools/r8/ir/optimize/unusedinterfaces/UnusedButKeptInterfaceTest.java
lu...@gmail.com <lu...@gmail.com> #16
Thank you very much, it works for me now. But 4.x version also has similar problems, do you plan to fix it?
ap...@google.com <ap...@google.com> #17
Branch: 8.1
commit 96e406ce0974730ba057c752e3f33281acb0af95
Author: Ian Zerny <zerny@google.com>
Date: Tue Jan 09 09:10:23 2024
Version 8.1.77
Bug:
Change-Id: Ic760005df6e6e41c2157486fdaa0c1408cef765a
M src/main/java/com/android/tools/r8/Version.java
ap...@google.com <ap...@google.com> #18
Branch: 8.1
commit 7b1a6e6695047ed4a2eaf6443e4736aa3ce7a885
Author: Ian Zerny <zerny@google.com>
Date: Tue Jan 09 09:08:21 2024
Keep redundant interfaces if class and interface are kept.
Bug:
Change-Id: I417d3a914246dc066ff36b96ca64a77a17e0804e
M src/main/java/com/android/tools/r8/shaking/TreePruner.java
A src/test/java/com/android/tools/r8/ir/optimize/unusedinterfaces/UnusedButKeptInterfaceTest.java
ze...@google.com <ze...@google.com> #19
We don't expect to pull anymore updates to the 4.0 branch. Is it possible to use 8.0 or later?
lu...@gmail.com <lu...@gmail.com> #20
OK, I'll try it with version 8.0.74.
ze...@google.com <ze...@google.com> #21
Thanks for confirming the fix works and for reporting the issue! A fix will be pushed to 8.0, 8.1, 8.2 and 8.3 branches for R8.
I'm assuming you know the set up to use newer versions of R8 without AGP updates, but if not or others find this issue, I've included it below.
Amend your settings.gradle
or settings.gradle.kts
file with:
pluginManagement {
buildscript {
repositories {
mavenCentral()
maven {
url = uri("https://storage.googleapis.com/r8-releases/raw")
}
}
dependencies {
classpath("com.android.tools:r8:8.0.74")
}
}
}
Since AGP 8, the R8 versions follow those of AGP, so if using AGP 8.0 use the latest version on the R8 8.0 branch (for this
ap...@google.com <ap...@google.com> #22
Branch: 8.2
commit 82f1ea5dd4d1870dd50e8f31ba6fce8ab6ef8b24
Author: Ian Zerny <zerny@google.com>
Date: Tue Jan 09 09:12:13 2024
Version 8.2.45
Bug:
Change-Id: Iba6a0eef5e23e3d850e85e7efea6c27fce45fabc
M src/main/java/com/android/tools/r8/Version.java
ap...@google.com <ap...@google.com> #23
Branch: 8.2
commit 661f36cd71988b5f548a1163d1072d3c324e9c74
Author: Ian Zerny <zerny@google.com>
Date: Tue Jan 09 09:11:50 2024
Keep redundant interfaces if class and interface are kept.
Bug:
Change-Id: I417d3a914246dc066ff36b96ca64a77a17e0804e
M src/main/java/com/android/tools/r8/shaking/TreePruner.java
A src/test/java/com/android/tools/r8/ir/optimize/unusedinterfaces/UnusedButKeptInterfaceTest.java
sg...@google.com <sg...@google.com>
an...@google.com <an...@google.com> #24
Thank you for your patience while our engineering team worked to resolve this issue. A fix for this issue is now available in:
- Android Studio Hedgehog | 2023.1.1 Patch 2
- Android Gradle Plugin 8.2.2
We encourage you to try the latest update.
If you notice further issues or have questions, please file a new bug report.
Thank you for taking the time to submit feedback — we really appreciate it!
so...@google.com <so...@google.com> #25
Comment added by automation: A postmortem has been automatically requested for this issue. Please author a postmortem on IRM. See go/android-postmortem-guidance for more context and details, including how to handle the case when this issue does not need a postmortem. If you have any questions not answered by go/android-postmortem-guidance, please email android-hygiene-tpm@, instead of reassigning the fixed bug. Thank you.
an...@google.com <an...@google.com> #26
The fixes for this issue are now also available in:
- Android Studio Iguana | 2023.2.1 RC 1
- Android Gradle Plugin 8.3.0-rc01
We encourage you to try the latest update.
If you notice further issues or have questions, please file a new bug report.
Description
My code is just like this:
If gradle version is 7.x , the
interfaces.length()
is 0. Such as:If gradle version is 6.x, the
interfaces.length()
is 1. Such as:If gradle version is 7.x and
minifyEnabled false
, theinterfaces.length()
is 1. Such as:I must upgrade gradle version to 7.x in my android project and
minifyEnabled true
so, how can I solve this problem??? I expectinterfaces.length()
is not 0.