Ad

Android Studio Build Error - Unable To Preview UI In Preview Window

Frankly speaking, I don't have the knowledge to understand and debug this error. So please inform me if I miss something.

How am I getting to this error:

  1. I wanted to work with compose and see the preview in preview window
  2. I created a new android studio project with empty compose activity
  3. The build was successful but the preview isn't loading. It is just showing "build and refresh" button there.
  4. When I click on the "build and refresh" button android studio starts the build process again and fails with the following message:
FAILURE: Build failed with an exception. 

What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details

on seeing the logs, it still doesn't give me any concrete idea of the problem:

Runtime JAR files in the classpath should have the same version. These files were found in the classpath:
    /Users/user1/.gradle/caches/transforms-3/ece73d8c62f06068285ab9f622839930/transformed/jetified-kotlin-stdlib-jdk8-1.5.30.jar (version 1.5)
    /Users/user1/.gradle/caches/transforms-3/eb2652034b57731d1f9e2d78d329ace4/transformed/jetified-kotlin-stdlib-jdk7-1.5.30.jar (version 1.5)
    /Users/user1/.gradle/caches/transforms-3/201581d8ae804b86784bd93bdadbd711/transformed/jetified-kotlin-stdlib-1.6.10.jar (version 1.6)
    /Users/user1/.gradle/caches/transforms-3/b9df9c1454100a3c8c23b28a1d48a919/transformed/jetified-kotlin-stdlib-common-1.6.10.jar (version 1.6)

My app level build.gradle file:

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

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.composebasics"
        minSdk 23
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary 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'
        useIR = true
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

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'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1'
    implementation 'androidx.activity:activity-compose:1.4.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"

and my project level build.gradle file is:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext {
        compose_version = '1.1.0'
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

I don't know how much and how relevant is the information above, but I can add more if required.

Ad

Answer

In project level gradle change kotlin version for compatibility with compose 1.1.0

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"

Check this version compatibility

Ad
source: stackoverflow.com
Ad