将jetpack撰写到现有项目中 [英] Add jetpack compose to existing project

查看:125
本文介绍了将jetpack撰写到现有项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的android studio项目,我想在我的项目中使用jetpack compose.该文档说明了如何使用jetpack compose创建一个新项目,但是如何将其与现有项目一起使用?

解决方案

Jetpack编写的 minSdkVersion 至少为21.因此,请在您的 app/build.gradle中添加/更新以下内容文件

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

此外,您还需要使用

仅此而已.

现有项目的手动配置:现在建议在Android Studio稳定之前

第1步:在您的 project/build.gradle 文件中使用最新版本的kotlin和gradle插件.

示例:

  buildscript {ext.kotlin_version ='1.3.71'def compose_release_version ="dev10"ext.compose_version ="0.1.0- $ compose_release_version"ext.compose_compiler_extension_version ="0.1.0- $ compose_release_version"储存库{谷歌()jcenter()}依赖项{classpath'com.android.tools.build:gradle:4.1.0-alpha08'类路径"org.jetbrains.kotlin:kotlin-gradle-plugin:$ kotlin_version"}}所有项目{储存库{谷歌()jcenter()}} 

在您的 project/app/build.gradle 中,添加以下内容

  android {//...defaultConfig {minSdkVersion 21targetSdkVersion 29//...}//...kotlinOptions {jvmTarget ="1.8"}buildFeatures {撰写真实}composeOptions {kotlinCompilerVersion"1.3.70-dev-withExperimentalGoogleExtensions-20200424"kotlinCompilerExtensionVersion"$ compose_compiler_extension_version"}}依赖项{实现("org.jetbrains.kotlin:kotlin-stdlib-jdk7:$ kotlin_version")实现("androidx.compose:compose-runtime:$ compose_version")实现("androidx.ui:ui-framework:$ compose_version")实现("androidx.ui:ui-layout:$ compose_version")实现("androidx.ui:ui-material:$ compose_version")实现("androidx.ui:ui-foundation:$ compose_version")实现("androidx.ui:ui-animation:$ compose_version")实现"androidx.ui:ui-tooling:$ compose_version"实现('androidx.appcompat:appcompat:1.1.0')实现('androidx.activity:activity-ktx:1.1.0')实现"androidx.core:core-ktx:1.2.0"} 

第2步:将compose活动添加到清单文件中.

 <应用程序android:label ="@ string/app_name"<!-...->><活动android:name =.ui.MainComposeActivity"android:label ="@ string/title_activity_main_compose"android:theme ="@ style/Theme.ComposeExperiment.NoActionBar"></activity><!-...-></应用程序> 

第3步:

创建jetpack-compose活动.

 导入android.os.Bundle导入androidx.appcompat.app.AppCompatActivity导入androidx.compose.Composable导入androidx.ui.foundation.Text导入androidx.ui.core.setContent导入androidx.ui.material.MaterialTheme导入androidx.ui.tooling.preview.Preview类MainComposeActivity:AppCompatActivity(){重写fun onCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)setContent {MaterialTheme {问候语("Android")}}}}@可组合有趣的问候语(名称:字符串){文字(文字="Hello $ name!")}@预览@可组合fun DefaultPreview(){MaterialTheme {问候语("Android")}} 

仅此而已.编码愉快:)

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 4.1 (from canary channel) in order to use jetpack-compose.

Easiest method for existing projects

Note: Currently (as of 2020-05-02) latest version of Android Studio (4.1) is under canary channel and uses an outdated dependency version for the jetpack-compose, So it is recommended to configure the dependencies manually as explained in the bottom section of this answer until Google fix this.

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

A dialog window will appear. Fill up the required fields and click Finish.

That's all.

Manual configuration for existing projects : Recommended now until Android Studio gets stable

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

Example:

buildscript {
    ext.kotlin_version = '1.3.71'

    def compose_release_version = "dev10"
    ext.compose_version = "0.1.0-$compose_release_version"
    ext.compose_compiler_extension_version = "0.1.0-$compose_release_version"

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0-alpha08'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

In your project/app/build.gradle, add the following

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

  kotlinOptions {
       jvmTarget = "1.8"
  }

  buildFeatures {
    compose true
  }
  composeOptions {
    kotlinCompilerVersion "1.3.70-dev-withExperimentalGoogleExtensions-20200424"
    kotlinCompilerExtensionVersion "$compose_compiler_extension_version"
  }
}


dependencies {
  implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version")
  implementation("androidx.compose:compose-runtime:$compose_version")
  implementation("androidx.ui:ui-framework:$compose_version")
  implementation("androidx.ui:ui-layout:$compose_version")
  implementation("androidx.ui:ui-material:$compose_version")
  implementation("androidx.ui:ui-foundation:$compose_version")
  implementation("androidx.ui:ui-animation:$compose_version")
  implementation "androidx.ui:ui-tooling:$compose_version"
  implementation('androidx.appcompat:appcompat:1.1.0')
  implementation('androidx.activity:activity-ktx:1.1.0')
  implementation "androidx.core:core-ktx:1.2.0"
}

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

 <application      
    android:label="@string/app_name"
     <!-- ... -->
     >
    <activity
        android:name=".ui.MainComposeActivity"
        android:label="@string/title_activity_main_compose"
        android:theme="@style/Theme.ComposeExperiment.NoActionBar">
    </activity>

      <!-- ... -->
  </application>

Step 3:

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

That's all. Happy coding :)

这篇关于将jetpack撰写到现有项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆