Difference between “Build Target SDK” in Eclipse a

2020-01-30 06:58发布

As I'm importing an existing Android project into Eclipse, I am asked to select an SDK build target as part of the process.

Why do I need to enter this information? How is this different from the android:targetSdkVersion/android:minSdkVersion values specified in AndroidManifest.xml?

For example, in the Google IO sample app, its AndroidManifest says android:targetSdkVersion=11, but the README says the Eclipse project needs to target API level 13 or higher, or compile errors will occur.

标签: android
7条回答
Fickle 薄情
2楼-- · 2020-01-30 07:21

Here is a description of each attribute, exactly what it controls, and how you should use it.

In AndroidManifest.xml:

  • midSdkVersion: Used by the Google Play Market. The lowest API on which you will allow your app to run. Devices running a version of Android older than this will not be allowed to install your app. Set this to the lowest version of android that your code can support (i.e. it doesn't use any API calls newer than this without paying special attention to backward compatibility). Of course, you should test on a device or emulator running this version.

    Google provides a dashboard giving you a breakdown of the number of people using each version, which can be useful when deciding whether it's OK to stop supporting one.

    Note: If you are using any of the android-support libraries, you should not use an older version than indicated in the name of the support library. For example, android-support-v4.jar will not run on versions of Android older than 4.

  • targetSdkVersion: Used by devices at run time. Devices use this to decide whether to run your app in a backward compatibility mode. For example, if you set this to 10 (Gingerbread) devices that run 16 (Jelly Bean) will still use the visual styling of Gingerbread for your app; e.g. it will have a title bar instead of an action bar. Set this to the newest version of Android that you want your app to look like (which you can only determine by testing it on newer versions to see if it looks good and behaves well).

In project.properties, or set via Eclipse's Project Build Target setting:

  • target: Used by your computer at compile time. The version of Android that your app is compiled against. Trying to use API features newer than this will cause an error. You should set this to the same as minSdkVersion unless you do special things for backward compatibility (see below), so that the compiler will prevent you from accidentally using features that don't exist on your users' older devices (causing it to crash).

    Note: It is possible for library projects you include to require a minimum for this value. For example, android-support-v7-appcompat includes .xml resource files in res/layout-v14 that require you to compile against API 14 or newer.

A note on backward compatibility:

There is a case when project.properties should be higher than minSdkVersion: when you want to use newer API features, but you include special code for backward compatibility to allow it to run on older devices. In this case you will have to bump project.properties up to the match the oldest API that contains all the features you use.

When possible, use the Android Support Libraries to accomplish backward compatibility, because it is conventional, well tested, easy, and allows you to leave project.properties alone. However, sometimes the support libraries do not meet your needs, in which case you will have to use techniques such as this or these

查看更多
贼婆χ
3楼-- · 2020-01-30 07:25

AFAIK, the android:minSdkVersion and the one that the Eclipse asks you to select while setting up a project are one and the same. However, the android:targetSdkVersion is where you'd want your app to target a specific sdk version of Android available in the market.

For example, you might have your android:minSdkVersion="8" (the one that Eclipse asks you during setting up of your project) because you'd want your app to run on devices that have Froyo also (along with higher versions of Android). But you might want your app to truly target GingerBread users or HoneyComb users or ICS users.

EDIT : Please do keep in mind that your targetSdkVersion has to be equal to or more than that of the minSdkVersion. Otherwise, it doesn't really make much sense.

查看更多
爷、活的狠高调
4楼-- · 2020-01-30 07:28

android:minSdkVersion in manifest file means that market will filter devices with lower sdk.

target=android-x in project properties file means that Eclipse will not allow use methods or classes from sdk higher than x. It will show compiler errors.

You can use it like this: provide min version in manifest - depending on your app critical features. Set the same value to project properties. Then, if you want use somewhere APIs from higher SDKs - raise up value in project properties, and wrap code with check if device API can do this code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR_MR1)
{
    // here you can use APIs, that appears in Android 2.1
}
查看更多
我想做一个坏孩纸
5楼-- · 2020-01-30 07:30

I'm not saying Eclipse's build target should be set to something less than minSdkVersion. I'm asking why Eclipse doesn't automatically use either minSdkVersion or targetSdkVersion as its build target SDK instead. Why would you ever want to set it to something different?

call it a feature. eclipse is not made exclusively for android. if nobody thought of adding a automatism to use the sdk version given in the manifest, then eclipse simplely wont do it. i dont think there's much magic behind this. i think it has just not been added yet to eclipse / android plugin for eclipse features.

also, since you allways may change the sdk version stated in the manifest, it makes perfectly sense to also be flexible in eclipse (you always can change the eclipse sdk target via the properties of your project). like others posted there are times when the manifest sdk may differ from the eclipse sdk. (multi sdk-version support). using a lower then the min-sdk in manifest makes no sense of course but here gain: if nobody wrote yet a checker for the eclipse plugin that checks if what you're doing makes sense, then it simply will be possible.

查看更多
唯我独甜
6楼-- · 2020-01-30 07:39

Guys I done some testing on these and find below answer:

minSDKVersion - application will not run below that sdk

targatSDKVersion - application runtime environment. For ex application have latest SDK in device let say 21 but you set target sdk version 19 it means when application run in this device the runtime (themes and other UI/Classes) will be 19.

Project Build Target - When you select a project build target version it means you are apk or classes will be compiled according to selected sdk. for ex - if you select project build target 16 and try to use anotation @JavaScriptInterface it will not find because this anotation is available in above that target.

maxSDKVersion - It means application will not install on above that sdk.

查看更多
家丑人穷心不美
7楼-- · 2020-01-30 07:41

Every android versions are assigned to the android:targetSdkVersion. The higher version of project cannot run on a lower version of emulator but the viceversa is possible. make sure your sdk is updated and try changing the project to the corresponding version

查看更多
登录 后发表回答