Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a product SDK: How do I add a native lib (.so) and a jar with the SDK I am creating?

I am creating a widget that we will provide to developer end users and it consists of a .jar and a native library (.so) built using the NDK. The JAR has a JNI interface to the dynamic library.

It's very clear on how to include an external .jar in a project but not how to include a dependent dynamic library.

  1. How do I package up and build the .jar and .so? What are the best practices here?

  2. I can create the JAR file using the JDK's jar command. Do I need to run dx.bat on the jar to convert to Dalvik bytecode?

  3. I need to create a sample project showing the widget in action. How do I include this .jar and .so in a sample project that demonstrates how to use the widget?

like image 411
The Sarge Avatar asked Feb 03 '11 02:02

The Sarge


4 Answers

I spent some time on this, and i just can't understand why isn't this written on wikitude documentation.... anyway follow this changes!

  • go to windows/preferences/android/build
  • uncheck the first and the second option
  • extract files from wikitudesdk.jar with winrar as if it is an archive, search libarchitect.so and copy it in /libs/libs/armeabi/
  • add wikitudesdk.jar to your build path

enter image description here

like image 115
Alessandro Annini Avatar answered Nov 08 '22 09:11

Alessandro Annini


You should use the standard build tools included with the SDK for this. If you include the .jar files you need in the /lib directory of your project, the ant build process will convert the included class files to Dalvik bytecode format and include them in your classes.dex file for the app. Add a build.properties file to your project root as well, with one line:

external.libs.dir=lib

Depending on the version of your SDK and configuration of it, you may need to place the jar in libs rather than lib.

As for the .so, I presume that it's properly compiled using the Android NDK, or using a build script that uses the proper compiler and compiler flags that are required to successfully link the shared object on Android. If this is the case, you can include the .so file in libs/armeabi and they will be added in the jar as well. Furthermore, the dynamic library loader will know to look in this location in the .jar when you actually try to load the library from Java code.

like image 23
Jason LeBrun Avatar answered Nov 08 '22 07:11

Jason LeBrun


Using ADT 12, I accomplished this by doing the following:

1) Export JAR from your library with the SO file using Eclipse. Make sure you exclude AndroidManifest.xml. This will include source code and other data, so if you are distributing, you'll want to strip these unnecessary bits out using any ZIP utility.

2) Create a directory in your App's source tree (I use "lib" directory) and copy your library JAR to it.

3) Right-click project in Eclipse and select "Configure Build Path". Add JAR and point it to JAR inside your App source tree.

4) In your Eclipse preferences, select Android/Build section and uncheck "Automatically refresh Resources and Assets folder on build". You will notice an option below that says "Force error when external jars contain native libraries." There is an ADT bug, which will supposedly be fixed in R17, which uses the wrong setting. Once it is fixed, you will use the "Force error" one (make sure it unchecked).

5) Once you build your app APK, you will have a libs/armeabi/libXXXX.so file. This will not be automatically unpacked on install. You will need to write code to extract it from your APK file into your data directory, then use System.load on the .so file in your data directory.

I have bidirectional JNI calls going from the dynamically loaded file, and even use dlopen() on it to do my custom plugin registration stuff.

like image 8
n8n8baby Avatar answered Nov 08 '22 09:11

n8n8baby


Add the below lines to android.mk.

include $(BUILD_PACKAGE)

LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := alias:libs/your.jar

include $(BUILD_MULTI_PREBUILT)
like image 1
aganpro Avatar answered Nov 08 '22 08:11

aganpro