Android Tutorial android.jar / targetSdkVersion / minSdkVersion

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
There are several versioned components that affect the compilation process and the runtime behavior of our apps.
The purpose of this tutorial is to explain the differences between them and help you choose which version to use.

Each Android version is mapped to an api level. You can see this mapping here: https://developer.android.com/about/dashboards/index.html
The api level is used in our code.

minSdkVersion

Set in the manifest editor. Default value is 5 (Android 2.0). This one is very simple. Your app will only run on devices running this OS version or higher.
In most cases you can leave it with the default value or set it to 16 to target Android 4.1+ and (theoretically) lose 1% of the devices.

android.jar

Set under Tools - Configure Paths. android.jar is a referenced library. It is only used during compilation.
It doesn't affect the runtime behavior and it will not cause your app not to run on older versions.
Use the latest version available to avoid compilation errors.

targetSdkVersion

Set in the manifest editor.
This one is a bit more confusing. It helps with backwards compatibility.
I will explain it with an example. Google introduced the runtime permissions system in Android 6 (API 23). Requesting runtime permissions requires developers to update their existing apps. Forcing this feature on existing apps would have broken 90% of the apps (only when they run on Android 6+).
The way to solve it is to check the targetSdkVersion value. If it is 22 or below then the system knows that the app doesn't handle the changes introduced in API 23+ and it runs it without those changes.
This means that an app with targetSdkVersion set to 14 (Android 4.0) will run properly on an Android 8.0 device and it will not be required to request runtime permissions, nor handle any other breaking change.

Q: Will my app run on Android 4.0 if targetSdkVersion is set to 8.0?
A: Yes.

Q: Can I use new features without setting the targetSdkVersion to a higher version?
A: In most cases yes.

The minimum version for Google Play app is 26. It will be raised to 28 on August 2019.

Q: What are the consequences of raising the targetSdkVersion?
A: There are many changes that might affect your app.

The most common are:
- 22 (?) - notifications icons colors are ignored. Alpha levels used as a mask.
- 23 - runtime permissions (https://www.b4x.com/android/forum/threads/runtime-permissions-android-6-0-permissions.67689/#content)
- 23 - Setting system settings require special handling - WRITE_SETTINGS permission (https://www.b4x.com/android/forum/threads/permission-write_settings.94311/#post-596846)
- 24 - must use FileProvider when sharing files (https://www.b4x.com/android/forum/threads/class-fileprovider-share-files.97865/)
- 24 - Default background color is white (https://www.b4x.com/android/forum/threads/version-safe-themes.87694/)
- 24 - Setting the volume or the ringer mode will throw an error if the user set the Do Not Disturb mode. Your app can request special access: https://www.b4x.com/android/forum/threads/set-volume-error.92938/#post-588012
- 26 - notifications will not work without channels (https://www.b4x.com/android/forum/threads/version-safe-notification.87663/)
- 26 - background limits. Features such as push notifications, StartServiceAt or #StartAtBoot will only work with B4A v8+.
- 26 - installation from unknown sources requires explicit setting: https://www.b4x.com/android/forum/threads/version-safe-apk-installation.87667/
- 28 - Foreground services require a new non-dangerous permission. It is added automatically if using B4A v9+.
- 28 - The old http SDK is not available by default. This will cause problems with native libraries such as Google Maps who rely on the old SDK. To enable it: https://www.b4x.com/android/forum/threads/solved-stdactionbar-error-in-android-9-pie.103247/#post-649875
- 28 - Non-ssl (non-https) communication is not permitted by default. It can be enabled in B4A v9+ by adding this line to the manifest editor:
B4X:
CreateResourceFromFile(Macro, Core.NetworkClearText)
You can see the behavior changes of each version:

https://developer.android.com/about/versions/10/behavior-changes-all
https://developer.android.com/about/versions/pie/android-9.0-changes-28
https://developer.android.com/about/versions/oreo/android-8.0-changes.html

Q: Can I test the new behavior changes with a device running an older version of Android?
A: No. This means that you must test your app on devices running a recent version of Android.
 
Last edited:

incendio

Well-Known Member
Licensed User
Can new behavior test via emulator such as genie?

What about private app that isn't publish on play store, will it still runs OK after Oct 18 if target sdk not set to 26?
 

Eme Fibonacci

Well-Known Member
Licensed User
Is it correct to think that many apps are will not be updated and will be out of the market in August 2018?
 

Erel

Administrator
Staff member
Licensed User
No. Quoting Google's blog:
This will be required for new apps in August 2018, and for updates to existing apps in November 2018.

At this point it only affects updates to existing apps. So as long as an existing app is not updated it will continue to run without this restriction.
 

Erel

Administrator
Staff member
Licensed User
I don't think that it requires a solution, it is not a problem. It is a new design that we need to follow.

There is no workaround for this.
 

LucaMs

Expert
Licensed User
I don't understand this statement:
Forcing this feature on existing apps would have broken 90% of the apps
Who "forces"?

Q: Should we panic?
A: No. Forget about it until October 2018 and then panic.
Nice! Fortunately, I already live with panic, I'm used to it :p


it might require some libraries to be updated
This is a further reason to publish libraries written using B4A and their SOURCE CODE (the same as GitHub).
 

jnjft

Member
Licensed User
... android.jar is a referenced library... Use the latest version available to avoid compilation errors.
Where do I get the latest version from?

Thank you.
 

janderkan

Active Member
Licensed User
- 26 - background limits. Will require an update to B4A. Don't set targetSdkVersion to 26 if you are using features such as: push notifications, StartServiceAt or #StartAtBoot.

Will there be an update to B4A before august 18 ?
 

Erel

Administrator
Staff member
Licensed User
One of the reasons that I decided to wait with the release of B4A v8.0 is that not everything related to the new background limitation is exactly clear from Google documentation. More experience is gained over time as more developers implement it.

v8.0 is planned to be released in March (at least a beta version).
 
Status
Not open for further replies.
Top