簡體   English   中英

將 jetpack compose 添加到現有項目

[英]Add jetpack compose to existing project

我有一個現有的 android 工作室項目,我想在我的項目中使用 jetpack compose。 文檔說明了如何使用 jetpack compose 創建一個新項目,但是如何將它與現有項目一起使用?

Jetpack compose 要求minSdkVersion至少為 21。因此在您的app/build.gradle文件中添加/更新以下內容

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

您還需要使用Android 工作室(來自金絲雀頻道)才能使用最新功能的 jetpack-compose。

現有項目的最簡單方法

步驟1:

在項目 window 中, right click on the package you want to include the compose activity -> compose -> Empty compose activity

或者

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

第2步

將出現一個對話框 window。 填寫必填字段,然后單擊Finish

在此處輸入圖像描述

就這樣。


現有項目的手動配置

第 1 步: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"
}

第 2 步:將 compose 活動添加到清單文件中。

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

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

在此處輸入圖像描述

在此處輸入圖像描述

就這樣。 快樂編碼:)

只需按照官方設置即可 添加您的build.gradle

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

android {
    defaultConfig {
        ...
        minSdkVersion 21
    }

    buildFeatures {
        // Enables Jetpack Compose for this module
        compose true
    }
    ...

    // Set both the Java and Kotlin compilers to target Java 8.

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
        useIR = true
    }

    composeOptions {
        kotlinCompilerExtensionVersion "1.0.0-alpha01"
    }
}

dependencies {
    // You also need to include the following Compose toolkit dependencies.
    implementation 'androidx.compose.ui:ui:1.0.0-alpha01'
    implementation 'androidx.compose.material:material:1.0.0-alpha01'
    implementation 'androidx.ui:ui-tooling:1.0.0-alpha01'
    ...
}

同樣從1.0.0-alpha01開始,您可以將 Compose 與您現有的 UI 設計相結合。

在您的布局中添加ComposeView

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  
    <TextView/>

    <androidx.compose.ui.platform.ComposeView
        android:id="@+id/compose_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

在您的Activity中,使用 XML ID 設置ComposeView ,然后調用setContent()以使用 Compose:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main_std)
    .apply {
        findViewById<ComposeView>(R.id.compose_view).setContent {
           MaterialTheme () {
              Text(text = "Compose text", style = MaterialTheme.typography.body2)
           }
         }
    }
}

在此處輸入圖像描述

在您的root level build.gradle 在buildscript中添加 compose 版本變量

ext {
    compose_version = '1.0.1'
}

在此處輸入圖像描述

現在在您的app level build.gradle 在您的 android { } 塊中添加 buildFeatures 和 composeOptions

buildFeatures {
    compose true
}
composeOptions {
    kotlinCompilerExtensionVersion compose_version
}

在你的依賴中

// Jetpack dependencies
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation 'androidx.activity:activity-compose:1.4.0'
// Jetpack debug dependencies
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"

在此處輸入圖像描述

現在您可以開始在現有/新項目中使用 compose

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        // your compose code goes here
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM