简体   繁体   English

在应用程序上针对不同的Android版本创建-在较低版本中可用,仅在较高版本中导入

[英]creating on app for different Android version - available on lower versions with imports only in higher version

I know this kind of question has been asked before, for example here: Android: how to code depending on the version of the API? 我知道以前曾问过这种问题,例如在这里: Android:如何根据API版本进行编码?

There it is also mentioned that one has to compile to the newest Android level. 还提到了必须将其编译到最新的Android级别。 My basic app version should be available with 7 and the more advanced starting with level 12. 我的基本应用程序版本应从7开始提供,而更高级的版本应从12级开始。

So does that mean that the target setting of the project should be 12 as well as the min-sdk in the manifest. 这是否意味着项目的目标设置应该与清单中的min-sdk一样为12。

BUT the app in the Android market will then ONLY be available to level 12 devices? 但是,只有12级设备可以使用Android市场中的应用吗? Even though the app should run starting with 7 - just with limited features. 即使该应用程序应从7开始运行-功能也有限。

Also in level 12 I would need to import certain packages only available starting at 12. So I could not even set a lower Android target, otherwise I get compiler error. 同样在12级中,我将需要导入仅从12开始可用的某些软件包。因此,我什至无法设置较低的Android目标,否则会出现编译器错误。

So in summary: 因此,总而言之:

  1. App should be available starting with level 7 应用应该从7级开始可用
  2. If the user has level 12+ then the app should import certain level 12+ packages and make available these features 如果用户的级别为12+,则应用程序应导入某些级别12+的软件包并提供这些功能
  3. If the user has only < 12, then the packages should not import 如果用户只有<12,则不应导入软件包

Many thanks! 非常感谢!

If I'm reading your question correctly then this would allow you application to be installed on devices with level 7 如果我正确阅读了您的问题,则可以将应用程序安装在7级级别的设备上

<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="12" />

For development you can set it to any target you want ie your project target can be SDK 4.0 but you manifest target can be 1.5 (android:targetSdkVersion="3") or vice versa. 对于开发,您可以将其设置为所需的任何目标,即项目目标可以是SDK 4.0,但清单目标可以是1.5(android:targetSdkVersion =“ 3”),反之亦然。 Eclipse will give you a warning but you can develop for any target and set you manifest to any target as long as you take in to account the consequences. Eclipse会警告您,但是您可以针对任何目标进行开发,并且只要考虑到后果,就可以将清单设置为任何目标。

You are probably aware there is a bit of work to do in you code to make sure the application doesn't crash if it tries to call methods that don't exist at level 7. I have written code to handle that if you want some examples. 您可能已经知道代码中需要做一些工作,以确保应用程序在尝试调用7级不存在的方法时不会崩溃。如果您需要一些代码,我已经编写了代码来处理例子。

EDIT: 编辑:

An example that works for me. 一个对我有用的例子。

/**
 * Get the current SDK version as an integer. If we are using 1.5 the SDK is
 * returned as a String so using the Build.VERSION.SDK_INT method will cause
 * the application to crash. This method will return 3 if the version is 1.5
 * and will return the proper result for Build.VERSION.SDK_INT if that
 * method is available.
 * 
 * @return
 */
public static int getSdkInt() {
    if (Build.VERSION.RELEASE.startsWith("1.5"))
        return 3;

    try {
        return YourInternalClass.getSdkIntInternal();
    } catch (VerifyError e) {
            Log.e(TAG, e.toString());
        return 3;
    }
}

You should call any methods that may not exist in an internal class so it wont load up until the method is called. 您应该调用内部类中可能不存在的任何方法,这样在调用该方法之前,它不会加载。

private static class YourInternalClass {
    private static int getSdkIntInternal() {
        return Build.VERSION.SDK_INT;
    }
}

Now you can check the SDK version. 现在,您可以检查SDK版本。 If you have any methods that exist only in packages greater than 7, put those methods in an internal class. 如果有任何方法仅存在于大于7的程序包中,请将这些方法放在内部类中。 then you can do 那你就可以

if(TheClassYouPutThisMethodIn.getSDKInt() >= 12)
YourInternalClass.yourLevel12Method();

I'm sure there is probably a better way to do this (and I hope someone could post it) but it works great for me. 我确信可能有更好的方法可以做到这一点(我希望有人可以发布它),但对我来说很好。 I have applications that use methods from SDK 13 running safely on phone with level 3. 我的应用程序使用SDK 13中的方法在3级手机上安全运行。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 较低版本的应用未在android的较高版本中运行 - Lower version app not running in the Higher version in android 如何在Android中实现从较高版本引入到较低版本的功能 - How to implement the feature introduced in higher version to lower version in Android Android App无法在更高版本上运行 - Android App not running on higher version 如何允许较低的 Android API 级别用户下载较高 API 级别版本的应用程序? - How can I allow lower Android API level users to download my app in a higher API level version? 应用程序在较高版本的操作系统上崩溃,但在较低版本上不会 - app crashing on higher OS versions, but not on lower versions 如何根据Android版本拥有不同的应用程序图标版本? - How to have different app icon versions according to the version of Android? 如何在较低版本中运行较高版本的应用程序? - How to run Higher version application in lower version? "Ionic 4 应用程序未在 Android 9 和低于 7 的 Android 版本中运行" - Ionic 4 app not running in Android 9 and Android version lower then 7 Android:针对较低的OS版本使用其他样式 - Android: Use different style for lower OS version android服务可在2.2版中使用,但在更高版本中会失败 - android service works in version 2.2 but fails in higher versions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM