My question popped up a very similar question, this one. But the accepted answer (the single one) points to another question, this one, which doesn't really answer the original question.
The Android documentation states:
The Build Target specifies which Android platform you'd like your
application built against.
But what does it mean really?
The way I see it, I can have the minSdkVersion=4 and targetSdkVersion=10 but set the build target to API Level 4. What will happen? Eclipse assumes I'm developing for API Level 4 and any method, constant or whatever defined on API Levels above 4 will not be available to me. If I try to use them, the application will not compile. I'm aware of this.
But let me put it differently...
Let's say I have only set minSdkVersion=4, targetSdkVersion is not defined. I am also not using any method or constant only available on API Levels above 4. In this situation, does it really matter the build target I pick? Will it have any impact in the final APK?
Build target
Build target is the API level Eclipse/IntelliJ/whatever IDE you’re using is building against. This
is simply used by the IDE/build system to know which APIs to offer
you. If you build against API level 14, the application will still be
able to run on API level 7, providing you don’t call any APIs that are
not available on API level 7.
I mostly set the build target to the same as android:targetSdkVersion,
though this is not required.
Source: http://simonvt.net/2012/02/07/what-api-level-should-i-target/
If you use a higher build target then you can write code that will work on earlier versions by using reflection, for example. If you want to be restricted to just API 4 then don't worry about the build target.
For an example of targeting earlier api levels when compiling for a higher one you can look at this question:
Android: how to code depending on the version of the API?
The way I see it, I can have the minSdkVersion=4 and targetSdkVersion=10 but set the build target to API Level 4. What will happen? Eclipse assumes I'm developing for API Level 4 and any method, constant or whatever defined on API Levels above 4 will not be available to me. If I try to use them, the application will not compile.
When you set the build target to API level 4, Eclipse will not let you compile any methods you use higher than that, because it strictly uses API level 4. However, when you put the build target to a higher API level, in your case API level 10, your APK is available for use for phones from API level 4 to 10.
The 2nd question's answer answers your question, that is the Android build target, both minSdkVersion and targetSdkVersion affects the range of users that are able to use your application.
EDIT:
Since you're not going to define targetSdkVersion and you're not using any features which is above API level 4, the targetSdkVersion will be the same as minSdkVersion. Whatever build target your chose will automatically be specified. It doesn't really matter which build target you pick unless it is below API level 4
From Android documentation of targetSdkVersion:
An integer designating the API Level that the application targets. If not set, the default value equals that given to minSdkVersion.
This attribute informs the system that you have tested against the target version and the system should not enable any compatibility behaviors to maintain your app's forward-compatibility with the target version. The application is still able to run on older versions (down to minSdkVersion).
Related
Is there any impact of lowest (minimum sdk) and highest (target sdk) levels in the android project. Is these things may effect the project reliability and efficiency.
There is no impact, If you target SDk level 8 then your app will target 99.8 % of the mobiles make sure you use V7 support jar to make you development process easier. Since no new popular mobiles have 23 yet you can target it to be ready for the future but Make sure you dont add unwanted permissions as it will make you life difficult also ensure you handle the scenario if permission is declied.
Yes, the targets are there to filter out what your app caters to which android versions. Different SDK levels has different set of new/updated APIs and depreciated APs.
The lower u go , the more devices you are able to target.
More info: https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels
The impact is only in what features you are able to use. For example API 8 doesn't support animations that were introduced in Honeycomb, API 22 doesn't support the optional permissions introduced in Marshmallow etc.
It is still possible to use these features in your app using the TargetApi annotation, but you have to be sure that they will not be run on older devices, usually by using a statement such as this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
// JellyBean specific code goes here
}
else
{
// Pre-JellyBean code goes here
}
Well, I think that is not well explained and right now I'm a little bit confused. From this link:
Compile With is the platform version against which you will compile
your app. By default, this is set to the latest version of Android
available in your SDK. (It should be Android 4.1 or greater; if you
don't have such a version available, you must install one using the
SDK Manager). You can still build your app to support older versions,
but setting the build target to the latest version allows you to
enable new features and optimize your app for a great user experience
on the latest devices.
From this one instead:
When you are developing your application, you will need to choose the
platform version against which you will compile the application. In
general, you should compile your application against the lowest
possible version of the platform that your application can support.
Now, as they are contradicting themselves, here I'll explain what I've understood: let's say that we compile with api 5. It means that if I run from 1 to 5, my application works but it doesn't for versions > 5. So:
Why I need to set the minSdkVersion if compileWith is set to 5?
What is targetSdkVersion?
The point is: if compileWith is set to the latest version, is it possible to use latest APIs and still have backward compatibility? I'm sure that I'm wrong and I'm missing something. Thank you.
Edit 1
Now I understand more but anyway I'm missing something: let's say that minSdk is 9 and target is 18 (my current sdk). It means that I cannot use anything specific to api 18 unless there's some support package for older versions, right? I'm thinking different: I would compile with the lowest version possible to get forward compatibility. I mean, if you compile a Windows application on Win2000, it will run till Windows 8 but I'm limited to Win2000 apis. If I compile with Windows 8, the backward compatibility should be checked on runtime and I can use Windows 8 apis on Win2000 only with "support packages" (it's an example, obviously). Right?
Edit 2
This is what I'm asking about Android (not about the JVM). Android is mixing forward with backward and I've mixed it more using the Windows example. FIY, It seems that Windows is backward compatible, as explained in the examples here. So, what about Android? It seems that the minSdk version should be tested from the developer, I mean: I should compile with latest version and test my application behaviour till the minimum api level that doesn't change it. So it seems that I need to test the backward compatibility by testing on every api level lower than the one with wich is compiled and they assure the forward compatibility. Right?
I think that I'm mixing forward and backward compatibility definitions. Everytime I read them they seem different.
from the Documentation you can read the following:
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.
and
android:targetSdkVersion
An integer designating the API Level that the application targets. If not set, the default value equals that given to minSdkVersion.
This attribute informs the system that you have tested against the target version and the system should not enable any compatibility behaviors to maintain your app's forward-compatibility with the target version. The application is still able to run on older versions (down to minSdkVersion).
And to answer your last question, targeting the latest API will not allow you to execute any newer method or use any newer class on old phones, for that you need to use the support packages, with that you are able to obtain a close result to what you can do in newer APIs.
Hope this gives you a more clear idead of why you have those two values and the difference between them.
UPDATE:
Well about the example of something built for windows 2000 and working on windows 8, is not guarantee that it will work, that is why on windows you have the "run with compatibility" mode, and then you select according to what operating system you want to run it.
This is because maybe both OS (or APIs in this case) have the same method or option, they do it in a different ways or need other permissions. The same happens here, so the target SDK will be the reference frame against you tested and thus it will use that behavior if you run it on newer devides than the taget SDK.
For the APIs between the min and the target they will run as normally they do, and so you can always have a weird behavior and some patching might be need it, normally is most visible on the 2.x to 3.x jump.
UPDATE 2:
Well, let me try to explain it in a different way.
The minSDK will determine which methods and options you can use throughout the application, this means that if a method is only available after that API, you will not be able to use it and instead get an error message.
The targetSDK represents the lastest version of the API that you tested and so, if the phone has a newer API than the one you specified, it will try to behave as it was on the API version you specified, take for example this, in the section Important Behavior Changes:
This inexact batching behavior applies only to updated apps. If you've set the targetSdkVersion to "18" or lower, your alarms will continue behave as they have on previous versions when running on Android 4.4.
So, as you can see there, if you set the targetSDK to a previous API (before 19), it will behave as it use to, but if you target the newest API (at this moment API 19), then it will behave differently.
Hope i was clear enough, if not, please do not hesitate and ask.
I am building an Android application which I compile against API-17: project.properties contains
target=android-17
I am slightly confused because I can download and execute the application on an Android phone running Gingerbread 3.3.6, API 10. Can perhaps someone clarify?
You are able to run it on API 10 as your android:minSdkVersion would be less than 10. It is
an integer designating the minimum API Level required for the application to run.
See http://developer.android.com/guide/topics/manifest/uses-sdk-element.html
To differ between target in project.properties and android:targetSdkVersion
target=android-sdk version in project.properties file indicates Eclipse will not allow use methods or classes from sdk higher than this one.
android:targetSdkVersion attribute informs the system that you have tested against the target version and the system should not enable any compatibility behaviors to maintain your app's forward-compatibility with the target version. The application is still able to run on older versions
Source: What is the difference between the 'target' settings in the 'project.properties' file and the 'android:targetSdkVersion' tag in the manifest?
Because target=android-17 is compilation target not a MIN-SDK.
Min-sdk version will be Android 2.2 so you are able to download and execute on 3.3.6, API 10
Previous answers are valid.
I'll only add that there's one condition to that: That the code of that application should be written in API-10 compatible code. The application shouldn't call classes or methods that are not defined in API-10 otherwise it will crash when doing so.
Then, Android has many way for developers to work around compatibility, such as the support libraries and detecting device version to write specific compatibility code.
I wanted to know the consequences of having targetSDK > buildTarget.
I recently observed that if I keep the buildTarget=16 and targetSDK=17 the tabs on my tablet (running 4.1.1, API Level 16) moves to the center of the actionBar. I was unable to rationalize the behavior. Can somebody shed some light on why this happened?
Nice question! I had a similar behavior some time ago, when buildTarget and targetSDK differed in the described way. It took me some time, to figure it out, but I will try to summarize my understanding.
You have to distinguish between three important values:
minSdkVersion:
This is the lowest available version, on which the app will (or should!) run. When installing an .apk onto Android, the value will be checked and if the Android version you're running on, is lower than the specified version, it won't install.
buildTarget:
That's the SDK on which the application's .apk will be compiled (and Eclipse will be target that value too, for checking for compilation errors). If the buildTarget is higher than the minSdkVersion, you will be able to install the app even if your Android version does not support all methods. By default, this is set to the latest version of Android available in your SDK. You can still build your app to support older versions, but setting the build target to the latest version allows you to enable new features and optimize your app for a great user experience on the latest devices.
You need to check if the methods you are using are present at runtime if running on a lower API level, otherwise the application might crash!
targetSdkVersion:
The targetSdkVersion specifies on which SDK platform your app should run fine. So, if you tested against API 17, you can add API 17 as targetSdkVersion. If using an Android version > targetSdkVersion, the Android system will enter into some kind of forward-compatibility mode to ensure support for the application. This compatibility behavior will be entered to ensure that your app continues to work the way you expect, as there might be some changes in behavior between never API levels (here are some of the most important changes). So, any application developed for a lower API level will be able to run on a higher version, as the old behavior (like obsolete values) might be "simulated" within the compatibility mode.
For example:
If you set targetSdkVersion to HONEYCOMB (API 11), the default theme will be changed to Theme_Holo (that's the dark holographic UI). Setting targetSdkVersion to a lower value will affect the system to stay on the default light theme, regardless which build API you will use!
In your case, there don't seem to be many noticeable changes between API 16 and 17, that should affect in a design change, but I guess, the higher targetSdkVersion will affect in some additional changes at compile time (like including additional classes, themes, values, ...), that will affect in a different behavior, just like in the theme example above.
I hope, that helped you a bit, to figure out the weird behavior. Here is some more related information to read in the Android Developer documentation.
PS: There is some kind of forward-backward-hell: The Android system is backward-compatible, so that the forward compatibility of Android applications is ensured. That means: If you update your Android version via OTA e.g., all old applications should stay running (so they will stay forward compatible).
The build target is for app development, the target SDK is for app compatibility.
The build target specifies which API you have access to while implementing the app. Like if you set the build taget to android API level 10 then as far as your code is concerned, there is no such thing as an ActionBar. The API you use during development is just a stub implementation of Android, this is way it has to be emulated or run on a real device. Therefore, the build target defines (to the compiler and your IDE) Android interface you are using. Once compiled, there should be no difference based on build target (the Android system doesn't see the build target, it's a compile-time flag). This is a strict contract between you and the android compiler (and your IDE) that defines which components in Android you are able to use in your application, as you will get compilation errors if you try to use something that is beyond the Android version set as your build target.
The target SDK is a contract you sign with the Android system, assuring it that your app is prepared to work properly from you minimum SDK up through to the target SDK (effective the maximum SDK, as the maximum SDK setting should generally be avoided). I believe there are a few things that don't get forward-compatibility, like some of the security changes (probably changes the come from beyond app development and are system-wide). This contract is an agreement that means you have performed measures to make sure that your app handles any changes in the Android API in that range, such that it provides behavior you expect in all situations. The other end of the contract is from the Android system, it agrees to use Android implementation that does not exceed your target SDK, even when on a device running a higher version of Android (this excludes the few changes that I mentioned previously).
The note on forward-compatibility implies that your build target should always at least match your target SDK. You are saying that you have tested your app to run at your target SDK, so why build it against a lower API level?
Real life example of build target and target SDK in action:
ActionBarSherlock provides backward-compatible ActionBar. Here are quotes from requirements to use the library at this time.
The library itself must be built against Android 4.0 (API level 14). Your project should be built using the latest version of the SDK as possible as long as it is 4.0 or newer.
Targetting API level 11 or newer is required as it will cause Android to automatically add the native action bar when run on newer devices. Since you will be compiling against new APIs but your app will likely be run on devices with older versions of Android extra care must be taken to either avoid using or properly check and call any methods that were introduced after your minimum SDK version.
The first paragraph shows that a build target that contains the 4.0 ActionBar API is required, as the library makes use of it and can't compile without it. The second paragraph shows that a target SDK that contains the 3.0 ActionBar API is required as the library uses the native ActionBar on such devices, but the Android system won't provide the ActionBar if your target SDK is lower than 3.0 since that tells it not to use anything newer than your target (like the 3.0 ActionBar).
Some references:
Build Target
Target SDK
I'm working on an application which uses ActionBarSherlock. As it's documentation points out:
[...] the library requires that both it and your project are
compiled with Android 4.0 or newer. The project also requires that
you are compiling with JDK 1.6 in both your editor and any build
systems that you may be using.
So, that means I'll compile my application (and the library) against Android 4.X but in my Manifest, I declare that I'm targeting (e.g.) API Level 9.
This all works fine and well but there is something that disturbs me. From the FAQ:
What API level should I target in my manifest when using the library?
Targetting API level 11 or newer is required as it will cause Android
to automatically add the native action bar when run on newer devices.
Since you will be compiling against new APIs but your app will likely
be run on devices with older versions of Android extra care must be
taken to either avoid using or properly check and call any methods
that were introduced after your minimum SDK version.
That means, that I'll have to manually check every method call, so I don't use any that are not available in my targeted API Level (9 in my case)? This sounds wrong to me.
Is there a way to tell my IDE (IntelliJ), that I'm only using the API Level 9 (so I don't get any auto-completion for non-existing methods/classes and don't use them by accident) and then choose to compile it against another Android version?
Or can I use some automated checks (which run at compile time) to check for that?
The ADT's lint feature should take care of this by warning when API calls are being made for the wrong API version.
You should be compiling both ABS and your project with the latest SDK available (at present, 4.1). Your manifest should have a targetSdkVersion as high as possible (ideally matching your compilation SDK) and your minSdkVersion should be set to the lowest version you support.
Lint is partially integrated with IntelliJ IDEA and is also available as a command line tool.
You temporarily set your target SDK to the various lower ones and debug with it. Your final build then is with the latest SDK.
Set a Build target similar to that you have mentioned in your manifest.
as always , you should set the targetSdk to the maximum available on both the manifest and the project.properties file (as recommended by google on one of their videos) , so that the ADT&SDK would be able to optimize the ADK accordingly.
set the minSdk to the one that you wish to support your app from , and let Lint to tell you if there are any problems in case you use too-new-features.