Jetpack Compose 无法再导入 Text 或 setContent

Jac*_*lin 6 android kotlin android-jetpack android-jetpack-compose

我最近一直在玩弄 Jetpack Compose,我已经在这里完成了基本教程然后我开始查看他们拥有的 Jetnewssample 项目。现在我已准备好开始处理我自己的项目,但是现在当我在与 Jetnewssample 项目相同的父目录中创建一个新项目时,(工作正常)Android Studio 无法再导入 androidx.ui.core.Text 或androidx.ui.core.setContent。我可以从同一位置导入其他类,但现在我只是收到一个未解决的引用错误。这是一个以 Empty Compose Activity 开始的新项目。这是代码:


import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.Composable
import androidx.ui.core.Text
import androidx.ui.core.setContent
import androidx.ui.material.MaterialTheme
import androidx.ui.tooling.preview.Preview

class MainActivity : 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")
    }
}
Run Code Online (Sandbox Code Playgroud)

这是模块的 gradle 构建文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "com.franklin.sanctified"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.ui:ui-layout:0.1.0-dev03'
    implementation 'androidx.ui:ui-material:0.1.0-dev03'
    implementation 'androidx.ui:ui-tooling:0.1.0-dev03'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Run Code Online (Sandbox Code Playgroud)

Nic*_*zzi 23

就我而言,问题是我缺少compose活动集成。是类的扩展函数,定义于setContentComponentActivitypackage androidx.activity.compose

只需导入:

implementation 'androidx.activity:activity-compose:1.4.0'
Run Code Online (Sandbox Code Playgroud)


Pra*_*dhe 10

更新日期:2021 年 5 月

在应用级 Gradle 或 kts 文件中为 set content{ .. } 添加以下依赖项

implementation("androidx.activity:activity-compose:1.3.0-alpha07")
Run Code Online (Sandbox Code Playgroud)

  • 我仍然不明白为什么 ```setContent``` 函数在当前(稳定 1.4.0)版本中不可用。 (5认同)

Com*_*are 7

那些似乎是ui-framework为了dev03。尝试将该依赖项添加到您的阵容中:

implementation "androidx.ui:ui-framework:0.1.0-dev03"
Run Code Online (Sandbox Code Playgroud)