用更高的sdk编译android项目 [英] Compile android project with a higher sdk

查看:59
本文介绍了用更高的sdk编译android项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 android sdks 的问题.我正在开发一个 Android 应用程序,它必须适用于 Android 2.3.3 及更高版本.

I have a question about android sdks. I'm developing an android application and it must be work on Android 2.3.3 and higher.

因为我需要使用android-support-v7-appcompat"库,所以我需要用Android 4或更高版本编译项目.如果我尝试使用 Android 2.3.3 进行编译,则该项目无法编译.

Because I need to use "android-support-v7-appcompat" library, I need to compile the project with Android 4 or higher. If I try to compile with Android 2.3.3, the project doesn't compile.

那么如果我用 Android 4 编译项目会发生什么?它可以在 Android 2.3.3 上运行吗?

So what happens if I compile the project with Android 4? Would it work on Android 2.3.3?

当然我会在 AndroidManifest 中设置.

Of course I would set in AndroidManifest.

推荐答案

在任何情况下,您都应该在清单文件中正确定义 minSDK 和 targetSDK.例如,当您使用 API 19 (KitKat) 进行编译并设置为 minSDK API 10 时,一切都应该在您的 2.3.3 Android 上运行.

In any case you should properly define minSDK and targetSDK in manifest file. When you compile for example with API 19 (KitKat) and you set as minSDK API 10 everything should work on your 2.3.3 Android.

但是,如果您在代码中使用了一些在 2.3.3 中不可用的方法、常量,您将在 Eclipse 中收到警告/错误(我打赌在 Android Studio 中也是如此).例如:

However if you used in your code some methods, constants that are not available on 2.3.3 you will get warnings/errors in Eclipse (I bet in Android Studio as well). For example:

if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        // before JELLY_BEAN_MR2 code;
        // methods, constants available for example from API 1
    } else {
        // after JELLY_BEAN_MR2 code
        // here are new methods,constants available from GINGERBREAD_MR1 and after
    }

所以在这里,您的应用程序将根据运行的 Android 版本以两种不同的方式执行某些操作...如果您不使用等于或高于 JELLY_BEAN_MR2 的 API 进行编译,您甚至无法编译,因为以前, 常量 JELLY_BEAN_MR2 在 API 中不可用......但是,当您使用更高的 API 进行编译时,一切都会被正确编译,并且应用程序可以在早期版本上正常运行而不会出现问题.

so here your app will execute something in a two different way depending on Android version it is running on... In case you don't compile with API equals or higher of JELLY_BEAN_MR2 you would not even be able to compile because before, constant JELLY_BEAN_MR2 was not available in API... However when you compile with higher API everything will be compiled properly and app will work on earlier versions without problems.

如果你留下这样的代码会出现警告,但@SuppressLint("NewApi") 在调用方法之前应该删除它,不会有进一步的问题.

If you leave code like this warning will appear but @SuppressLint("NewApi") before calling method should remove it with no further problems.

希望它有所帮助,现在很清楚了.. ;)

Hope it helped and it is clear now.. ;)

干杯

这篇关于用更高的sdk编译android项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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