How to create an APK and individual libraries that an application loads dynamically

Short description: How to create an APK and separate libraries (by which I mean a set of classes (and ideally resources) in one form or another, such as JAR, AAR or DEX files), but do not include those libraries in the agro-industrial complex; instead, does the application load them at runtime?

Detail

So, my main question is: how to create such an application (e.g. Gradle). How to specify which classes are included in JAR or DEX files? Am I creating an Android Studio module for every DEX file I want to end in?

A close question is how Java code should then load external libraries and access their classes at runtime. For the latter, I hope that the approach shown in accessing application classes from the dex file by the class loader will work.

I tried the instructions at https://developer.android.com/studio/projects/android-library.html , but this creates an APK that includes a dependency library.

I also tried Multidex ( https://developer.android.com/studio/build/multidex.html ), but this does not seem to leave the developer any control over which classes go into the DEX file and also package them All in one APK. AFAICT cannot control the loading of these DEX files at run time.

Background

There is the possibility of an “ XY problem, ” so I’d better explain the background.

I am creating an application for a client. It will not be distributed through the app store, so it will not have access to the normal update mechanism. Instead, the client wants the application to be able to update itself by downloading new components on its own to replace old components, without having to manually download the new APK manually. The main motive here is that updates should be easy for non-technical users. If an application can control the update process, it can make it smooth and user-friendly.

In addition, the application will be used in areas where Internet access is limited and expensive, so the client wants to be able to release application updates in small pieces (for example, 2 MB), and not force the user to re-download the entire application to receive a small update.

One aspect of the requirements that I should mention in case it matters is that the libraries that need to be loaded at runtime must live on a microSD card. It can also help distribute updates without Internet access.

The current state of the application is that it is written about 50%: that is, a couple of earlier versions were released, but now the application needs to be changed (changed) to meet the above requirements, as well as others,

+28
android android-gradle mobile-application dynamic-class-loaders
Aug 26 '16 at 21:58
source share
2 answers

This tutorial is a good start for external download of DEX files. Only three small source files (MainActivity.java, LibraryInterface.java, LibraryProvider.java) and it copies the secondary_dex.jar file from the resource folder to the internal application storage [outdex / dex] (as indicated on the Internet in the tutorial). You have to build it with ant , as it uses custom build steps. I tried, it works fine. Worth a look.
custom class loading in Dalvik and ART


UPDATE this code has been ported to Android Studio gradle (no ant required). https://github.com/timrae/custom-class-loader
Tested ok . It copies com.example.toastlib.jar from the SDcard to the internal application storage [outdex / dex], (not the resource folder). (you must read the README.md file in the project to create it).

Q: How to add activity, I can’t add it to the manifest?
A: Using Fragments, they don’t need entries in the manifest.

Q: A jar with resources that must be added to an existing project should be able to combine its resources with its own resources (R.).
A: Hacks are available, data file ...
Upload Android resource files to a Jar distribution file

Q: The external file has incorrect permissions.
A: Import it.

Q: I need to add use-permission.
A: Use API23, you can programmatically add assignment permissions (but they should still be declared in the manifest, so the new permission model is probably not so much for us).

This section is for more general users (@LarsH has more specific update requirements). The example above is 17kb apk and 1 kb jar. You can put the bulk of your code in a one-time jar, and updates will simply include downloading a new Apk (and then importing a large ATM to minimize data transfer). When Apk gets too big, start with a small Apk and everything will move to another jar (import 2 jars). You need to balance coding efforts, user interface, serviceability, support, bandwidth, Android rules, file storage rules (if these words exist, O)).

NOTE Dalvik discontinued

Dalvik's successor is Android Runtime (ART), which uses the same bytecode and .dex files (but not .odex files), with continuity aimed at improving performance that is transparent to end users. The new runtime was first included in Android 4.4 “KitKat” as a technology preview and completely replaced Dalvik in later versions; Android 5.0 "Lollipop" is the first version in which ART is the only runtime included.

+5
Sep 01 '16 at 18:46
source share

You can try to build multiple apk with the same sharedUserId and the same process .

This is the plugin engine used by Threema

Edit: More on Theema

Threema has one main application and two plugins:

For this, the main application does not need permissions to access the camera or microphone

+1
Sep 01 '16 at 19:44
source share



All Articles