How to choose Android sdkVersion properly? - android

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.

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)

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 compileSdkVersionshould be 25 (android 7.1).

Related

How to get "CompileSdkversion" programmatically in Android

I have an About box in my App that displays information about the App, the phone and the data it uses. It's very useful when a user has a problem. I can get the phone's SDK version using "android.os.Build.VERSION.SDK_INT". However, I haven't found a way to get the value of "CompileSdkversion" which indicates the SDK version the App was compiled with. This is the value that is set in the build.gradle file.
While the Android OS version varies by user, the compileSdkVersion does not. For version X.Y.Z of your app, the compileSdkVersion is whatever you said it was when you compiled that app version. So long as your about box contains the app version, you know what compileSdkVersion that you used, if you keep track of that (e.g., check what it was in your version control system).
But, if you really want to have it be available to you at runtime, you have two options.
If your minSdkVersion is 31 or higher, you can use compileSdkVersion on ApplicationInfo. However, most likely, if you are reading this before the year 2026, your minSdkVersion is lower than that.
For older devices than Android 12, you could add a BuildConfig field for it, at least with newer versions of the Android Gradle Plugin:
android {
compileSdk 31
defaultConfig {
applicationId "com.commonsware.android.myapplication"
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField "int", "COMPILE_SDK_VERSION", "$compileSdk"
}
// other stuff goes here
}
This takes your defined value for compileSdk and hoists it into BuildConfig.COMPILE_SDK_VERSION, so you can reference it at runtime. This was tested using a scrap Arctic Fox project, using Gradle 7.0.2 and 7.0.3 of the Android Gradle Plugin.
Here is the relationship between the three values:
minSdkVersion (lowest possible) <=
targetSdkVersion == compileSdkVersion (latest SDK)
CompileSdkVersion has nothing to do with what devices can and cannot run your app. Usually, you set this to be the latest version of the Android SDK.
And the targetSdkVersion should be fully tested and less or equal to compileSdkVersion.(It depends on your app)
If you are using the features of API level of 26 then you need to use compileSdkVersion 26, the lower version will give you an error.
Android supports backward compatibility
(i.e. an app compiled on 26 can also run on a phone having API level 26 or lower).
Considering your use-case, wouldn't a better approach be just to show the current app version? If you know the version, you could look up how/when it was created (via git tags, for example) and then find out the SDK version it was compiled with.

Instant App Not Compiling

When I am trying to compile with following configuration in base feature,
compileSdkVersion 25
targetSdkVersion 25
I get the below error message
"Feature modules require compileSdkVersion set to 26 or higher. compileSdkVersion is set to 25"
Is there a relation between compileSdkVersion and targetSdkVersion of instant app and installable app ? Please help.
You need a compileSdkVersion of 26. You should always use the highest possible value for compileSdkVersion and targetSdkVersion. This does not affect backward compatibility. Moreover, Google requires current values for uploading to Google Play.

Affectiva affdex-sdk works with Android SDK 22 only

I am trying out the github examples referenced in the Affectiva AI docs.
See: Affectiva Android example on github
The examples work, shipping with the following project gradle configuration:
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
defaultConfig {
minSdkVersion 21
targetSdkVersion 22
versionCode 1
setProperty("archivesBaseName", "$project.name-$versionName")
applicationId "com.affectiva.cameradetectordemo"
versionName "3.2.0-01"
}
buildTypes {
release {
minifyEnabled true
}
}
buildToolsVersion '27.0.3'
}
dependencies {
implementation('com.affectiva.android:affdexsdk:3.+')
}
The problem is, the examples work ONLY with compileSdkVersion and targetSdkVersion both set to 22.
When i enter anything higher, 23 to 27, the apps compile but when i want to start the camera from inside the app, the app crashes with the Error Message
java.lang.IllegalStateException: Camera is unavailable.
Questions:
Why is are the Affectiva examples only working with SDK version 22?
How can i use the affdex-sdk with a current version of the Android SDK?
Thanks for your help!
Android introduced the runtime permissions feature in API level 23. An app that targets 23+ must implement runtime permissions logic and request camera permission in order to access the camera.
The purpose of the sample you are referencing is to demonstrate use of the Affectiva SDK, so to avoid the need to add a bunch of runtime permission logic which would be irrelevant to that purpose, it targets API level 22.

Android build version and minSDKVersion in Android Studio

I have read other answers in context of "What is minSDKVersion and targetSDKVersion?" on SO and those were good enough for understanding but they talked about eclipse, not that it really matters that much.
Anyway, I wanted to ask in context of Android Studio, that when creating new app, it asks for minSDKVersion only but no targetSDKVersion as it used to do in Eclipse. Why is this? Is it insignificant?
The other thing I wanted to ask is, when I did create a new app infact with minSDKVersion as IceCreamSandwich(4.0.3), the class MyActivity extended from ActionBarActivity and not from Activity. Why is this happening?
Are minSDKVersion and targetSDKVersion equal in this case? and if this is the case, Would I get the base class as Activityif I were to change the targetSDKVersion explicitly to say, API 21?
In Android Studio, everything turns around Gradle.
Your build.gradle file has this in it:
android {
compileSdkVersion 22
buildToolsVersion '22.0.1'
defaultConfig {
applicationId 'your.package.name'
minSdkVersion 14
targetSdkVersion 22 // default is latest
versionCode 1
versionName '1.0.0'
}
}
Here you can see the minSdkVersion and targetSdkVersion.
Nothing is required in the AndroidManifest.xml file anymore, regarding API levels.
Regarding your Activity issue:
ActionBarActivity is a child class of Activity.
ActionBarActivity is given automatically provided as it's the newest support tool available w.r.t. the minSDKVersion. If you simply want to use Activity, just change it in the code and remove unused methods.

Checking for backwards compatibility- an easier way?

I am about to release my first app and targeted lollipop when writing it in the hope that the uptake would of been greater by the time I was done. As well all know, it is still awful.
In my build.gradle I have this line:
android {
signingConfigs {
}
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.xxx.xxxx.toolbox"
minSdkVersion 21
targetSdkVersion 21
versionCode 7
versionName "1.15"
}
If I change the min SDK version to 19, the app will try and install on a device running API level 19. It crashes, but does not give me any feedback as to why (I have obviously used some methods from higher APIs). Is there any way to quickly figure out which methods I have used that need to be changed?
thanks,
Matt
If you set minSdkVersion to some version and recompile your application, Lint should automatically mark all instances where you use higher API as errors. That is, unless you annotate it with #TargetApi.
Also, if your application crashes, check LogCat. There will be an exception describing what went wrong and where.

Categories

Resources