From c8a33f1688323596cf29481d22655e2e9679c5d0 Mon Sep 17 00:00:00 2001 From: Yifan Hong Date: Tue, 29 Jun 2021 17:37:55 -0700 Subject: [PATCH] AndroidRuntime: Expose GetJavaVM Add a new header library, libandroid_runtime_vm_headers, that exposes AndroidRuntimeGetJavaVM. Test: boots Bug: 190450693 Change-Id: I64053b56eb1290cff8ea191b1cc592fbc9702ce8 --- core/jni/Android.bp | 27 ++++++++++++++++++++++++ core/jni/AndroidRuntime.cpp | 5 +++++ core/jni/include_vm/android_runtime/vm.h | 24 +++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 core/jni/include_vm/android_runtime/vm.h diff --git a/core/jni/Android.bp b/core/jni/Android.bp index b236385768b5..3e5fb9f7f948 100644 --- a/core/jni/Android.bp +++ b/core/jni/Android.bp @@ -297,6 +297,7 @@ cc_library_shared { header_libs: [ "bionic_libc_platform_headers", "dnsproxyd_protocol_headers", + "libandroid_runtime_vm_headers", ], }, host: { @@ -338,3 +339,29 @@ cc_library_shared { never: true, }, } + +cc_library_headers { + name: "libandroid_runtime_vm_headers", + host_supported: true, + vendor_available: true, + // TODO(b/153609531): remove when libbinder is not native_bridge_supported + native_bridge_supported: true, + // Allow only modules from the following list to create threads that can be + // attached to the JVM. This list should be a subset of the dependencies of + // libandroid_runtime. + visibility: [ + "//frameworks/native/libs/binder", + ], + export_include_dirs: ["include_vm"], + header_libs: [ + "jni_headers", + ], + export_header_lib_headers: [ + "jni_headers", + ], + apex_available: [ + "//apex_available:platform", + "com.android.media", + "com.android.media.swcodec", + ], +} diff --git a/core/jni/AndroidRuntime.cpp b/core/jni/AndroidRuntime.cpp index f4a10ef61585..f377608a6d3c 100644 --- a/core/jni/AndroidRuntime.cpp +++ b/core/jni/AndroidRuntime.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -1322,6 +1323,10 @@ void AndroidRuntime::onVmCreated(JNIEnv* env) return AndroidRuntime::mJavaVM; } +extern "C" JavaVM* AndroidRuntimeGetJavaVM() { + return AndroidRuntime::getJavaVM(); +} + /* * Get the JNIEnv pointer for this thread. * diff --git a/core/jni/include_vm/android_runtime/vm.h b/core/jni/include_vm/android_runtime/vm.h new file mode 100644 index 000000000000..a6e7c162d6ed --- /dev/null +++ b/core/jni/include_vm/android_runtime/vm.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +// Get the Java VM. If the symbol doesn't exist at runtime, it means libandroid_runtime +// is not loaded in the current process. If the symbol exists but it returns nullptr, it +// means JavaVM is not yet started. +extern "C" JavaVM* AndroidRuntimeGetJavaVM();