cytrinox
cytrinox

Reputation: 1946

Where to load a native library in Android if required by multiple activies and services

Some of my Kotlin classes are implemented by a native library (Rust using jni). These classes are used in most of the activies and by one sync service (a second service may come later).

What is the recommended solution to load this library and handle loading errors (for example sending a bug report if loading fails)?

Basic examples loads a library in the MainActivity.onCreate. Or often I see a static block inside the MainActivity class. Another method may be loading the library in a derived class from Application. But I'm not sure if there is only one Application instance for all activities and services or if one `` Application``` instance is created for each service and another one for all activities.

So my question is: where to put System.loadLibrary()?

Upvotes: 1

Views: 1357

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57163

Yes, the Application is a singleton for the whole Android app. But you should not worry, LoadLibrary() does nothing if the library had been loaded before. So, the best practice is to add LoadLibrary in static blocks of all classes that depend on this library.

Consider using ReLinker library that provides logging and also workarounds for some widespread problems with loading native libraries.

Upvotes: 1

Related Questions