diff --git a/core/jni/Android.bp b/core/jni/Android.bp index 73c93ac9d06b7e408086e4af6031b4c072536282..81c32550cf144464e4ec7bbccd0221463b29a091 100644 --- a/core/jni/Android.bp +++ b/core/jni/Android.bp @@ -15,7 +15,19 @@ license { ], } -cc_library_shared { +soong_config_module_type { + name: "cc_library_shared_for_libandroid_runtime", + module_type: "cc_library_shared", + config_namespace: "ANDROID", + bool_variables: [ + "release_binder_death_recipient_weak_from_jni", + ], + properties: [ + "cflags", + ], +} + +cc_library_shared_for_libandroid_runtime { name: "libandroid_runtime", host_supported: true, cflags: [ @@ -46,6 +58,12 @@ cc_library_shared { }, }, + soong_config_variables: { + release_binder_death_recipient_weak_from_jni: { + cflags: ["-DBINDER_DEATH_RECIPIENT_WEAK_FROM_JNI"], + }, + }, + cpp_std: "gnu++20", srcs: [ diff --git a/core/jni/android_util_Binder.cpp b/core/jni/android_util_Binder.cpp index f4bc2675e30df24f82d80f23baac82cd2127997b..55382cc1d38098cf9246998014d5f1d1be31c5b3 100644 --- a/core/jni/android_util_Binder.cpp +++ b/core/jni/android_util_Binder.cpp @@ -556,6 +556,7 @@ public: }; // ---------------------------------------------------------------------------- +#ifdef BINDER_DEATH_RECIPIENT_WEAK_FROM_JNI #if __BIONIC__ #include <android/api-level.h> static bool target_sdk_is_at_least_vic() { @@ -568,6 +569,7 @@ static constexpr bool target_sdk_is_at_least_vic() { return true; } #endif // __BIONIC__ +#endif // BINDER_DEATH_RECIPIENT_WEAK_FROM_JNI class JavaDeathRecipient : public IBinder::DeathRecipient { @@ -588,9 +590,12 @@ public: // you normally are not interested in the death of a binder service which you don't have any // reference to. If however you want to get binderDied() regardless of the proxy object's // lifecycle, keep a strong reference to the death recipient object by yourself. +#ifdef BINDER_DEATH_RECIPIENT_WEAK_FROM_JNI if (target_sdk_is_at_least_vic()) { mObjectWeak = env->NewWeakGlobalRef(object); - } else { + } else +#endif + { mObject = env->NewGlobalRef(object); } // These objects manage their own lifetimes so are responsible for final bookkeeping.