Merge "netjniutils: delete support for android R" into main

This commit is contained in:
Maciej Żenczykowski 2025-08-27 13:05:29 -07:00 • committed by Android (Google) Code Review
commit 8aa8a4f1d1

View file

@ -15,40 +15,19 @@
#define LOG_TAG "netjniutils"
#include "netjniutils/netjniutils.h"
#include <android-modules-utils/sdk_level.h>
#include <dlfcn.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/system_properties.h>
#include <android/api-level.h>
#include <android/log.h>
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