Which targetSDK to use for BLE? - android

I’m building an Android app which should run on every device with Bluetooth Low Energy, which means a minSDK of 18. I’m not sure as to which targetSDK I should use however. I read online that it is good practice to always use the latest version for this (API 22). Is this the case, or should I build my application with every targetSDK that I support, i.e. build with the SDK 18 for applications that run API 18, build with SDK 19 for devices with API 19, …?
I’m confused since to start a BLE discovery I can use either startScan() or startLeScan(). The Android documentation tells me: "startLeScan() was deprecated in API level 21, use startScan() instead". I’m unsure what impact this has on which targetSDK I should use to compile my app with. Will devices running API 18 will be able to run my app if I compile with SDK 22 and use startScan(), and will devices running API 22 be able to run my app if I compile with API 18 and use startLeScan()? Or should I really just build my application with every targetSDK that I support like mentioned above?

Let me answer this for you.
1) you already know minSDK should be 18(cool). Because LE supports is there on or after that.
2) If you build your app using target sdk 18, it will work on devices supporting Android 5.0/5.1 also.
3) If you build your app using target >sdk 21, and use startLeScan instead of startScan(startScan is introduced in 5.0/sdk21), it will work on all devices running on >18 api level.
4) Now the tricky part, there are also other apis which are available on 5.0+, which were missing on lower versions. I will suggest build the app using target of the latest sdk (currently 23).
i) Use different apis to achieve same results in different versions. Like if the phone running on lower than Android 5.0, use startLeScan, else startScan.
ii) There are differences on Android 5.0 and 5.1 also, so use those methods accordingly.
5) Some apis like startLeScan are deprecated, but I know, they are still working as they have tied them up with new apis. so until they remove the old apis, they will work on all platforms. This is precisely what deprecation means :)
So answer of your question "Which target sdk should be use", answer is latest sdk :), and call respective apis based on SDK version. You can get SDK version on run time via android.os.Build.VERSION.SDK_INT

I'd recommend to use Android 5.0 and above. Why? This video quite clearly states "BLE in Android below 5.0 is crappy".

Related

Is it possible to support newer android sdk versions in react-native?

I was following setup guide for android in react-native documentation
and have it up and running correctly using suggested SDK v23
However there are much newer SDK versions available, I would like to set v23 as a minimum SDK version, but use latest one as my main one.
Being new to android I'm not sure on how to achieve this, what steps should I take? I can see option to edit sdk versions in some xml files and can download latest one using Android Studio.
As a side note, what are my limitations of using SDK v23? Will it not work on newer devices, do I loose out on performance etc... ?
According to some sources React Native does support up to API level 25, but there seems to be some disagreement on the issue. So according to official sources, React Native only supports Android API level 16 to level 23.
I haven't used React Native myself, but assuming versioning works the same as native development, an application with a compileSdkVersion lower than the version it is running against will run fine. The only limitations on the application would be that it would be unable to access the new features and APIs introduced by those higher SDK versions.
For example, Android 8.0 (API level 26) introduced a new Picture-in-Picture API, but the method used to enter picture-in-picture mode would not be available in code compiled against API level 23. Some new features may effect performance (usually positively), but an application compiled against API level 23 won't perform any worse on on a device running on API 26 than one running on API 23.

How to make Android Apps designed for API 22 compatible with API21

I am new to android development and using Xamarin.Android so please keeps terminology simple and relevant if possible.
Current question is, are apps developed for API 22 (android 5.1) compatible with API 21 (android 5.0) by nature - i.e. without adding any package and stuff. If not, how do I make backwards compatibility possible? Thanks.
Typically, you set a minimum SDK value, and a target SDK, and that says what API "range" your app runs against. You additionally compile with a particular SDK version, and that includes the code and resources for the app.
Most of the core API methods and classes are available starting at API 1.
If you want to include libraries, the support libraries should get you backwards compatible back to about API 7.
are apps developed for API 22 (android 5.1) compatible with API 21
If you use classes and methods which were introduced in API 22, then your app will not run on API 21. On the other hand, if you limit yourself to only methods available on API 21, then your app will run on both API 21 and API 22. For newer features, you can use the Support Library.

Android target sdk vs deprecated methods

This code is some basic code to set up tabs in Android.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
If you write this code in a project where targetSDK < 20 or compileSDK < 20 (I don't know what the difference is between versions of compileSDK and targetSDK) then it is fine.
However, if you have compileSDK higher, e.g. 23, then Android Studio will tell you that the code above is deprecated (and moreover is removed from SDK, I guess).
And it will crash my app if i run it on device (btw on Android level 20, though the target of project was set to 23 and compiled version too 23, so it crashes because it is deprecated and removed from SDK level 23).
Questions:
1) What happens if I compile my project with compileSDK version and
targetSDK version set to 20 (where there is no problem with being
deprecated) and try run it on Android with SDK 21 or 23 where these
functions are obviously removed from SDK? Will the app crash or work?
2) What happens if I decide to target the "high" SDK (e.g. 23)? That
is, I will leave the "old" code mentioned above and will use some
"new" code for tabs, which appeared in SDK 23 or so (which didn't
exist in lower SDK); then I will try to run the application on
Android with lower SDK, e.g. 20. Will the app crash or work?
edit:
Btw now i launched project with targetSDK and compileSDK 20 on Android with level 22 and it worked.
So it seems the only thing that really matters is the compileSDK version? Doesn't even matter on the SDK version of device but it really matters the compileSDK?
Because as i said about the two tests - project with bad HIGH SDK launched on device with good LOW SDK = crash. Project with good LOW SDK launched on devide with bad HIGH SDK = not crash.
But the logic of course say that it must also matter on android version of device (eg to access some new functions.. or really only matter on compiledSDK or at least from view of backwards-functionallity)?
Need some opinions of experts on this.
However, if you have compileSDK higher eg 23, then Android Studio will tell you, the code above is deprecated (and moreover removed from SDK i guess).
No. getActionBar() is not deprecated. setNavigationMode() is deprecated but not removed.
What happens, if i compile my project with compileSDK version and targetSDK version eg 20 (where is not problem with deprecated) and will try run it on Android with SDK eg 21 or 23 where are these functions from SDK obviously removed? Will app crash or work?
The app should work fine. "Deprecated" means "we think that there is a better solution and we would prefer that you use it".
What happens, if i decide to target the "high" SDK that means eg 23, i will leave the "old" code mentioned above and will use some "new" code for tabs, which appeared eg in SDK 23 or so (that means didn't exist in lower SDK) and i will try run the application on Android with lower SDK, eg 20? Will app crash or work?
First, I am not aware of any "'new' code for tabs, which appeared eg in SDK 23". The Design Support library has the only new tab implementation that I can think of, and it works back to API Level 7.
Second, your app should fail to compile, if you are trying to use something that is newer than your minSdkVersion without adequate checks (e.g., using Build.VERSION.SDK_INT and bypassing that code on older devices).
In the end, if you refer to a class, method, field, interface, and so on that does not exist on the older API level, your app will crash if and when that code gets executed. A typical error is VerifyError, though it depends a bit on the nature of what it is that you are trying to use that does not exist.
With respect to tabs, there are countless implementations available to you. Most come in the form of third-party libraries tied to ViewPager. In terms of the Android SDK, there the aforementioned TabLayout from the Design Support library, FragmentTabHost, and PagerTabStrip (though the latter has a bug in the 23.0.0 edition of the support-v4 library, apparently).
Deprecated API are still available, so you're free to use them - the app won't crash on neither of platforms. However, it's advised that you read the documentation to understand why the API have been deprecated: usually developers provide alternative solutions.
Here is some explanation on compile/target/minSDK.
1) What happens, if i compile my project with compileSDK version and
targetSDK version eg 20 (where is not problem with deprecated) and
will try run it on Android with SDK eg 21 or 23 where are these
functions from SDK obviously removed? Will app crash or work?
Android Studio is probably only warning you that the API has been deprecated, but not removed. If it had been removed, setting the compile SDK to 23 should break your compilation. Your application should work fine even if it is using a deprecated API, but you should update that when you have the time as it is no longer the recommended one.
2) What happens, if i decide to target the "high" SDK that means eg
23, i will leave the "old" code mentioned above and will use some
"new" code for tabs, which appeared eg in SDK 23 or so (that means
didn't exist in lower SDK) and i will try run the application on
Android with lower SDK, eg 20? Will app crash or work?
When you use an API that has been introduced in SDK 23, you will need to set compileSDK to 23 as well. In case you set your minSDK to 22, you will be able to install it on a device that is using SDK 22, however, your app will crash with a NoSuchMethodError if you try to use that method. You can however check the SDK running on the device (use Build.VERSION.SDK_INT) and not call that method in this case.

What SDK to install, seriously?

I know there are plenty of documents over the internet but I have still some doubts about what SDK to install on my computer to create an android application for nearly %100 devices to working with. Google Play says that if I use API 8 (which is Android 2.2) then my application work nearly all Android devices.
So I downloaded API 8 and API 22 (Android 5.1, was default installed by Android Studio itself) and I don't know if it works or not if I select Minimum SDK to API 8 while creating a new project.
So seriously guys, what the heck is going on?
What SDK Platform(s) you install has little to no impact on what versions of Android you can support.
If you create a new Android Studio project via the new-project wizard, you will find an app/build.gradle file. In there, you will find a setting named compileSdkVersion. This controls what version of the Android SDK you are compiling against (i.e., what JAR is used to satisfy compile-time references to Java classes like Activity and TextView). Whatever value you specify for compileSdkVersion must be an "SDK Platform" that you have installed from the SDK Manager.
In a newly-created project given your setup description from your question, you will see that compileSdkVersion is 22, lining up with pre-established API Level 22 edition of the SDK Platform.
However, this does not mean that your app will only run on API Level 22+ devices. Much of what is in the API Level 22 edition of the Android SDK has existed in previous versions of the SDK.
The minSdkVersion property in the same app/build.gradle file says how old you are willing to go -- what is the lowest API level you are willing to support. Right now, I think a new project will be set up with minSdkVersion of 15, though that varies over time. More importantly, you can change it to be whatever you want, and you do not need the SDK Platform installed for whatever level you choose.
If, in your code, you reference stuff in the Android SDK that is valid for your compileSdkVersion but is newer than the minSdkVersion, the build tools will point out the discrepancy, so you can make sure that you know what you are doing. This is how Android handles progressive enhancement -- you see what version of Android you are running on (Build.VERSION.SDK_INT) and use newer APIs where you can, falling back to older APIs as needed.
TL;DR: So long as your compileSdkVersion has a value for which you have an installed SDK Platform, you're set.
It depends on the application you are developing. And the resources that it will need. It's true that if you use the oldest version (API 8) and your application is simple enough, then it will work on any device.
But there are limitations to it. on older APIs you will not be able to use new features as Navigation drawer for example
I would recommend API 11 or 14, that covers most of the devices and features nowadays.
You should consider the design you want to create on your app
You can see the market share of the different api versions here: https://developer.android.com/about/dashboards/index.html
Then you have a trade-off of how advanced APIs you want to use vs. how many devices you want to support.

Confusion regarding android API levels?

I was following through a Udacity course on android , and these terms seem to confuse me
(i use android studio 1.x latest version)
In my build.gradle i have these
compileSdkVersion 21
buildToolsVersion "21.1.2"
......
minSdkVersion 10
targetSdkVersion 20
versionCode 1
versionName "1.0"
....
compile 'com.android.support:appcompat-v7:20.0.0'
My assumption
If I leave the app theme as default that is in my manifest file,i set it to use app:theme and in styles.xml it is by default
appcompat.light.darkaction bar
I figured out the appcompat theme means ,it changes to use the theme for the version of the target SDK irrespective of the device OS version
when i chose API 21 ,it looked like a material app
when i chose API 20, it looked like a kitkat app
(both on my device which runs ICS)
so target SDK is contolling the look and feel of my app irrespective of device OS,
it defines what highest version of android the device can support , right?and by default the look and feel change to the latest or the target API look and feel whilst using app:compat theme still enabling it to run on lower sdks ,,as low as API 10
and minsdk is the minumum supported ,
am i right till here?
Now what confuses me is
what's compile SDK , should it always be the latest available ?
Example : material didnt show app icon in action bar icon , i didn't like it , and switched to API 20
to follow with the MOOC videos
but should i leave compile sdk as 21 , wont that make the app look like material , cause i'm confused as it didnt?
And a final question ,
I want to test the app on devices running gingerbread, i don't have any,
can you suggest some typical AVD config for gingerbread devices(cause i know a lotta people still use it in developing nation),
what ram,screen size and pixel density should i use that fits a typical gingerbread device?
so target SDK is controlling the look and feel of my app irrespective of device OS,
Yes. (among other things)
it defines what highest version of android the device can support , right?
No ! The target SDK indicates the SDK you use to test your app. You can use a target SDK 20 and run your app on a device with API 21 installed : it will use the KitKat l&f (i.e level 20).
How is it possible ? When an app targeting API 20 is running on a device with API 21 : the system will automatically enable some backward compatibility hook so that your app behaves just like it behave on older versions (i.e. targetVersion) of the OS.
The maximum sdk supported is the maxSdk attribute (rarely used and not recommended). If you specify a maxSdk 20 : you won't be able to install your app on a device running SDK 21+
The compile SDK level is the version of the sdk you use to compile your app. If you choose the latest one : the users of the latest android version will benefit of code that is optimized for them. It will allow you to use the latest features of the sdk... but be careful when you write code using new apis since it will crash on older devices.(see here under)
The minimum sdk version may be lower than the compile sdk. In your example you are declaring that your code can be executed on API level 10, but compiled with api level 21... that's OK but you MUST ensure that every calls to API that don't exist in API-10 won't be executed on a device running API-10.
Let's illustrate this tricky point. Assume : compileSdk = 20 ; targetSdk = 20 and minSdk = 10
if (Build.VERSION.SDK_INT >= 13) {
apiCallToSomethingAvailableOnlySinceApi13();
}else {
//put here some alternative code to perform something similar
//(but probably degraded) feature
//on device running API 10, 11 or 12
//device with api 9 or lower are exclude anyway (because minSdk is 10)
}
To summarize :
minSDK : it is possible to install the app on a device with at least this level and the developer wrote defensive code to ensure that all calls to api more recent than minSdk won't be executed on device running the minSdk version.
targetSDK : the app was tested on this level and more recent devices must enable backward compatibility hook so that the app still behave as if it was running on this level even on more recent devices.
compileSDK : the code is optimized to run on this level. (usually safe to put it the same as targetSDK)
maxSDK : you cannot install the app on a device running higher API (not recommended and most of the time not useful).

Categories

Resources