Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a JAR from Assets folder at runtime

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

like image 458
Arundas K V Avatar asked Jun 10 '14 07:06

Arundas K V


People also ask

How can I access a folder inside of a resource folder from inside my JAR file?

What you could do is to use getResourceAsStream() method with the directory path, and the input Stream will have all the files name from that dir. After that you can concat the dir path with each file name and call getResourceAsStream for each file in a loop.


1 Answers

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?

like image 196
Arundas K V Avatar answered Sep 20 '22 20:09

Arundas K V