Search code examples
androidc++java-native-interface

How long does a global variable (nonstatic) declared in JNI-level will live?


Example - in my Android app, at JNI layer, I declared something such as:

int32_t my_glob_var;

Is this variable guaranteed to live as long as the process does? And as such, as long as the application is running. Or are there any caveats I'm not aware of?


Solution

  • This variable will live as long as the .so file is loaded, so it will live as long as the process is alive and your classes have been loaded. If you want to make things more persistent you will need to hook into lifecycle events and use explicit persistence libraries like SharedPreferences.