久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何構建應用程序動態加載的 APK 和分離庫

how to build an APK and separate libraries that the app loads dynamically(如何構建應用程序動態加載的 APK 和分離庫)
本文介紹了如何構建應用程序動態加載的 APK 和分離庫的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

簡短的總結是:如何構建 APK 和單獨的庫(我的意思是某種形式的類集(理想情況下,還有資源),例如 JAR、AAR 或 DEX 文件),但不包括那些APK 中的庫;相反,應用會在運行時加載它們?

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?

所以我的主要問題是如何構建這樣的應用程序(例如 Gradle 配置).如何指定哪些類進入哪些 JAR 或 DEX 文件?我是否要為每個想要結束的 DEX 文件創建一個 Android Studio 模塊?

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?

一個密切相關的問題是 Java 代碼應該如何加載外部庫并在運行時訪問它們的類.對于后者,我希望 通過類加載器從 dex 文件訪問應用程序的類 會起作用.

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.

我已經嘗試了 https://developer.android.com 上的說明/studio/projects/android-library.html,但這會構建一個確實包含依賴庫的 APK.

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.

我也嘗試過 Multidex (https://developer.android.com/studio/build/multidex.html),但這似乎并沒有讓開發人員對哪些類進入哪個 DEX 文件有任何控制權,而且,將它們全部打包到一個 APK 中.AFAICT 無法在運行時控制這些 DEX 文件的加載.

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.

這里可能存在XY 問題",所以我最好解釋一下背景.

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

我正在為客戶構建應用程序.它不會通過應用商店分發,因此無法訪問正常的更新機制.相反,客戶端希望應用程序能夠通過下載自身的新組件來替換舊組件來更新自身,而無需手動旁加載新的 APK.這里的主要動機是更新必須對非技術用戶很容易.如果應用程序可以控制更新過程,它可以使其流暢并引導用戶.

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.

此外,該應用程序將用于互聯網訪問稀缺和昂貴的地區,因此客戶希望能夠以較小的塊(例如 2MB)發布應用程序更新,而不是強迫用戶重新下載整個應用程序接收一個小的更新.

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.

我應該提到的要求的一個方面是,在運行時要加載的庫應該存在于 microSD 卡上.這也有助于在沒有互聯網訪問的情況下分發更新.

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.

應用程序的當前狀態是編寫了大約 50%:也就是說,已經發布了幾個早期版本,但是現在需要對應用程序進行修改(重組)以滿足上述要求以及其他要求.

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.

推薦答案

本教程是外部加載 DEX 文件的良好開端.只有三個小源文件(MainActivity.java、LibraryInterface.java、LibraryProvider.java)并將secondary_dex.jar從assets文件夾復制到內部應用程序存儲[outdex/dex](教程中也盡可能地說明了互聯網)).您必須使用 ant 構建它,因為它使用自定義構建步驟.我試過了,效果很好.值得一看.
Dalvik 和 ART 中的自定義類加載

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


更新此代碼已移植到 Android Studio gradle(不需要 ant).https://github.com/timrae/custom-class-loader
測試正常.將 com.example.toastlib.jarSD 卡 復制到 內部應用程序存儲 [outdex/dex],(不是 assets 文件夾).(您必須閱讀項目中的 README.md 文件來構建它).


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

問:如何添加 Activity,我無法將其添加到清單中?
答:使用片段,它們不需要清單中的條目.

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.

問:一個帶有資源的 Jar,旨在添加到現有的項目需要能夠將其資源與項目的資源合并自有資源(R.).
A:黑客可用,數據文件...
在可分發的 Jar 文件中打包 Android 資源文件

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

問:外部文件的權限錯誤.
A:導入它.

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

問:我需要添加使用權限.
A:使用 API23,您可以通過編程方式添加使用權限(但它們仍然需要在 Manifest 中聲明,因此 新權限模型對我們來說可能沒多大用處.

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

此部分適用于更一般的用戶(@LarsH 對更新有更具體的要求),上面的示例是 17kb apk 和 1 kb jar.您可以將大部分代碼放在一次性 jar 中,更新只需加載一個新的 Apk(然后導入批量代碼 jar,以最大程度地減少數據傳輸).當 Apk 變得太大時,從一個小 Apk 重新開始,所有內容都遷移到另一個 jar(導入 2 個 jar).您需要平衡編碼工作量、用戶體驗、可維護性、可支持性、帶寬、android 規則、play store 規則(如果這些詞甚至存在的話;O)).

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

注意 Dalvik 已停產

Dalvik 的繼承者是 Android Runtime (ART),它使用相同的字節碼和 .dex 文件(但不是 .odex 文件),其繼承旨在提高對最終用戶透明的性能.新的運行時環境首次作為技術預覽包含在 Android 4.4KitKat"中,并在以后的版本中完全取代了 Dalvik;Android 5.0Lollipop"是第一個僅包含 ART 運行時的版本.

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 和分離庫的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

IncompatibleClassChangeError after updating to Android Build Tools 25.1.6 GCM / FCM(更新到 Android Build Tools 25.1.6 GCM/FCM 后出現 IncompatibleClassChangeError)
How to get current flavor in gradle(如何在 gradle 中獲取當前風味)
How to fix quot;unexpected element lt;queriesgt; found in lt;manifestgt;quot; error?(如何修復“意外元素lt;查詢gt;在“清單中找到錯誤?)
Multi flavor app based on multi flavor library in Android Gradle(基于 Android Gradle 中多風味庫的多風味應用)
Android dependency has different version for the compile and runtime(Android 依賴在編譯和運行時有不同的版本)
Transitive dependencies for local aar library(本地 aar 庫的傳遞依賴)
主站蜘蛛池模板: 国产一区二区不卡 | 天天久久 | 欧美日韩一区二区视频在线观看 | 日韩精品一区二区三区 | 国产精品区二区三区日本 | 国产精品亚洲一区二区三区在线观看 | 国产成人精品免费 | 深夜爽视频 | 黄色毛片在线看 | 日日射影院 | 性一交一乱一透一a级 | 久久麻豆精品 | 99精品欧美一区二区三区综合在线 | 一区二区三区在线免费观看 | 日本偷偷操 | 国产a区| 无吗视频| 九色av| 99re视频在线 | 99视频免费看 | 亚洲性视频网站 | 成人二区 | 亚洲二区在线 | 人人爽日日躁夜夜躁尤物 | 天天久久 | 成人精品一区二区 | 欧美黑人狂野猛交老妇 | 久久精品手机视频 | 精品亚洲二区 | 亚洲在线一区二区 | 天天激情综合 | 亚洲国产精品一区 | 一区二区亚洲 | 国产中文视频 | 在线观看免费av网 | 日韩欧美精品在线 | 日韩一区二区在线免费观看 | 日韩三级 | 一区二区三区欧美 | 久久久久久久国产精品 | 欧美日韩国产欧美 |