Add test for unused class to shrinker-test (#2455)

This commit is contained in:
Marcono1234 2023-08-23 01:31:56 +02:00 committed by GitHub
parent 392cc65ff3
commit 393db094dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 0 deletions

View File

@ -38,3 +38,7 @@
-keepclassmembernames class com.example.ClassWithVersionAnnotations {
<fields>;
}
# Keep the name of the class to allow using reflection to check if this class still exists
# after shrinking
-keepnames class com.example.UnusedClass

View File

@ -0,0 +1,17 @@
package com.example;
import com.google.gson.annotations.SerializedName;
/**
* Class with no-args constructor and field annotated with {@code @SerializedName},
* but which is not actually used anywhere in the code.
*
* <p>The default ProGuard rules should not keep this class.
*/
public class UnusedClass {
public UnusedClass() {
}
@SerializedName("i")
public int i;
}

View File

@ -19,7 +19,9 @@ package com.google.gson.it;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import com.example.UnusedClass;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
@ -248,4 +250,18 @@ public class ShrinkingIT {
}
});
}
@Test
public void testUnusedClassRemoved() throws Exception {
// For some reason this test only works for R8 but not for ProGuard; ProGuard keeps the unused class
assumeTrue(jarToTest.equals(R8_RESULT_PATH));
String className = UnusedClass.class.getName();
ClassNotFoundException e = assertThrows(ClassNotFoundException.class, () -> {
runTest(className, c -> {
fail("Class should have been removed during shrinking: " + c);
});
});
assertThat(e).hasMessageThat().contains(className);
}
}