0

I'm just starting with Android, and I have a question about why I have to choose an API level when I create a new project. I know that different APIs have different methods, classes and so on, but why do I have to choose one level? Couldn't the compiler determine the minimum SDK needed watching which methods I used in the project?

zer0uno
  • 6,150
  • 10
  • 47
  • 70
  • but how would phone(device) determine which sdk is required for you application which is in the form of compiled apk to run ? – dex Oct 11 '15 at 14:52
  • I mean, the compiler could determine the minimum sdk needed and it could save it in automatically somewhere in the apk – zer0uno Oct 11 '15 at 14:54

2 Answers2

2

Compiler can determine that which devices will be supported by APIs you used. but there will be no way for compiler to warn you if you want to build your app for a lower version and using APIs from higher version.

so when you define a minimum SDK version compiler knows what are you targetting for and warns you if you go above that.

and if you don't define minSdkVersion compiler will assume its 1.

from the documentation:

android:minSdkVersion

An integer designating the minimum API Level required for the application to run. The Android system will prevent the user from installing the application if the system's API Level is lower than the value specified in this attribute. You should always declare this attribute.

Caution: If you do not declare this attribute, the system assumes a default value of "1", which indicates that your application is compatible with all versions of Android. If your application is not compatible with all versions (for instance, it uses APIs introduced in API Level 3) and you have not declared the proper minSdkVersion, then when installed on a system with an API Level less than 3, the application will crash during runtime when attempting to access the unavailable APIs. For this reason, be certain to declare the appropriate API Level in the minSdkVersion attribute.

Rahul Tiwari
  • 5,931
  • 2
  • 41
  • 66
  • but compiler can throw some exception rite, from where I come to know ok minimum sdk required to run this particular app is this – dex Oct 11 '15 at 14:56
  • that will happen on run time. as at compile time compiler will not know what you want to target for and will decided on the basis of APIs used – Rahul Tiwari Oct 11 '15 at 14:58
  • so the original question remain opened, why is it necessary to declared a min sdk in android manifest – dex Oct 11 '15 at 15:00
  • to inform you about which APIs you should not use if you want to target a specific version. – Rahul Tiwari Oct 11 '15 at 15:01
0

Basically

Here's a nice explanation of compileSdkVersion: source.
So compileSdkVersion, chosen by a developer, points what files will be used to compile with your code.
For example:
API 19: Android 4.4 (KitKat)=> files from ...\sdk\sources\android-19
API 22: Android 5.1 (Lollipop) => files from ...\sdk\sources\android-22

Influence

It determines:
- what functionality will be available to you while you're developing (in general, higher version => more features you can use)
- what external libraries can be exploited in your project

Choice

As a rule of thumb: compiledSdkVersion should be the latest possible

Personal experience: sometimes it's very useful to compile your project against API version your device has, especially when you debug, because different APIs => different source files => your breakpoints might not be hit and on the whole the highlighted line in your IDE (when you suspend application) probably won't reflect the state of a program.

Community
  • 1
  • 1
Vitaly Zinchenko
  • 4,547
  • 4
  • 32
  • 49