如何在Android本身中共享使用即时运行时创建的拆分APK? [英] How to share split APKs created while using instant-run, within Android itself?

查看:96
本文介绍了如何在Android本身中共享使用即时运行时创建的拆分APK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序( 此处 > ),该功能除其他功能外,还可以共享APK文件.

I have an app (here) that, among other features, allows to share APK files.

为此,它通过访问packageInfo.applicationInfo.sourceDir的路径到达文件(文档链接 此处 ).

In order to do so, it reaches the file by accessing the path of packageInfo.applicationInfo.sourceDir (docs link here), and just shares the file (using ContentProvider when needed, as I've used here).

在大多数情况下,这都可以正常工作,尤其是从Play商店或独立APK文件安装APK文件时,但是当我使用Android-Studio本身安装应用程序时,在此路径上看到多个APK文件,但没有它们是有效的,可以安装并运行而没有任何问题.

This works fine in most cases, especially when installing APK files from either the Play Store or from a standalone APK file, but when I install an app using Android-Studio itself, I see multiple APK files on this path, and none of them are valid ones that can be installed and run without any issues.

下面是此文件夹中内容的屏幕截图,是从 "Alerter" github存储库中尝试了一个示例之后 :

Here's a screenshot of the content of this folder, after trying out a sample from "Alerter" github repo :

我不确定何时开始出现此问题,但至少在装有Android 7.1.2的Nexus 5x上确实会发生此问题.也许甚至以前.

I'm not sure when this issue has started, but it does occur at least on my Nexus 5x with Android 7.1.2. Maybe even before.

这似乎仅是由于在IDE上启用了即时运行这一事实造成的,因此它可以帮助更新应用程序而无需一起重新构建它:

This seems to be caused only from the fact that instant run is enabled on the IDE, so that it could help updating the app without the need to re-build it all together :

禁用它后,我可以看到只有一个APK,就像过去一样:

After disabling it, I can see that there is a single APK, just as it used to be in the past:

您可以看到正确的APK和拆分后的APK之间文件大小的差异.

You can see the difference in file size between the correct APK and the split one.

此外,似乎还有一个API可以获取所有拆分的APK的路径:

Also, it seems that there is an API to get the paths to all of the splited APKs:

https://developer.android.com/reference/android/content/pm/ApplicationInfo.html#splitPublicSourceDirs

共享被拆分为多个APK的最简单方法应该是什么?

What should be the easiest way to share an APK that got to be split into multiple ones ?

真的需要以某种方式合并它们吗?

Is it really needed to somehow merge them?

根据 文档,似乎有可能 :

与零个或多个拆分的APK结合使用时的完整路径 在sourceDir中定义的基本APK,形成一个完整的应用程序.

Full paths to zero or more split APKs that, when combined with the base APK defined in sourceDir, form a complete application.

但是正确的方法是什么,有没有一种快速有效的方法?也许没有真正创建文件?

But what's the correct way to do it, and is there a fast and efficient way to do it? Maybe without really creating a file?

是否可能有一个API可以从所有拆分的APK中获取合并的APK?还是这样的APK可能已经存在于其他路径中了,所以不需要合并吗?

Is there maybe an API to get a merged APK out of all the split ones? Or maybe such an APK already exist anyway in some other path, and there is no need for merging?

刚刚注意到,在这种情况下,我尝试过的所有第三方应用程序都应该共享已安装的应用程序的APK.

just noticed that all third party apps that I've tried are supposed to share an installed app's APK fail to do so in this case.

推荐答案

我是Android Gradle插件的技术负责人@Google,假设我了解您的用例,让我尝试回答您的问题.

I am the tech lead @Google for the Android Gradle Plugin, let me try to answer your question assuming I understand your use case.

首先,一些用户提到您不应共享启用了InstantRun的内部版本,并且它们是正确的.在应用程序上构建的Instant Run是针对要部署到的当前设备/仿真器映像高度定制的.因此,基本上来说,假设您为运行21的特定设备生成了启用了IR的应用,如果尝试在运行23的设备上使用这些完全相同的APK,那么它将失败,我会深入探讨.足以说我们生成了在android.jar中找到的API上自定义的字节码(这当然是特定于版本的).

First, some users mentioned you should not share your InstantRun enabled build and they are correct. The Instant Run builds on an application is highly customized for the current device/emulator image you are deploying to. So basically, say you generate an IR enabled build of your app for a particular device running 21, it will fail miserably if you try to use those exact same APKs on say a device running 23. I can go into much deeper explanation if necessary but suffice to say that we generate byte codes customized on the APIs found in android.jar (which is of course version specific).

因此,我认为共享这些APK不合理,您应该使用IR禁用的版本或发行版本.

So I do not think that sharing those APKs make sense, you should either use a IR disabled build or a release build.

现在,对于某些细节,每个切片APK都包含1个以上dex文件,因此从理论上讲,没有什么可以阻止您解压缩所有这些切片APK,将所有dex文件放回基础中.apk/rezip/sign,它应该可以正常工作.但是,它仍然是启用了IR的应用程序,因此它将启动小型服务器以侦听IDE请求等...我无法想象这样做的充分理由.

Now for some details, each slice APK contains 1+ dex file(s), so in theory, nothing prevents you from unziping all those slice APKs, take all the dex files and stuff them back into the base.apk/rezip/resign and it should just work. However, it will still be an IR enabled application so it will start the small server to listen to IDE requests, etc, etc... I cannot imagine a good reason for doing this.

希望这会有所帮助.

这篇关于如何在Android本身中共享使用即时运行时创建的拆分APK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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