Enable use of minimized synthetic names in L8 desugaring
RELNOTES:
The name of synthetic classes generated by D8 normally contains a marker "$$ExternalSynthetic" that tells this is a synthetic generated by D8. Moreover, the name of the synthetic also encodes the synthetic kind (e.g., "Backport", "Lambda"). This has a negative impact on the resulting DEX size, since the class names take up more space in the string pool.
This enables a new option in L8 (core library desugaring) so that the DEX file containing all j$ classes will use a new shortened class name format for synthetic classes, which simply uses a numeric id (e.g., "$1").
Bug: b/185560004
Change-Id: I590c34083f53aba76d797697bc84d1b2c4069e22
diff --git a/src/main/java/com/android/tools/r8/L8Command.java b/src/main/java/com/android/tools/r8/L8Command.java
index 7a8e096..023f3dd 100644
--- a/src/main/java/com/android/tools/r8/L8Command.java
+++ b/src/main/java/com/android/tools/r8/L8Command.java
@@ -232,6 +232,8 @@
internal.threadCount = getThreadCount();
}
+ internal.desugarSpecificOptions().minimizeSyntheticNames = true;
+
// Disable global optimizations.
internal.disableGlobalOptimizations();
internal
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/RetargetAndBackportTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/RetargetAndBackportTest.java
index dae52bc..bcd0fb7 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/RetargetAndBackportTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/RetargetAndBackportTest.java
@@ -92,7 +92,7 @@
MethodSubject firstBackportMethod =
inspector
.clazz(
- SyntheticItemsTestUtils.syntheticBackportClass(
+ SyntheticItemsTestUtils.syntheticClassWithMinimalName(
toMillisMethod.getFinalReference().getHolderClass(), 1))
.uniqueMethod();
assertThat(firstBackportMethod, isPresent());
@@ -100,7 +100,7 @@
MethodSubject secondBackportMethod =
inspector
.clazz(
- SyntheticItemsTestUtils.syntheticBackportClass(
+ SyntheticItemsTestUtils.syntheticClassWithMinimalName(
toMillisMethod.getFinalReference().getHolderClass(), 2))
.uniqueMethod();
assertThat(secondBackportMethod, isPresent());
diff --git a/src/test/testbase/java/com/android/tools/r8/synthesis/SyntheticItemsTestUtils.java b/src/test/testbase/java/com/android/tools/r8/synthesis/SyntheticItemsTestUtils.java
index 513c533..9b5bb95 100644
--- a/src/test/testbase/java/com/android/tools/r8/synthesis/SyntheticItemsTestUtils.java
+++ b/src/test/testbase/java/com/android/tools/r8/synthesis/SyntheticItemsTestUtils.java
@@ -53,8 +53,11 @@
}
public static ClassReference syntheticClassWithMinimalName(Class<?> clazz, int id) {
- return SyntheticNaming.makeMinimalSyntheticReferenceForTest(
- Reference.classFromClass(clazz), Integer.toString(id));
+ return syntheticClassWithMinimalName(Reference.classFromClass(clazz), id);
+ }
+
+ public static ClassReference syntheticClassWithMinimalName(ClassReference clazz, int id) {
+ return SyntheticNaming.makeMinimalSyntheticReferenceForTest(clazz, Integer.toString(id));
}
private static ClassReference syntheticClass(Class<?> clazz, SyntheticKind kind, int id) {