Replacement for `__system_property_get` in Android L NDK
Asked Answered
I

3

10

As of the Android L NDK, __system_property_get is removed (https://groups.google.com/a/chromium.org/forum/#!topic/chromium-reviews/keQP6L9aVyU). Is there another API in the Android L NDK to access the same property values?

Innings answered 3/11, 2014 at 19:55 Comment(0)
I
9

I went with popen as detailed in the answer at https://mcmap.net/q/64265/-how-do-i-execute-a-command-and-get-the-output-of-the-command-within-c-using-posix to run getprop. Something like

std::string command = "getprop ro.product.model";
FILE* file = popen(command.c_str(), "r");
if (!file) {
    // error
}
// read the property value from file
pclose(file);
Innings answered 3/11, 2014 at 20:45 Comment(0)
M
4

You can use __system_property_read / __system_property_read_callback (__ANDROID_API__ >= 26). I couldn't find official doc about it but it's well documented in system_properties.h.

I know it's not for Android L but I think a more modern solution should be written.

#include <sys/system_properties.h>

const prop_info* pi = __system_property_find("persist.sys.locale");
std::string prop;  // Not local, it will be modified by the callback.
if (pi != nullptr) {
  __system_property_read_callback(
      pi,
      [](void* cookie, const char*, const char* value, unsigned int) {
        *prop = value;
      },
       &prop);
}
Mammal answered 5/5, 2023 at 15:16 Comment(0)
T
0

what about

androidVersion = system("getprop ro.build.version.release");

   printf("%s", androidVersion.c_str());
Tahsildar answered 3/11, 2022 at 4:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.