How to load a JAR from Assets folder at runtime
Asked Answered
M

1

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 ..

Marabelle answered 10/6, 2014 at 7:22 Comment(7)
Do you mean that you want to load a jar file that wasn't linked at compilation time, when you built the apk?Caesium
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 pluginsMarabelle
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 loadingDense
do u have any working code to load jar from any external or internal memory.Marabelle
@Dense could you mention the reason why you down voted my question ??Marabelle
seems like you have a problem with reading with understanding ... i wrote: "Not possible" you wrote: "do u have any working code" ...Dense
Possible duplicate of Android: How to dynamically load classes from a JAR file?Azotize
M
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?

Marabelle answered 10/6, 2014 at 9:9 Comment(3)
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 ...Dense
this works. and this is what i am looking for. The jar files will be available inside Assets before building it into apk.Marabelle
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 effortCompton

© 2022 - 2024 — McMap. All rights reserved.