What is the correct way to bundle native libraries?

590 views
Skip to first unread message

Gili

unread,
Jul 10, 2012, 10:27:56 AM7/10/12
to andro...@googlegroups.com
Hi,

I are trying to port the JNA library to Android. Under conventional operating systems, JNA bundles native libraries inside JAR files and unpacks them at runtime. It's my understanding that Android is a bit different in that:

  1. Libraries cannot be loaded from the "java.io.tmpdir" directory.
  2. Native libraries are automatically unpacked from APK if they are bundled correctly.


Can you please let me know what is the correct way for JNA to unpack native libraries? Keep in mind that end-user applications link against JNA so JNA is not directly involved in the building of APK files. Furthermore, JNA needs to unpack two kinds of libraries:


  1. It's own native libraries, bundled in jna.jar
  2. User native libraries, bundled with the application.


What do you recommend we do?


Thank you,

Gili

David Turner

unread,
Jul 10, 2012, 11:43:25 AM7/10/12
to andro...@googlegroups.com
See $NDK/docs/CPU-ABIS.html, there is a section at the end that describes exactly what you need.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/android-ndk/-/ZuAeQotxDk4J.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

cowwoc

unread,
Jul 10, 2012, 12:17:37 PM7/10/12
to andro...@googlegroups.com
Hi David,

    One point of clarification: how is a library, such as JNA, supposed to bundle its own native libraries in order for them to be extracted when the application that links against it is installed? Will packaging the native libraries in lib/<abi>/lib<name>.so inside the JAR file work? Does JNA need to package itself using an Android-specific "Library Project" instead?

Thanks,
Gili

David Turner

unread,
Jul 11, 2012, 4:52:26 AM7/11/12
to andro...@googlegroups.com
On Tue, Jul 10, 2012 at 6:17 PM, cowwoc <cow...@bbs.darktech.org> wrote:
Hi David,

    One point of clarification: how is a library, such as JNA, supposed to bundle its own native libraries in order for them to be extracted when the application that links against it is installed? Will packaging the native libraries in lib/<abi>/lib<name>.so inside the JAR file work? Does JNA need to package itself using an Android-specific "Library Project" instead?


The platform / package manager don't know anything about .jar files, they really can't handle them (this is not a JVM).
I assume that its the build tool that should be modified to extract these files from the .jar and place them at the correct location in the .apk. As far as I know, this is not supported, though I guess there is already a bug for this on b.android.com but I can't find it right now.

I recommend filing a new issue (e.g. "Allow library .jar files to contain native libraries") on b.android.com and the devtools team would be able to look at it.

cowwoc

unread,
Jul 11, 2012, 9:10:25 AM7/11/12
to andro...@googlegroups.com

    When you compile an Android project, does it copy all class files and resources (libraries and application code) into one directory and generate an APK out of that? I mean, how do library class files end up in the APK? If the build process is already copying the class files over, perhaps it's copying the native libraries over too...?

    I'm going to try experimenting later on today.

Gili

Chris Stratton

unread,
Jul 11, 2012, 10:23:23 AM7/11/12
to android-ndk
On Jul 11, 9:10 am, cowwoc <cow...@bbs.darktech.org> wrote:
>      When you compile an Android project, does it copy all class files
> and resources (libraries and application code) into one directory and
> generate an APK out of that? I mean, how do library class files end up
> in the APK? If the build process is already copying the class files
> over, perhaps it's copying the native libraries over too...?

It's likely filtering out anything that is not a class file.

If it were copying them by accident, it would likely get an error when
trying to convert them to dex format.

And even if it didn't try to process them as something else, they'd
end up in the wrong place in the apk to be recognized as compatible-
ABI native libraries by the platform and copied out for use on
installation.

Patching the build system to handle them should be possible though; in
the meantime you will have to instruct developers who use your library
to copy your tree of native libs into their project's libs/ folder
Reply all
Reply to author
Forward
0 new messages