6

I want to add a external third party jar file in the inbuilt android app.

I've added the LOCAL_CLASSPATH variable in Android.mk due to which the compilation goes fine. But during runtime, it is not able to find the class definiation which is in the JAR.

Which is the variable I need to set to add the third party JARs in the .dex/.apk ?

TIA.

Karan
  • 12,724
  • 6
  • 40
  • 33

5 Answers5

17

An example is more than just talking.

...

LOCAL_STATIC_JAVA_LIBRARIES := libmylibs

LOCAL_PACKAGE_NAME := myapp

...

include $(BUILD_PACKAGE)

##################################################
include $(CLEAR_VARS)

LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := libmylibs:mylib.jar

include $(BUILD_MULTI_PREBUILT)

Note: put the "mylib.jar" at the project root.

Taryn
  • 242,637
  • 56
  • 362
  • 405
davidj
  • 331
  • 2
  • 5
  • 1
    Just want to add a couple of notes: "libmylibs" is basically a symbolic name. You can just as easily use "foo", as long as you use the same name in the two declarations. Also, you can use a full path to your jar file if you like, e.g. "libs/mylib.jar". This is handy since it's compatible with a typical eclipse working environment. – Edward Falk Dec 11 '12 at 23:52
  • 1
    can you please explain what is LOCAL_PACKAGE_NAME, what should use it here, my jar file has packages like org.jivesoftwar.smack and org.jivesoftwar.smacks. Then should I use org.jivesoftwar as a LOCAL_PACKAGE_NAME? – anargund May 29 '13 at 19:02
  • 1
    Though, this answer really lacks of explanations, I think it's better than the accepted answer. – cwhsu Nov 13 '15 at 04:31
  • Added external jar as defined above. But still getting "package not found" compile time error. Any idea? – Sagar Trehan Dec 02 '16 at 10:37
  • how to import ..? – Amruth A Aug 27 '20 at 11:24
13

Here is what I used to solve the problem :

LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := path_to_jar_file.jar
include $(BUILD_MULTI_PREBUILT)

This should be added in Android.mk after include $(BUILD_PACKAGE)

You also need to specify the library name in LOCAL_STATIC_JAVA_LIBRARIES for compilation.

Cristian
  • 198,401
  • 62
  • 356
  • 264
Karan
  • 12,724
  • 6
  • 40
  • 33
0
LOCAL_STATIC_JAVA_LIBRARIES := \
other libs \
your_jar

include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := your_jar:jar_path/jar_file.jar
include $(BUILD_MULTI_PREBUILT)
BJDM
  • 101
  • 2
  • 4
0

Add it with LOCAL_STATIC_JAVA_LIBRARIES & LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES flag.

and put LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES between include $(BUILD_PACKAGE) & include $(BUILD_MULTI_PREBUILT).

It will OK. thanks for the URL a2ronus provided.

Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68
Sharl
  • 1
-2

In Eclipse choose modify build path and choose add external jar, and select the jar you watn to include.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119