netjniutils: delete support for android R

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 <maze@google.com
Change-Id: I9d68ea9f4aa180b3ec24d252bcd49593e19cf475
This commit is contained in:
Maciej Żenczykowski 2025-08-04 11:58:12 -07:00
commit 94d800d96f

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