How to add android library (xx.jar) to apk in command-line? - android

For some reason I can't build apk by Eclipse.
If I need to add external jar file (include *.class, *.properties..), which files (Android.mk,
AndroidManifest.xml, default.properties?) do I need to modify and how should I do this?

For a standard Ant-based build, to include a third-party JAR, make a libs/ directory in your project and copy the JAR there. Everything else is handled for you by the standard Ant build script.

Related

How do I configure ant on Android to include an external jar without exporting it in the apk?

I Have an application that uses a shared jar library located on my platform. I would like to build my project off my platform. To do so, I need to include the jar file in compilation.
This works in Eclipse - I add the library to the project, and everything works fine. I encounter a problem after building with ant. Ant requires that I include the jar file in my libs directory. However if I include it there, it is exported into the apk. When I deploy this locally built apk to my device, I get runtime errors complaining about multiple symbol definitions.
Try to add a custom_rules.xml file to your project to exclude jar after compilation from apk. See here Exclude dependency jar file from apk when using Ant for Android

Libs folder vs build path dependency (ANDROID)

I have some jar libraries on which my app depends.
When I copy and paste the jars in the project/libs folder, it builds, compiles and executes correctly.
However, if I right click my project -> Build path -> Configure build path -> Libraries tab -> Add external JARs, and add exactly the same jars, it still gives me no error, it allows me to build and compile, but when the app is executed it crashes.
Why?
If you put jars in libs folder it will automatically add those JARs to your compile-time build path. More importantly, it will put the contents of the JARs into your APK file, so they will be part of your run-time build path.
Adding jar to build path with lib manually will solve the compile-time problem, but then your JAR contents are not in your APK and therefore will not be available to you at runtime.
Don't use add external jar this will add as Absolute path instead of this use the relative path by adding jar, with this you must put all the jar files into the libs directory.
If you using Add External Jar it will search for that jar file based on Absolute path so on device that path was not find and you getting Force Close.
Try with adding same jar file by using Add Jar and Add External Jar option.
check the difference here https://stackoverflow.com/a/10679344/760489

Android ant build: How to configure library order?

I am building an android application using one external somelibrary.jar file. In eclipse I can build it by
Configuring build path
Adding external somelibrary.jar
Change library order so that somelibrary.jar is before android system libraries.
Now I would like to build this project with ant. I dropped this somelibrary.jar file to /libs folder, but build does not work. I guess that this because somelibrary.jar has to be before android system libraries.
How can I change library order in ant build?
Order is not important. What is happening is that somelibrary.jar is not seen by ANT
I was not able to solve how to put this library before android.jar in the build order. However I was able to do workaround: Merge this library into the android.jar with these steps:
Unzip android.jar to "unzipped_android_jar" folder
Unzip somelibrary.jar on top of this "unzipped_android_jar" folder
Repackage android.jar by zipping the folder
Rename zip file to android.jar
Your library is now merged into system library android.jar. You can use this by replacing android.jar at SDK_FOLDER/platforms/android-N/android.jar

Adding prebuilt library to android apk

I have built a dynamic library in android using android build system. This library provides jni interface for functions inside it. Now I want to include this library in an application (.apk). I am using eclipse for application development. Now, how can I use the prebuild dynamice library (.so) in my application ? I tried putting it in a lib folder in my application but it is not working.
Any pointers are appreciated.
I am not using ndk to build my .so.
Since you write 'so' I think you're using NDK. If you're using NDK I don't know the answer.
If you're using the "Java" SDK, then in your library project go to Properties -> Android, and Check "Is Library". In your "apk" project, go to Properties -> Android -> Add . And your Library project should be available.
Also, any Library added in the "Java Build Path" Menu (again, in project properties) should be available in the APK in the end.
I know it's slightly old, but have you checked in the built APK to see if your .so library is there? Should be in the libs/armeabi folder.
Also, your .so file should be in lib/armeabi folder in your eclipse solution. I'm guessing the armeabi bit depends on which processor your .so file is build for.
Also, I know that if your library isn't called lib[name].so, it won't get copied when the apk is installed on the device. So:
libfoo.so copies
foo.so doesn't copy
foo.so doesn't copy
Also, you can use DDMS (its a view in eclipse) and it's file explorer to see if it's been copied to your device. It should be under data/data/[packagename]/lib.
Hope this helps a bit!
Andy.
I hit this same problem while building Qiqqa for Android. Under your eclipse android project, make sure you have a libs directory (not that it is plural libS not singular lib). Inside that create the armeabi/etc subdirs with their respectibe .so files.
Then when you build, eclipse will automatically pick up this libs directory and create the corresponding lib in your apk. System.loadLibrary("XXX") will then resolve to libXXX.so on your correct architecture...
Cheers,
Jimme

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.
How do I package up and build the .jar and .so? What are the best practices here?
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?
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?
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
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.
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.
Add the below lines to android.mk.
include $(BUILD_PACKAGE)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := alias:libs/your.jar
include $(BUILD_MULTI_PREBUILT)

Categories

Resources