4

solution from comment by Pylyp Dukhov


I set compose build.gradle(:compose) same as build.gradle(:app)

but preview in (:compose) not showing. It looks like below.

enter image description here

the message bottom of preview on Android-Studio

The following classes could not be instantiated: - androidx.compose.ui.tooling.ComposeViewAdapter (Open Class, Show Exception, Clear Cache) Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE. If this is an unexpected error you can also try to build the project, then manually refresh the layout. Exception Details java.lang.ClassNotFoundException: androidx.customview.poolingcontainer.R$id   at java.lang.ClassLoader.loadClass(ClassLoader.java:589)   at java.lang.ClassLoader.loadClass(ClassLoader.java:522)   at androidx.customview.poolingcontainer.PoolingContainer.   ... (PoolingContainer.kt:121)   at androidx.compose.ui.platform.ViewCompositionStrategy$DisposeOnDetachedFromWindowIfNotInPoolingContainer.installFor(ViewCompositionStrategy.android.kt:97)   at androidx.compose.ui.platform.AbstractComposeView.(ComposeView.android.kt:123)   at androidx.compose.ui.platform.ComposeView.(ComposeView.android.kt:392)   at androidx.compose.ui.platform.ComposeView.(ComposeView.android.kt:388)   at androidx.compose.ui.tooling.ComposeViewAdapter.(ComposeViewAdapter.kt:131)   at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(NativeConstructorAccessorImpl.java:-2)   at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)   at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)   at java.lang.reflect.Constructor.newInstance(Constructor.java:490)   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)   at android.view.LayoutInflater.inflate(LayoutInflater.java:663)   at android.view.LayoutInflater.inflate(LayoutInflater.java:505) Copy stack to clipboard

someday before, when I set compose_version = 1.1.1(commented) in build.gradle(:compose) it worked but after moveing git branch or quit Android-Studio, it dosen't today.

build.gradle(:compose)

plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk target_sdk

    defaultConfig {
        minSdk min_sdk
        targetSdk target_sdk

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled true
            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
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

//    def  compose_version =  "1.1.1"
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation 'androidx.activity:activity-compose:1.4.0'
    implementation("io.coil-kt:coil-compose:1.4.0")

    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1"

    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"

    implementation "com.google.accompanist:accompanist-flowlayout:0.24.7-alpha"
}

This preview worked well in :app(module)

enter image description here

I searched and tried stackoverflow answers that related but not found solution.

liveAnyway
  • 629
  • 6
  • 15

0 Answers0