Android 在现有项目中引入Compose

在现有的项目中引入Jetpack Compose,三步配置步骤也可以看 官网。

一、配置kotlin版本
Compose 从Kotlin 1.4.0 才开始支持

plugins {
     
  id 'org.jetbrains.kotlin.android' version '1.4.0'
}

二、配置 Gradle

android {
     
    defaultConfig {
     
        ...
        minSdkVersion 21 // 从API 21开始支持
    }

	// 启用Jetpack Compose组件特性
    buildFeatures {
     
        compose true
    }
    ...

    // 设置Java和kotlin的编译版本
    compileOptions {
     
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
	
    kotlinOptions {
     
        jvmTarget = "1.8"
        useIR = true
    }

    composeOptions {
     
        kotlinCompilerVersion '1.4.0'
        kotlinCompilerExtensionVersion '1.0.0-alpha05'
    }
}

三、添加 Jetpack Compose 工具包依赖项

dependencies {
     
    implementation 'androidx.compose.ui:ui:1.0.0-alpha05'
    // Tooling support (Previews, etc.)
    implementation 'androidx.ui:ui-tooling:1.0.0-alpha05'
    // Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
    implementation 'androidx.compose.foundation:foundation:1.0.0-alpha05'
    // Material Design
    implementation 'androidx.compose.material:material:1.0.0-alpha05'
    // Material design icons
    implementation 'androidx.compose.material:material-icons-core:1.0.0-alpha05'
    implementation 'androidx.compose.material:material-icons-extended:1.0.0-alpha05'
    // Integration with observables
    implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-alpha05'
    implementation 'androidx.compose.runtime:runtime-rxjava2:1.0.0-alpha05'

    // UI Tests
    androidTestImplementation 'androidx.ui:ui-test:1.0.0-alpha05'
}

至此就配置完成了。

你可能感兴趣的:(Android技术,Kotlin,android,Compose,Jetpack)