orodbhen
orodbhen

Reputation: 2694

Is JNI_OnLoad normally used for Android NDK?

I'm new to Android NDK, and I was reading these tips on the Android Developer site. It recommends using JNI_OnLoad to register your native methods, but doesn't give much detail on how to do that.

Searching Google's NDK sample repo on GitHub only turned up one usage of JNI_OnLoad, and it doesn't call RegisterNatives. Searching the web didn't turn up much on how to do this either.

I feel like I'm missing something. This is supposed to be the correct way to do it, according to Google, but their own examples use the "discovery" method naming approach instead.

Is this perhaps an old way of doing it, that's not really done anymore?

Upvotes: 3

Views: 2861

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57203

RegisterNatives is fully supported on Android, and the proper way to do it is from JNI_OnLoad, which also works well and is shown prominently in NDK documentation. There are few reasons to use RegisterNatives vs. the usual automatic resolution of native methods through name matching (always use javah to get correct names).

  1. When you have many of native methods, you may not want to have a huge exported function table in your shared library.

  2. Using automatic matching makes reverse engineering and hacking your shared library easier.

  3. You can build custom logic to match native methods at runtime.

  4. @CriticalNative methods must be registered with RegisterNatives instead of relying on dynamic JNI linking.

None of these reasons apply to how-to samples and introductory tutorials.

Upvotes: 4

Related Questions