I have an existing android studio project and I want to use jetpack compose in my project. The documentation says how to create a new project with jetpack compose, but how to use it with existing projects?

推荐答案

Jetpack compose requires a minSdkVersion of at least 21. So add/update the following in your app/build.gradle file

android{ 
   //...
   defaultConfig {       
      minSdkVersion 21
      targetSdkVersion 29
      //...
   }
  //...
 }

Also you need to use Android studio (from canary channel) in order to use jetpack-compose with the latest features.

Easiest method for existing projects

Step 1:

In project window, right click on the package you want to include the compose activity -> compose -> Empty compose activity.

or

File -> new -> compose -> Empty compose activity.

Step 2

将出现一个对话框窗口.填写必填字段,然后单击Finish.

enter image description here

That's all.


现有项目的手动配置

Step 1: Use latest version of kotlin and gradle plugins in your project/build.gradle file.

例子:

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"
}

Step 2: Add the compose activity into your manifest file.

 <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>

Step 3:

创建jetpack compose活动.

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")
    }
}

enter image description here

enter image description here

That's all. Happy coding :)

Kotlin相关问答推荐

Jetpack Compose中的数字 Select 器问题

用普通Kotlin理解Gradle的Kotlin DSL'""

可以从背景图像中点击图标吗?

Spring Boot kotlin协程不能并行运行

用vararg替换列表的设计弃用警告

我可以更改方法中泛型类的类型参数边界吗?

通过快捷键手动砍掉功能参数等

Kotlin 基于参数类型的返回类型推断

kotlin 模式匹配如何像 scala 一样工作

协程子作业(job)取消

如何避免键盘打开时jetpack compose 内容上升

kotlin 如何决定 lambda 中的参数名称?

Kotlin 日期格式从一种更改为另一种

有没有办法重用 Job 实例?

在 kotlin 中写入 parcer 可空值

ActivityOptions.makeSceneTransitionAnimation 在具有多个视图的 kotlin 中不起作用

类型不匹配:推断类型为 LoginActivity 但应为 LifecycleOwner

Kotlin中OnclickListener方法之间的差异

如何在Kotlin中使方法param可变?

Android Studio - java.io.IOException:无法生成 v1 签名