如何构建APK和分离应用动态加载的库 [英] how to build an APK and separate libraries that the app loads dynamically

查看:227
本文介绍了如何构建APK和分离应用动态加载的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短的摘要是:如何构建APK和独立的库(我指的是某种形式的类集(最好是资源),例如JAR,AAR或DEX文件),但不包括那些APK中的库;相反,应用程序会在运行时加载它们吗?

详细信息

所以我的主要问题是如何构建这样的应用程序(例如Gradle配置).如何指定将哪些类放入哪个JAR或DEX文件?是否要为每个最终要生成的DEX文件创建一个Android Studio模块?

一个密切相关的问题是Java代码随后应如何在运行时加载外部库并访问其类.对于后者,我希望该方法显示在 https://developer.android.com上的说明/studio/projects/android-library.html ,但这会构建一个确实包含依赖项库的APK.

我还尝试了Multidex( https://developer.android.com/studio /build/multidex.html ),但这似乎并没有使开发人员可以控制哪些类进入了哪个DEX文件,而且还将它们全部打包到一个APK中. AFAICT无法在运行时控制这些DEX文件的加载.

背景

这里可能出现" XY问题",因此,我最好解释一下背景.

我正在为客户构建应用程序.它不会通过应用程序商店进行分发,因此它将无法访问常规的更新机制.相反,客户端希望该应用能够通过下载自身的新组件来替换旧组件来更新自身,而无需手动加载新的APK.这里的主要动机是,对于非技术用户而言,更新必须很容易.如果该应用程序可以控制更新过程,则可以使其平滑并指导用户.

此外,该应用程序将在互联网访问稀缺和昂贵的地区使用,因此客户端希望能够以较小的块(例如2MB)发布应用程序更新,而不是强迫用户重新下载整个应用程序接收小更新.

在重要的情况下,我应该提到的要求的一个方面是,要在运行时加载的库应该驻留在microSD卡上.这也有助于在没有Internet访问的情况下分发更新.

该应用程序的当前状态是大约50%的编写状态:也就是说,已经发布了几个较早的版本,但是现在需要修改(重构)该应用程序才能满足上述要求,以及其他要求. .

解决方案

本教程是从外部加载DEX文件的一个很好的开始. 仅源文件的三个小文件(MainActivity.java,LibraryInterface.java,LibraryProvider.java)及其将资源文件夹中的secondary_dex.jar复制到内部应用程序存储[outdex/dex](本教程中也尽可能说明了互联网) ). 您必须使用 ant 进行构建,因为它使用了自定义的构建步骤. 我试过了,效果很好.值得一看.
在Dalvik和ART中加载自定义类


更新 此代码已被移植到Android Studio gradle(无需ant). https://github.com/timrae/custom-class-loader
测试正常.从 SD卡复制 com.example.toastlib.jar 内部应用程序存储 [outdex/dex](不是资产文件夹)中. (您必须阅读项目中的 README.md 文件才能构建它).

问:如何添加活动,不能将其添加到清单中?
A:使用 片段,它们不需要清单中的条目.

Q:一个带有要添加到现有资源中的资源的Jar 项目需要能够将其资源与项目的资源合并 自有资源(R.).
A:黑客可用,数据文件...
注意Dalvik已停产

Dalvik的后继产品是Android Runtime(ART),它使用相同的字节码和.dex文件(但不使用.odex文件),其目标是对最终用户透明地提高性能.新的运行时环境作为技术预览首次包含在Android 4.4"KitKat"中,并在以后的版本中完全取代了Dalvik; Android 5.0"Lollipop"是第一个版本,其中ART是唯一包含的运行时.

The short summary is: How do I build an APK and separate libraries (by which I mean sets of classes (and ideally, resources too) in some form, such as JAR, AAR or DEX files), but not include those libraries in the APK; instead, the app loads them at run time?

Detail

So my main question is how to build such an app (e.g. Gradle configuration). How do I specify which classes go into which JAR or DEX files? Do I create an Android Studio module for each DEX file I want to end up with?

A closely related question is how the Java code should then load the external libraries and access their classes at run time. For the latter, I'm hopeful that the approach shown at accessing to classes of app from dex file by classloader would work.

I've tried the instructions at https://developer.android.com/studio/projects/android-library.html, but that builds an APK that does include the dependency library.

I've also tried Multidex (https://developer.android.com/studio/build/multidex.html), but that doesn't seem to leave the developer any control over which classes go in which DEX file, and furthermore, packages them all into a single APK. AFAICT there is no way to control the loading of these DEX files at run time.

Background

There's a possibility of the "X-Y problem" here, so I'd better explain the background.

I'm building an app for a client. It's not going to be distributed through an app store, so it won't have access to the normal mechanism for updates. Instead, the client wants the app to be able to update itself by downloading new components of itself to replace the old components, without a need to manually sideload a new APK. The primary motive here is that the updates have to be easy for non-technical users. If the app can control the update process, it can make it smooth and guide the user.

Moreover, the app will be used in areas where internet access is scarce and expensive, so the client wants to be able to issue app updates in smaller chunks (e.g. 2MB) rather than forcing the user to re-download the whole app to receive a small update.

One aspect of the requirements I should mention, in case it matters, is that the libraries to be loaded at run time are supposed to live on a microSD card. This can also help with distribution of updates without internet access.

The current status of the app is that it's about 50% written: That is, a couple of earlier versions have been released, but the app now needs to be modified (restructured) to meet the above requirements, as well as others.

解决方案

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


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

Q: How do I add an Activity, I cannot add it to the manifest ?
A: Use Fragments, they don't need entries in the manifest.

Q: A Jar with resources that is meant to be added to an existing project needs to be able to merge its resources with the project's own resources (R.).
A: Hacks are available, Data file...
Packaging Android resource files within a distributable Jar file

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

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

This section is for more general users (@LarsH has more specific requirements about updates), The example above is 17kb apk and 1 kb jar. You could put the bulk of you code in the one-off jar, and updates would involve just loading an new Apk (and then importing the bulk code jar, to minimise the data transfer). When the Apk gets too big, start again with a small Apk and everything migrated to another jar (import 2 jar's). You need to balance coding effort, user experience, maintainability, supportability, bandwidth, android rules, play store rules (if these words even exist ;O)).

NOTE Dalvik is discontinued

The successor of Dalvik is Android Runtime (ART), which uses the same bytecode and .dex files (but not .odex files), with the succession aiming at performance improvements transparent to the end users. The new runtime environment was included for the first time in Android 4.4 "KitKat" as a technology preview, and replaced Dalvik entirely in later versions; Android 5.0 "Lollipop" is the first version in which ART is the only included runtime.

这篇关于如何构建APK和分离应用动态加载的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆