dbarnes
dbarnes

Reputation: 529

Maintaining backwards compatibility while using Jetpack Compose?

Jetpack Compose has a min API of 21, but what's the proper way to maintain backwards compatibility back to API 16 or below?

Can we do this with simple build version checks? Something like this:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
       
        // API 16 and below
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN){
             setContentView(R.layout.activity_main)
             val someTV = findViewById(R.id.someTV)
        } 
        // API 21 and above
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             setContent{
                 someComposeUI()
             }
        }
}

Upvotes: 2

Views: 1866

Answers (1)

Martin Zeitler
Martin Zeitler

Reputation: 76679

Building two productFlavors might still be the most realistic approach, because such a configuration will give you source sets, which strictly tell apart what is being compiled.

flavorDimensions "apilevel"
productFlavors {
    legacy {
        dimension "apilevel"
        versionNameSuffix "-legacy"
        minSdkVersion 16
        maxSdkVersion 19
    }
    compose {
        dimension "apilevel"
        minSdkVersion 21
        versionCode android.defaultConfig.versionCode + 50000
    }
}

This also gives you legacyImplementation and composeImplementation for the dependencies.

Upvotes: 5

Related Questions