Android:从Android代码获取APKs minSdkVersion [英] Android: getting an APKs minSdkVersion from Android code

查看:559
本文介绍了Android:从Android代码获取APKs minSdkVersion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序,如果用户需要,它可以安装其他应用程序(充当我的应用程序的插件).

I'm developing an Android app that has the ability of installing additional apps (which act as plugins for my app) if the user requires it.

但是,这些附加应用程序中的每一个都可能需要特定的Android版本才能运行.我想在运行时进行检查,以查看我要安装的APK是否确实与该设备兼容.

However, each of these additional apps may require a specific Android version to run. I would like to perform a check at runtime to see if the APK I'm trying to install is actually compatible with the device.

现在,使用以下方法:

public PackageManager getPackageArchiveInfo(String archiveFilePath, int flags)

我可以在APK文件上获取信息.但是,问题在于返回的信息似乎仅包含APK的targetSdkVersion,而没有包含minSdkVersion,据我了解,这实际上是确定可以安装/运行应用的最低Android版本的信息.如果我理解正确的话,targetSdkVersion只是最佳"版本.

I can get info on an APK file. However, the problem is that the returned information seems to only include the APK's targetSdkVersion but not the minSdkVersion, which to my understanding is the one that actually determines the minimum version of Android an app can be installed/run on. The targetSdkVersion if I understand correctly is just the "optimal" version.

那么,长话短说,我如何确定APK cna是否在Android本身的设备上运行? (我知道我可以在台式机上使用AAPT,但这在Android本身上不可用)

So, long story short, how can I determine whether an APK cna run on the device from Android itself? (I know I can use AAPT on desktop, but that is not available on Android itself)

推荐答案

您可以做到.

对于Android N及更高版本,请使用 官方API .

For Android N and above, use the official API.

对于早期版本,您可以使用 此代码 ,它非常高效且快捷.这是它的更好的版本(即使getAttributeName返回空字符串也可以使用):

For earlier versions, you can use this code, which is very efficient and fast. Here's a bit better version of it (works even if getAttributeName returns an empty string) :

public static int getMinSdkVersion(File apkFile) throws ClassNotFoundException, IllegalAccessException, InstantiationException,
        NoSuchMethodException, InvocationTargetException, IOException, XmlPullParserException {
    final Class assetManagerClass = Class.forName("android.content.res.AssetManager");
    final AssetManager assetManager = (AssetManager) assetManagerClass.newInstance();
    final Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
    final int cookie = (Integer) addAssetPath.invoke(assetManager, apkFile.getAbsolutePath());
    final XmlResourceParser parser = assetManager.openXmlResourceParser(cookie, "AndroidManifest.xml");
    while (parser.next() != XmlPullParser.END_DOCUMENT)
        if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("uses-sdk"))
            for (int i = 0; i < parser.getAttributeCount(); ++i)
                if (parser.getAttributeNameResource(i) == android.R.attr.minSdkVersion)//alternative, which works most of the times: "minSdkVersion".equals(parser.getAttributeName(i)))
                    return parser.getAttributeIntValue(i, -1);
    return -1;
}

而且,如果您想要一个全方位的解决方案(可悲的是,这可能会占用大量的堆内存和时间),则可以使用APK解析库,例如> 此处 > .

And, if you want an all around solution (which sadly can take a lot of heap memory and time), you can use an APK parsing library, such as APKParser. If you want just the basics of it, consider the APKParser improvement I suggested here.

这篇关于Android:从Android代码获取APKs minSdkVersion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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