Jetpack Compose dev06 setContent()不起作用?

时间:2020-03-10 10:04:18

标签: android android-jetpack-compose

在更新至dev06并运行应用程序时,出现以下错误:

java.lang.NoSuchMethodError: No static method setContent(Landroid/app/Activity;Lkotlin/jvm/functions/Function0;)Landroidx/compose/Composition; in class Landroidx/ui/core/WrapperKt; or its super classes (declaration of 'androidx.ui.core.WrapperKt' appears in /data/app/tt.reducto.composesample-BYNjMDWbVhiprnPCNJw0LA==/base.apk)

6 个答案:

答案 0 :(得分:20)

如果您来自dev05,dev04(或更低版本),则需要进行迁移。

我设法使其正常运行。您需要执行以下操作:

  • Android Studio 4.1 Canary 2或+
  • gradle-wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.1-all.zip

  • build.gradle :(项目级别)
buildscript {
    ext.kotlin_version = "1.3.70"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.0-alpha02"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}
  • build.gradle(应用级别):
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "com.package.name"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    composeOptions {
        kotlinCompilerExtensionVersion = "0.1.0-dev06" // THIS ONE is important
    }

    buildFeatures {
        compose true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    def compose_version = "0.1.0-dev06"

    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    implementation "androidx.ui:ui-foundation:$compose_version"
    implementation "androidx.ui:ui-framework:$compose_version"
    implementation "androidx.ui:ui-tooling:$compose_version"

    implementation "androidx.ui:ui-layout:$compose_version"
    implementation "androidx.ui:ui-material:$compose_version"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

完成所有操作后,运行代码,您就可以开始工作了。

答案 1 :(得分:2)

使用Compose dev07时出现相同的错误。我通过在 build.gradle(应用程序级别)中提供以下代码来解决此问题:

type Constraint = {
    constraint: 'required' | 'equal'
}


function bar<K extends string>(d: Record<K, Constraint[]>) {
    return Object.keys(d).reduce((a, b) => (a[b] = '', a),
                                 {} as { [key: string]: string }) as Record<K, string>
}

const foo = { apple: [ { constraint: 'required' } ] }
// Error  Type 'string' is not assignable to type '"required" | "equal"'.
const baz = bar(foo)


// Works fine
const foo2 = { apple: [  { constraint: 'required' } as Constraint ] }
const baz2 = bar(foo2)

有关更多信息,请检查here

答案 2 :(得分:0)

对于Kotlin DSL(android { buildFeatures { compose = true composeOptions.kotlinCompilerExtensionVersion = Depends.Versions.composeVersion //"1.0.0-alpha08" composeOptions.kotlinCompilerVersion = Depends.Versions.kotlinVersion //"1.4.20" } ):

gradle-wrapper.properties

distributionUrl=https://services.gradle.org/distributions/gradle-6.7.1-bin.zip文件->
$_SERVER

答案 3 :(得分:0)

在你的应用中 build.gradle 应用

 buildFeatures {
    compose true
}
composeOptions {
    kotlinCompilerExtensionVersion compose_version
    kotlinCompilerVersion kotlin_compiler_verion
}

答案 4 :(得分:0)

我在 buildSrc/build.gradle.kts 中遇到了与不必要的依赖相同的错误

dependencies {
    implementation("com.android.tools.build:gradle:7.0.0-beta02")
}

删除后,错误消失了。

撰写版本 1.0.0-beta07

答案 5 :(得分:0)

对于 Kotlin DSL

kotlinOptions {
    jvmTarget = V.JVM.Kotlin.target
    useIR = true
}

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

composeOptions {
    kotlinCompilerExtensionVersion = "1.0.0-beta03"
}