Android Tutorial android.jar / targetSdkVersion / minSdkVersion

Discussion in 'Tutorials & Examples' started by Erel, Dec 24, 2017.

Thread Status:
Not open for further replies.
  1. Erel

    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.
    Up until last week there was almost no incentive for developers to raise the targetSdkVersion.

    Starting from August 2018 for new apps and November 2018 for existing apps, Google will force developers to set the targetSdkVersion to 26+: https://android-developers.googleblog.com/2017/12/improving-app-security-and-performance.html

    Q: What are the consequences of raising the targetSdkVersion to 26?
    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/

    You can see the behavior changes of each version: 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.

    Q: Should we panic?
    A: No. Forget about it until October 2018 and then panic.
    Or better, start with upgrading your app to targetSdkVersion 23 and handle runtime permissions which can require some work.
    Make sure to test your app with a device running Android 7 or 8 and later raise the targetSdkVersion to 26.
    There is still time for the new enforcement and it might require some libraries to be updated.
     
    Last edited: Oct 3, 2018
  2. Multiverse app

    Multiverse app Active Member Licensed User

    Thank you for this tutorial.
     
    hromek likes this.
  3. somed3v3loper

    somed3v3loper Well-Known Member Licensed User

    I started to panic though I don't have any apps published
     
    ocalle, eps, j_o_h_n and 6 others like this.
  4. incendio

    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?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Yes. Emulators are not perfect but they can be useful for these tests.
     
    Peter Simpson likes this.
  6. Eme Fibonacci

    Eme Fibonacci Active 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?
     
  7. Erel

    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.
     
  8. ilan

    ilan Expert Licensed User

    :D:D:D
     
    igodese and Peter Simpson like this.
  9. mw71

    mw71 Active Member Licensed User

    is there an solution for that?
     
  10. Erel

    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.
     
  11. LucaMs

    LucaMs Expert Licensed User

    I don't understand this statement:
    Who "forces"?

    Nice! Fortunately, I already live with panic, I'm used to it :p


    This is a further reason to publish libraries written using B4A and their SOURCE CODE (the same as GitHub).
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Google.
     
  13. jnjft

    jnjft Member Licensed User

    Where do I get the latest version from?

    Thank you.
     
  14. NJDude

    NJDude Expert Licensed User

    "android.jar" is part of the Android SDK, by downloading the latest SDK you are getting the latest .jar.
     
    jnjft likes this.
  15. jnjft

    jnjft Member Licensed User

    Thanks for your light speed answer!
     
  16. janderkan

    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 ?
     
    Multiverse app likes this.
  17. DonManfred

    DonManfred Expert Licensed User

    Yes. It is announced for next month somewhere here in forum if i remember correctly.
     
    Erel, Multiverse app and janderkan like this.
  18. Erel

    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).
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    Multiverse app likes this.
  20. Erel

    Erel Administrator Staff Member Licensed User

Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice