Disable hidden API checks for WellKnownClasses

WellKnownClasses acquires handles to some special-cased classes/methods/
fields at startup using JNI. If the process has hidden API checks
enabled, it will prevent WellKnownClasses from doing that as the request
is coming from an unattached native thread. This patch disables the
checks for the duration of the initializer.

Test: check there are no log message during compilation on target
Bug: 64382372
Change-Id: I31b2293336ac634ce0e07fa4edc754cd7d1568e2
diff --git a/runtime/well_known_classes.cc b/runtime/well_known_classes.cc
index 902c3b8..67ea64b 100644
--- a/runtime/well_known_classes.cc
+++ b/runtime/well_known_classes.cc
@@ -283,7 +283,27 @@
 }
 #undef STRING_INIT_LIST
 
+class ScopedHiddenApiExemption {
+ public:
+  explicit ScopedHiddenApiExemption(Runtime* runtime)
+      : runtime_(runtime),
+        initially_enabled_(runtime_->AreHiddenApiChecksEnabled()) {
+    runtime_->SetHiddenApiChecksEnabled(false);
+  }
+
+  ~ScopedHiddenApiExemption() {
+    runtime_->SetHiddenApiChecksEnabled(initially_enabled_);
+  }
+
+ private:
+  Runtime* runtime_;
+  const bool initially_enabled_;
+  DISALLOW_COPY_AND_ASSIGN(ScopedHiddenApiExemption);
+};
+
 void WellKnownClasses::Init(JNIEnv* env) {
+  ScopedHiddenApiExemption hiddenapi_exemption(Runtime::Current());
+
   dalvik_annotation_optimization_CriticalNative =
       CacheClass(env, "dalvik/annotation/optimization/CriticalNative");
   dalvik_annotation_optimization_FastNative = CacheClass(env, "dalvik/annotation/optimization/FastNative");