From 94d800d96fc96a7a9606a995308d9c7e0bcb78c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Mon, 4 Aug 2025 11:58:12 -0700 Subject: [PATCH] netjniutils: delete support for android R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mainline M-2025-11 drops support for android R, thus making the new minimum Android 12 / S (api 31) Once we get this to pass tests, we can merge it, then we need to bump min sdk to 31, then we can probably get rid of the dlsym stuff and just link it directly. Test: TreeHugger Flag: EXEMPT mainline Signed-off-by: Maciej Żenczykowski #include #include #include -#include -#include #include namespace android { namespace netjniutils { -static int GetNativeFileDescriptorWithoutNdk(JNIEnv* env, jobject javaFd) { - // Prior to Android S, we need to find the descriptor field in the FileDescriptor class. The - // symbol name has been stable in libcore, but is a private implementation detail. - // Older libnativehelper_compat_c++ versions had a jniGetFdFromFileDescriptor method, but this - // was removed in S to replace it with the NDK API in libnativehelper. - // The code is copied here instead. This code can be removed once R is not supported anymore. - static const jfieldID descriptorFieldID = [env]() -> jfieldID { - jclass cls = env->FindClass("java/io/FileDescriptor"); - jfieldID fieldID = env->GetFieldID(cls, "descriptor", "I"); - env->DeleteLocalRef(cls); - if (fieldID == nullptr) { - __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "Failed to get descriptor field."); - abort(); - } - return fieldID; - }(); +int GetNativeFileDescriptor(JNIEnv* env, jobject javaFd) { + if (!javaFd) return -1; - return env->GetIntField(javaFd, descriptorFieldID); -} - -static int GetNativeFileDescriptorWithNdk(JNIEnv* env, jobject javaFd) { // Since Android S, there is an NDK API to get a file descriptor present in libnativehelper.so. // libnativehelper is loaded into all processes by the zygote since the zygote uses it // to load the Android Runtime and is also a public library (because of the NDK API). @@ -68,15 +47,5 @@ static int GetNativeFileDescriptorWithNdk(JNIEnv* env, jobject javaFd) { return ndkGetFd(env, javaFd); } -int GetNativeFileDescriptor(JNIEnv* env, jobject javaFd) { - if (!javaFd) return -1; - static const bool preferNdkFileDescriptorApi = modules::sdklevel::IsAtLeastS(); - if (preferNdkFileDescriptorApi) { - return GetNativeFileDescriptorWithNdk(env, javaFd); - } else { - return GetNativeFileDescriptorWithoutNdk(env, javaFd); - } -} - } // namespace netjniutils } // namespace android