我正在使用sys / system_properties.h中的__system_property_get()来获取系统属性。我正在尝试使用r10c ndk,因为我需要arm64工具链。
__ system_property_get()在libc.so中定义。下面是libc.so对于armv5 / armv7a的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可以通过本机代码获取系统属性?
谢谢!
在较早的NDK中,这不是官方支持的API。早期它被错误地暴露于32位ABI,但是直到得到正式支持之前,它才暴露于64位ABI。无论如何,系统会在所有API级别上公开它,因此,无论ABI或minSdkVersion
,较新的NDK都可以使用它。
#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());
}
}
return (*__real_system_property_get)(name, value);
}
#endif // __ANDROID_API__ >= 21
确认@bleater的答案是未公开的__system_properties_ *符号的变通办法:根据需要dlopen libc和dlsym。