I tried to create a new android-library module (:widget). set minSdkVersion=21 and also configure Compose inside it.
Add :widget to :app module dependencies
dependencies {
implementation project(":widget")
...
}
Override minSdkVersion of all Compose packages in :app's AndroidManifest.xml
<uses-sdk
android:minSdkVersion="17"
tools:overrideLibrary="com.example.widget, androidx.compose.ui.platform, androidx.compose.material.icons, androidx.activity.compose, androidx.compose.ui.tooling, androidx.compose.ui.tooling.data, androidx.compose.material.ripple, androidx.compose.foundation, androidx.compose.animation, androidx.compose.foundation.layout, androidx.compose.ui.text, androidx.compose.ui.graphics, androidx.compose.ui.unit, androidx.compose.ui.util, androidx.compose.ui.geometry, androidx.compose.runtime.saveable, androidx.compose.animation.core" />
Finally, define custom-view and return a configured ComposeView as a normal view.
It'll work fine in Android API >= 21
fun myCustomView(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
): View {
return ComposeView(context, attrs, defStyleAttr).apply {
setContent {
Text(text = "Hello")
}
}
}