Skip to content

Commit

Permalink
Hide the usage of ASM from R8.
Browse files Browse the repository at this point in the history
This fixes a compile error for at least some R8 users.

I recently did some other work on the ASM dep in CL 336875929. I think that this CL and that one may technically be orthogonal. They're probably both good changes to make.

RELNOTES=Fixed (we think :)) R8 compilation failure: `Error: com.android.tools.r8.errors.b: Compilation can't be completed because `org.objectweb.asm.ClassVisitor` and 1 other classes are missing.`.
PiperOrigin-RevId: 338056327
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Oct 20, 2020
1 parent 6bcacbf commit 0bfa285
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions core/src/main/java/com/google/common/truth/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,23 @@ static String inferDescription() {
}
StackTraceElement top = stack.getStackTrace()[0];
try {
// Invoke reflectively so that Truth can be compiled and run without ASM on the classpath.
/*
* TODO(cpovirk): Consider always compiling with ASM present but then omitting it (and
* ActualValueInference itself) at runtime. Then we could use a trick more like Guava's
* UnsignedBytes.lexicographicalComparator() (being sure to eagerly load ASM classes at the
* same time as we load ActualValueInference). But the reference to a missing class is likely
* to confuse tools like Proguard (and thus require suppressions).
* Invoke ActualValueInference reflectively so that Truth can be compiled and run without its
* dependency, ASM, on the classpath.
*
* Also, mildly obfuscate the class name that we're looking up. The obfuscation prevents R8
* from detecting the usage of ActualValueInference. That in turn lets users exclude it from
* the compile-time classpath if they want. (And then *that* probably makes it easier and/or
* safer for R8 users (i.e., Android users) to exclude it from the *runtime* classpath. It
* would do no good there, anyway, since ASM won't find any .class files to load under
* Android. Perhaps R8 will even omit ASM automatically once it detects that it's "unused?")
*
* TODO(cpovirk): Add a test that runs R8 without ASM present.
*/
String clazz =
Joiner.on('.').join("com", "google", "common", "truth", "ActualValueInference");
return (String)
Class.forName("com.google.common.truth.ActualValueInference")
Class.forName(clazz)
.getDeclaredMethod("describeActualValue", String.class, String.class, int.class)
.invoke(null, top.getClassName(), top.getMethodName(), top.getLineNumber());
} catch (IllegalAccessException
Expand Down

0 comments on commit 0bfa285

Please sign in to comment.