@Preview不起作用-Jetpack Compose 1.0.0-alpha02

时间:2020-09-03 20:57:39

标签: android kotlin android-jetpack android-jetpack-compose

在dev-015版本中,我一直使用预览效果很好,但是当我更新到alpha-01和alpha-02时,它停止工作,我重新启动,清理了缓存,重新构建,问题仍然存在

这是我的代码

val recipeList = listOf(Recipe(R.drawable.header,"Test", listOf("Azucar","Tomate","lasagna")),
        Recipe(R.drawable.header,"Test", listOf("Azucar","Tomate","lasagna")),
        Recipe(R.drawable.header,"Test", listOf("Azucar","Tomate","lasagna")))


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            RecipeList(recipeList)
        }
    }
}

@Composable
fun RecipeCard(recipe: Recipe) {
    val image = imageResource(R.drawable.header)
    Column(modifier = Modifier.padding(16.dp)) {
        val imageModifier = Modifier
                .preferredHeight(150.dp)
                .fillMaxWidth()
                .clip(shape = RoundedCornerShape(8.dp))

        Image(image,modifier= imageModifier, contentScale = ContentScale.Crop)
        Spacer(Modifier.preferredHeight(16.dp))
        Text(recipe.title, style = typography.h6)
        for(ingredient in recipe.ingredients){
            Text(ingredient,style = typography.body2)
        }
    }
}

@Composable
fun RecipeList(recipeList:List<Recipe>){
    LazyColumnFor(recipeList) { item ->
        RecipeCard(recipe = item)
    }
}

@Preview
@Composable
fun RecipePreview(){
    RecipeCard(recipeList[0])
}

data class Recipe(
        @DrawableRes val imageResource: Int,
        val title: String,
        val ingredients: List<String>
)

这是我的依赖项

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.example.jetexample"
        minSdkVersion 21
        targetSdkVersion 30
        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 JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
        kotlinCompilerVersion kotlin_version
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
    implementation 'com.google.android.material:material:1.2.0'

    //Fundamental building blocks of Compose's programming model and state management, and core runtime for the Compose Compiler Plugin to target.
    implementation "androidx.compose.runtime:runtime:$compose_version"

    //Fundamental components of compose UI needed to interact with the device, including layout, drawing, and input.
    implementation "androidx.compose.ui:ui:$compose_version"

    //Build Jetpack Compose UIs with ready to use Material Design Components. This is the higher level entry point of Compose, designed to provide components that match those described at www.material.io.
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.material:material-icons-extended:$compose_version"

    //Write Jetpack Compose applications with ready to use building blocks and extend foundation to build your own design system pieces.
    implementation "androidx.compose.foundation:foundation:$compose_version"
    implementation "androidx.compose.foundation:foundation-layout:$compose_version"

    //Build animations in their Jetpack Compose applications to enrich the user experience.
    implementation "androidx.compose.animation:animation:$compose_version"

    //In charge of annotators like @Preview and tooling for render views
    implementation "androidx.ui:ui-tooling:$compose_version"

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

和我的项目gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.example.jetexample"
        minSdkVersion 21
        targetSdkVersion 30
        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 JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
        kotlinCompilerVersion kotlin_version
    }
}

    dependencies {
    
        implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
        implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
        implementation 'com.google.android.material:material:1.2.0'
    
        //Fundamental building blocks of Compose's programming model and state management, and core runtime for the Compose Compiler Plugin to target.
        implementation "androidx.compose.runtime:runtime:$compose_version"
    
        //Fundamental components of compose UI needed to interact with the device, including layout, drawing, and input.
        implementation "androidx.compose.ui:ui:$compose_version"
    
        //Build Jetpack Compose UIs with ready to use Material Design Components. This is the higher level entry point of Compose, designed to provide components that match those described at www.material.io.
        implementation "androidx.compose.material:material:$compose_version"
        implementation "androidx.compose.material:material-icons-extended:$compose_version"
    
        //Write Jetpack Compose applications with ready to use building blocks and extend foundation to build your own design system pieces.
        implementation "androidx.compose.foundation:foundation:$compose_version"
        implementation "androidx.compose.foundation:foundation-layout:$compose_version"
    
        //Build animations in their Jetpack Compose applications to enrich the user experience.
        implementation "androidx.compose.animation:animation:$compose_version"
    
        //In charge of annotators like @Preview and tooling for render views
        implementation "androidx.ui:ui-tooling:$compose_version"
    
        testImplementation 'junit:junit:4.+'
        androidTestImplementation 'androidx.test.ext:junit:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    }
    
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions {
            // Treat all Kotlin warnings as errors
            allWarningsAsErrors = true
            freeCompilerArgs += '-Xopt-in=kotlin.RequiresOptIn'
            // Enable experimental coroutines APIs, including Flow
            freeCompilerArgs += '-Xopt-in=kotlin.Experimental'
            freeCompilerArgs += '-Xallow-jvm-ir-dependencies'
    
            // Set JVM target to 1.8
            jvmTarget = "1.8"
        }
    }

我的模拟器刚刚显示了这个

我已经尝试了一切,但似乎并没有为我渲染预览,每次更新时我都在为jetpack撰写而苦苦挣扎,但似乎无法在API的每次更新中使其稳定工作,有人知道吗发生在这里吗?

谢谢

0 个答案:

没有答案