diff --git a/staticlibs/native/netjniutils/netjniutils.cpp b/staticlibs/native/netjniutils/netjniutils.cpp index ef49cf657c..26e306069d 100644 --- a/staticlibs/native/netjniutils/netjniutils.cpp +++ b/staticlibs/native/netjniutils/netjniutils.cpp @@ -15,40 +15,19 @@ #define LOG_TAG "netjniutils" #include "netjniutils/netjniutils.h" -#include #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