Accessing methods from .so files that are on a device

1,115 views
Skip to first unread message

Steve Malek

unread,
May 24, 2018, 8:41:44 PM5/24/18
to android-ndk
Hello,

I am very new to android ndk and I am having trouble with the following issue.
I am writing a standalone application which I want for it to be able to load a .so file that is on my mobile device and call its methods.
Looking online I am not able to find a solution and most of what I find are based on older versions of Studio and Gradle.

I am familiar enough with NDK to create a simple .so file myself but no clear on how to access .so files external to my own project.
To be more clear what I need is to be able to call a external .so file from an Android App without having access to its source code.  As noted above an example would be something on the device that I can load to my app and call.

Can I directly call the .so file that is on my device from an application? 
Do I need to pull the .so file and place it in my project? 
Or is this not achievable without the source code being available?

Looking for advice on this.

Thanks!

J Decker

unread,
May 24, 2018, 10:24:40 PM5/24/18
to andro...@googlegroups.com
what you're probably missing is the java->C interface?
it's JNI - ... can google seach 'android jni ndk' maybe?


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/d99d2e79-779f-4d9f-ad6d-59edd4694d86%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Steve Malek

unread,
May 25, 2018, 10:50:17 AM5/25/18
to android-ndk
Just to be clear are you suggesting that in order to call a native library from the device I should do it within the C code using for example the dlopen() method rather than calling System.loadLibrary and passing a full path to the native library?  
I have tried the latter but method is not being recognized and hence I get compiler errors, however I have not tried writing a C program to do this.
Problem is I have read that using System.loadLibrary should work fine in the situation I describe.
I apologize for my confusion.



On Thursday, May 24, 2018 at 10:24:40 PM UTC-4, J Decker wrote:
what you're probably missing is the java->C interface?
it's JNI - ... can google seach 'android jni ndk' maybe?

On Thu, May 24, 2018 at 2:52 PM, Steve Malek <pmal...@gmail.com> wrote:
Hello,

I am very new to android ndk and I am having trouble with the following issue.
I am writing a standalone application which I want for it to be able to load a .so file that is on my mobile device and call its methods.
Looking online I am not able to find a solution and most of what I find are based on older versions of Studio and Gradle.

I am familiar enough with NDK to create a simple .so file myself but no clear on how to access .so files external to my own project.
To be more clear what I need is to be able to call a external .so file from an Android App without having access to its source code.  As noted above an example would be something on the device that I can load to my app and call.

Can I directly call the .so file that is on my device from an application? 
Do I need to pull the .so file and place it in my project? 
Or is this not achievable without the source code being available?

Looking for advice on this.

Thanks!

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

Alex Cohn

unread,
May 26, 2018, 5:29:33 AM5/26/18
to android-ndk
There are different kinds of .so files on your Android device.
  1. libraries automatically installed for your app. They come inside the APK package and the system installer unpacks them to the app native libraries directory. These libs can be loaded from Java by System.loadLibrary(). You must call this API if they are JNI libraries. Since Lollipop, you don't need to load these libraries in reverse dependency order, they will be automatically resolved by the system dynamic linker. These libs are publicly accessible for read, but no app (including your app) can change them.

    Note that since Marshmallow, you can set application/extractNativeLibs to false in your Manifest, and the native libraries will be used without unpacking. The APK file itself is also publicly readable.

  2. libraries that you install in non-automatic way. E.g. some apps have different flavors of native libs packaged in their APK, and extract the correct flavor on activation. These libs typically get unpacked into app private directory, and can be loaded from Java by System.load(). These libs are not  

  3. public system libraries, that are part of NDK, like libc or libdl. These will be automatically loaded by the system dynamic linker if a loaded library depends on one of them.

  4. non-public system libraries, like libcrypto or libcutil. You should not use these libraries in your app. Since Nougat, Android actively protects these libraries from being loaded by user apps.

  5. libraries that are installed by other apps (see 1.). Today, any app can load native libraries from another app, if it knows the full path to the lib file. 

BR,
Alex
Reply all
Reply to author
Forward
0 new messages