Add a test where R8 fails by itself keeping the Kotlin metadata class
Bug: b/435327947
Change-Id: Id23144963ad13f40643de911612acb10f18a9bee
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataKeepClassOnlyTest.java b/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataKeepClassOnlyTest.java
new file mode 100644
index 0000000..bd05a1c
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataKeepClassOnlyTest.java
@@ -0,0 +1,60 @@
+// Copyright (c) 2025, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package com.android.tools.r8.kotlin.metadata;
+
+import static org.junit.Assert.assertThrows;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.KotlinCompileMemoizer;
+import com.android.tools.r8.KotlinTestParameters;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.kotlin.metadata.metadata_pruned_fields.Main;
+import com.android.tools.r8.shaking.ProguardKeepAttributes;
+import java.util.Collection;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class MetadataKeepClassOnlyTest extends KotlinMetadataTestBase {
+
+ public TestParameters parameters;
+
+ @Parameterized.Parameters(name = "{0}, {1}")
+ public static Collection<Object[]> data() {
+ return buildParameters(
+ getTestParameters().withAllRuntimesAndApiLevels().build(),
+ getKotlinTestParameters().withAllCompilersAndLambdaGenerations().build());
+ }
+
+ public MetadataKeepClassOnlyTest(
+ TestParameters parameters, KotlinTestParameters kotlinParameters) {
+ super(kotlinParameters);
+ this.parameters = parameters;
+ }
+
+ private static final KotlinCompileMemoizer code =
+ getCompileMemoizer(getKotlinFileInTest(PKG_PREFIX + "/keep_class_only", "A"));
+
+ @Test
+ public void testR8() throws Exception {
+ assertThrows(
+ // TODO(b/435327947): Fails with: Unexpected error during rewriting of Kotlin metadata for
+ // class 'A': com.android.tools.r8.kotlin.KotlinClassMetadataReader$MetadataError: element
+ // 'k' is missing.
+ CompilationFailedException.class,
+ () -> {
+ testForR8(parameters.getBackend())
+ .addProgramFiles(kotlinc.getKotlinStdlibJar())
+ .addProgramFiles(code.getForConfiguration(kotlinParameters))
+ .addProgramClassFileData(Main.dump())
+ .addKeepRules("-keep class A { *; }")
+ .addKeepRules("-keep class kotlin.Metadata")
+ .setMinApi(parameters)
+ .addKeepAttributes(ProguardKeepAttributes.RUNTIME_VISIBLE_ANNOTATIONS)
+ .compile();
+ });
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/keep_class_only/A.kt b/src/test/java/com/android/tools/r8/kotlin/metadata/keep_class_only/A.kt
new file mode 100644
index 0000000..ee9ae0f
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/keep_class_only/A.kt
@@ -0,0 +1,7 @@
+// Copyright (c) 2025, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+class A() {
+ fun m() {}
+}