Reputation: 7
I read about targetSdkVersion in the Documentation and I know what targetSdkVersion is. But my question is when targetSdkVersion is 19 and if minSdkVersion is 8.. Now if my app uses API 16 in some parts of the code, does my app run in android 2.3 ?
Upvotes: 1
Views: 629
Reputation: 1302
No forget about running, if ur using the API's available for API level 16 and not available on API level 2.3. First thing is Eclipse will give u an ERROR stating that ur minimum sdk ver is 8 where in the API(a method or a function) which ur using is not available for sdk ver 8.
So u can check ur sdk version and make decisions based on that. This is also applicable the vice versa , that is suppose the method which u will be using is deprecated will be shown as a warning to u in Eclipse. That particular functionality may work upto some sdk version and then it might completely lose its functionality after some API levels and the support is completely stopped in the upcoming API level.
Here is a small example of how to make use of this check:
private int currentApi = Build.VERSION.SDK_INT;
if(currentApi >= 16){
btnIconTxt.setBackground(new ColorDrawable(Color.parseColor(btnColor)));
}
else
btnIconTxt.setBackgroundDrawable(new ColorDrawable(Color.parseColor(btnColor)));
The setBackgroundDrawable(Drawable)[this method wont work properly on KITKAT] is deprecated from API level 16, so a precheck and using appropriate methods is better. If the API level is greater than or equal to 16 then go for the first one in this case.
Upvotes: 0
Reputation: 17879
Your app will still run BUT you have to check the Android Version before using the specific method. Android Studio generally prevent you about possible issue. If you're using a API 16 method on API8, is likely the app will crash. It will throw a NoSuchMethodException or a ClassNotFoundException (depends). This is why you have to check the version running to prevent crash.
Example
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN){
view.setBackground(drawable);
} else {
view.setBackgroundDrawable(drawable);
}
You can also use Annotation to specify a method that will run on a specific version and above :
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void setBackground(View view, Drawable drawable){
view.setBackground(drawable);
}
Upvotes: 3
Reputation: 11439
The short answer is: yes. The long is, only if you don't use higher level API calls. The MinSDKVersion tag is used by the market to indicate that lowest device OS that that app can be installed on. The target SDK version is used to indicate the API level that you would like to compile with, which gives you access to the higher level API calls. However, if you do that, you will have you guard some of your code as some devices (the minSDKVersion devices) may not have the desired API.
Upvotes: 0
Reputation: 2584
If you will use code from api 16 on device with android 2.3,probably will be NoSuchMethodException.
Upvotes: 0