我正在使用 sys/system_properties.h 中的 __system_property_get() 来获取系统属性。我正在尝试使用 r10c ndk,因为我需要 arm64 工具链。
__system_property_get() 在 libc.so 中定义。以下是 armv5/armv7a 的 libc.so 的 readelf 输出。
readelf -Ws libc.so | grep property_get
194: 00009100 20 FUNC GLOBAL DEFAULT 4 __system_property_get
198: 00009100 20 FUNC GLOBAL DEFAULT 4 __system_property_get
但是,看起来它已从 arm64 版本中删除!我收到一个链接器错误,说它没有定义。我分析了所有 arm64 的共享库,但没有一个有这个符号。
是否有备用 API 来获取 native 代码中的系统属性?
谢谢!
最佳答案
它对 native 应用程序来说是有用的 API,就像它对 Java 应用程序一样,它起源于 native 端(参见 http://rxwen.blogspot.com/2010/01/android-property-system.html ),其他 Android 系统代码也使用它,因此它不太可能很快消失。
#include <android/log.h>
#include <dlfcn.h>
#if (__ANDROID_API__ >= 21)
// Android 'L' makes __system_property_get a non-global symbol.
// Here we provide a stub which loads the symbol from libc via dlsym.
typedef int (*PFN_SYSTEM_PROP_GET)(const char *, char *);
int __system_property_get(const char* name, char* value)
{
static PFN_SYSTEM_PROP_GET __real_system_property_get = NULL;
if (!__real_system_property_get) {
// libc.so should already be open, get a handle to it.
void *handle = dlopen("libc.so", RTLD_NOLOAD);
if (!handle) {
__android_log_print(ANDROID_LOG_ERROR, "foobar", "Cannot dlopen libc.so: %s.\n", dlerror());
} else {
__real_system_property_get = (PFN_SYSTEM_PROP_GET)dlsym(handle, "__system_property_get");
}
if (!__real_system_property_get) {
__android_log_print(ANDROID_LOG_ERROR, "foobar", "Cannot resolve __system_property_get(): %s.\n", dlerror());
}
}
if (!__real_system_property_get) return (0);
return (*__real_system_property_get)(name, value);
}
#endif // __ANDROID_API__ >= 21
关于android - 在 arm64 平台中删除了获取 android 系统属性的 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28413530/