[英]Add Jetpack-Compose to projects uses API below 21
我知道在项目中启用 Jetpack Compose 必须至少达到 API 级别 21。
我想在我现有的项目中使用 JetPack Compose。 它使用minSdkVersion=17
并且我无法将撰写依赖项添加到build.gradle
。
uses-sdk:minSdkVersion 17 cannot be smaller than version 21 declared in library [androidx.activity:activity-compose:1.3.0-alpha03]
我的目标是为拥有 Android API >= 21 的用户使用 Compose。
我试图创建一个新的 android-library 模块( :widget
)。 设置minSdkVersion=21
并在其中配置 Compose。
添加:widget
到:app
模块依赖项
dependencies {
implementation project(":widget")
...
}
覆盖:app
的 AndroidManifest.xml 中所有 Compose 包的minSdkVersion
<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" />
最后,定义自定义视图并将配置的 ComposeView 作为普通视图返回。
它会在 Android API >= 21 中正常工作
fun myCustomView(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
): View {
return ComposeView(context, attrs, defStyleAttr).apply {
setContent {
Text(text = "Hello")
}
}
}
没办法。
这是官方的,Android 工作室甚至不允许您创建具有证明这一点的配置的项目。
尝试手动进行可能会编译,但我认为如果在 API 级别 <21 的设备上运行该应用程序肯定会崩溃
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.