我有一个现有的 android studio 项目,我想在我的项目中使用 jetpack compose。文档说明了如何使用 jetpack compose 创建一个新项目,但是如何将它与现有项目一起使用?
最佳答案
Jetpack compose 需要 minSdkVersion
至少 21 个。因此,在您的 app/build.gradle
中添加/更新以下内容文件
android{
//...
defaultConfig {
minSdkVersion 21
targetSdkVersion 29
//...
}
//...
}
您还需要使用 Android studio (来自金丝雀 channel )以使用具有最新功能的 jetpack-compose。现有项目的最简单方法
第一步:
在项目窗口中,
right click on the package you want to include the compose activity -> compose -> Empty compose activity
.或者
File -> new -> compose -> Empty compose activity.
第二步将出现一个对话窗口。填写必填字段并单击
Finish
.就这样。
现有项目的手动配置
第一步:
在您的
project/build.gradle
中使用最新版本的 kotlin 和 gradle 插件文件。例子:
buildscript {
ext {
compose_version = '1.0.0-beta08'
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.0-alpha02'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
在您的 project/app/build.gradle
,添加以下内容android{
//...
defaultConfig {
minSdkVersion 21
targetSdkVersion 30
//...
}
//...
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
kotlinCompilerVersion '1.4.32'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.5.2'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.0-beta1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
}
第二步:将撰写 Activity 添加到 list 文件中。
<application
android:label="@string/app_name"
<!-- ... -->
>
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.MyApp.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- ... -->
</application>
第三步:创建 jetpack-compose Activity 。
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.Composable
import androidx.ui.foundation.Text
import androidx.ui.core.setContent
import androidx.ui.material.MaterialTheme
import androidx.ui.tooling.preview.Preview
class MainComposeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
Greeting("Android")
}
}
}
}
@Composable
fun Greeting(name: String) {
Text(text = "Hello $name!")
}
@Preview
@Composable
fun DefaultPreview() {
MaterialTheme {
Greeting("Android")
}
}
就这样。快乐编码:)
关于android - 将 jetpack compose 添加到现有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61559352/