4

I've been learning about Android recently and I have a question about the new AndroidX library. I understand that AndroidX is simply a new upgrade to the support library which focuses on providing backwards compatibility for earlier Android SDKs.

However, Google requires that "If you want to use AndroidX in a new project, you need to set the compile SDK to Android 9.0 (API level 28) or higher". I am confused here. Does this mean AndroidX actually uses some API methods or functionalities that are only available on Android 9.0 or higher version? If so, how could it provide backwards compatibility for older APIs which don't have access to these high level API methods?

I don't think the support library has any requirements on compileSdkVersion.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
NJUHOBBY
  • 850
  • 3
  • 10
  • 30

3 Answers3

3

I had the same question before, too. The key is, don't confuse minSdkVersion with compileSdkVersion. If you minSdkVersion is 16, then your app will support down to Android API version 16. But to use AndroidX, you need to make sure the compileSdkVersion is 28. You normally use the most recent Android SDK version when compiling an app anyway, so this is nothing new.

Here is a sample app build.gradle file using AndroidX. Note the minSdkVersion with compileSdkVersion version numbers.

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
2

The Compiled version is what you need to use while developing an application on your IDE. Accroding to this answer.

So it does not matter on which SDK you do your code. it will always provide the backward compatibility.

Hardik Trivedi
  • 613
  • 7
  • 10
Urvish rana
  • 601
  • 10
  • 24
-1

I am confused here, does this mean AndroidX actually uses some API methods or functionalities that are only available on Android 9.0 or higher version?

No AndroidX will available for Android releases with backwards-compatibility

Read AndroidX Overview

  • Like the Support Library, AndroidX ships separately from the Android OS and provides backwards-compatibility across Android releases.

For more information Please read Android Developers Blog about Hello World, AndroidX

ALSO Read more about compileSdkVersion

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.
  • It should be emphasized that changing your compileSdkVersion does not change runtime behavior
AskNilesh
  • 67,701
  • 16
  • 123
  • 163