Reputation: 1174
everyone!
I'm new to android programming, so simple things sometimes become a problem.
I have my application. It should work on devices with Android 5 and higher. The question is what is proper strategy of sdkVersion defining?
What I mean.
For example, I need to acheive permision to use bluetooth. If my target sdkVersion is 7 and minimum sdkVersion is 5 I should ask permission in manifest file and then acheive it in runtime. Like this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
...
}
But if my target sdkVersion is 5 even Build.VERSION_CODES.M
cannot be resolved.
So the question is : what is proper approach to choose sdkVersion? Where can I read about it?
I read here https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#target
but I didn't get what is best practice. So please share your experience.
Upvotes: 0
Views: 5157
Reputation: 3268
I suppose you are using Android Studio and build.gradle. If not, I recommend you to get it. All of the following is relevant for Android Studio and gradle build system.
Your main mistake is that SDK version in build.gradle
of app
module is not the same as Android Version. Here is the list of Platform Codenames, Versions, API Levels. What you need for SDK version is number in API level column of first table.
This is how android
section of build.gradle
for app targeting Android 5.0 and newer should look like.
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "net.eraga.myobjectives"
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
Read more about targetSdkVersion
, minSdkVersion
and compileSdkVersion
here.
In this case, your minSdkVersion
should be 21 (android 5.0) and targetSdkVersion
along with compileSdkVersion
should be 25 (android 7.1).
Upvotes: 1
Reputation: 13865
Read this article from Ian Lake: Picking your compileSdkVersion, minSdkVersion, and targetSdkVersion
compileSdkVersion:
compileSdkVersion is your way to tell Gradle what version of the Android SDK to compile your app with. Using the new Android SDK is a requirement to use any of the new APIs added in that level.
minSdkVersion:
If compileSdkVersion sets the newest APIs available to you, minSdkVersion is the lower bound for your app. The minSdkVersion is one of the signals the Google Play Store uses to determine which of a user’s devices an app can be installed on.
targetSdkVersion:
The most interesting of the three, however, is targetSdkVersion. targetSdkVersion is the main way Android provides forward compatibility by not applying behavior changes unless the targetSdkVersion is updated. This allows you to use new APIs (as you did update your compileSdkVersion right?) prior to working through the behavior changes.
Ideally, the relationship would look more like this in the steady state:
minSdkVersion (lowest possible) <= targetSdkVersion == compileSdkVersion (latest SDK)
Upvotes: 2