6

How to load a jar file from assets folder of android application during run time. Loading from assets folder is my requirement. Is there any way to do this. Please help ..

Arundas K V
  • 801
  • 2
  • 14
  • 28
  • Do you mean that you want to load a jar file that wasn't linked at compilation time, when you built the apk? – Daniel Hernández Alcojor Jun 10 '14 at 07:25
  • jar files will be available at assets during compilation. But i want to load it from assets folder during runtime based on requirement. not all jars needed all time. I want to implement a mechanism like adding jars to application like adding different plugins – Arundas K V Jun 10 '14 at 07:30
  • Not possible, android using dalvik not normal jvm... Please next time use google to find other option im pretty sure that was an article on android developers blog how to use dynamic binary loading – Selvin Jun 10 '14 at 07:32
  • do u have any working code to load jar from any external or internal memory. – Arundas K V Jun 10 '14 at 07:34
  • @Selvin could you mention the reason why you down voted my question ?? – Arundas K V Jun 10 '14 at 09:01
  • seems like you have a problem with reading with understanding ... i wrote: "Not possible" you wrote: "do u have any working code" ... – Selvin Jun 10 '14 at 10:19
  • Possible duplicate of [Android: How to dynamically load classes from a JAR file?](http://stackoverflow.com/questions/7947422/android-how-to-dynamically-load-classes-from-a-jar-file) – rds Sep 06 '16 at 15:35

1 Answers1

7

I got the answer.I am adding this answer here. Because this may be helpful to some others searching.

There are steps to accomplish this.

  1. You have to make a copy of your JAR file into the private internal storage of your aplication.

    1. Using the dx tool inside the android folder, you have to generate a classes.dex file associated with the JAR file. The dx tool will be at the location /android-sdks/build-tools/19.0.1 (this file is needed by the Dalvik VM, simply jar can not be read by the dalvik VM))

    2. Using the aapt tool command which is also inside the same location, you have to add the classes.dex to the JAR file.

    3. This JAR file could be loaded dynamically using DexClassLoader.

    4. If you are making a JAR from any one your own library, you have to do this steps (1-4) every time when there is a change in your library source code. So you can automate this steps by creating a shell script(in Mac/Linux/Ubuntu) or batch scripts(in Windows). You can refere this link to understand how to write shell scripts.

Note : One situation for implementing this method is, when it is impossible to add the JAR files directly to the build path of core project and need to be loaded dynamically at run time. In normal cases the JAR files could be added to the build path.

please check this link for the detailed code and implementation.

How to load a jar file at runtime

Android: How to dynamically load classes from a JAR file?

Community
  • 1
  • 1
Arundas K V
  • 801
  • 2
  • 14
  • 28
  • did you test it? it will not work since classes in jar are not in dalvik format ... and your links shows only 1) how to do it in normal java 2) how to load class that is already part of framework ... – Selvin Jun 10 '14 at 10:21
  • 1
    this works. and this is what i am looking for. The jar files will be available inside Assets before building it into apk. – Arundas K V Jun 10 '14 at 10:29
  • Thanks, i also loaded both jar and dex after caching them in my apps internal files dir. 1. my app checks & caches the dex & jar files on launch. 2. then it uses DexClassLoader to load required classes. 3. then it uses java reflection to access members & methods. This was necessary for me coz i use LGPL code which only provides option for dynamic linking, i can't implement the jar / java classes directly in app due to LGPL limitation. This process is working perfectly, but with a little more effort – Diljeet Apr 14 '21 at 02:36