From 43ba9df55f4bfc30e9ba4dda72ff53c05599f0d5 Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Mon, 9 Jun 2025 19:15:47 -0700 Subject: [PATCH] Add jni target to ReactCxxPlatform (#51898) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51898 changelog: [internal] Reviewed By: andrewdacenko Differential Revision: D76240376 fbshipit-source-id: bbae620dde69261e6d26d088471601e7d7e9a074 --- .../ReactCxxPlatform/react/jni/JniHelper.cpp | 28 +++++++++++++++++++ .../ReactCxxPlatform/react/jni/JniHelper.h | 19 +++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 packages/react-native/ReactCxxPlatform/react/jni/JniHelper.cpp create mode 100644 packages/react-native/ReactCxxPlatform/react/jni/JniHelper.h diff --git a/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.cpp b/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.cpp new file mode 100644 index 00000000000..a3689d5d094 --- /dev/null +++ b/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include + +namespace facebook::react { + +jobject getApplication(JNIEnv* env) { + auto activityThreadClass = env->FindClass("android/app/ActivityThread"); + auto currentApplicationMethodID = env->GetStaticMethodID( + activityThreadClass, "currentApplication", "()Landroid/app/Application;"); + return env->CallStaticObjectMethod( + activityThreadClass, currentApplicationMethodID); +} + +jni::alias_ref getContext() { + auto env = facebook::jni::Environment::ensureCurrentThreadIsAttached(); + auto application = getApplication(env); + return facebook::jni::wrap_alias( + static_cast(application)); +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.h b/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.h new file mode 100644 index 00000000000..8db809186f3 --- /dev/null +++ b/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include +#include + +namespace facebook::react { + +jobject getApplication(JNIEnv* env); + +jni::alias_ref getContext(); + +} // namespace facebook::react