API 获取 android 系统属性在 arm64 平台中被移除

API to get android system properties is removed in arm64 platforms

我正在使用 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 的所有共享库,但其中 none 个具有该符号。

是否有备用 API 来获取本机代码中的系统 属性?

谢谢!

在旧的 NDK 中,这不是官方支持的 API。早期错误地暴露了 32 位 ABI,直到正式支持后才暴露 64 位 ABI。无论如何,它 由系统在所有 API 级别公开,因此无论 ABI 或 minSdkVersion.

,较新的 NDK 都可以使用它

它对本机应用程序很有用API,就像对 Java 应用程序一样,它源自本机端(参见 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

确认@bleater 的回答是未公开 __system_properties_* 符号的解决方法:根据需要使用 dlopen libc 和 dlsym。