java.lang.AssertionError: CALL 'public final fun <get-currentComposer> - android

I'm trying to make a project using jetPack Compose , i have all necessary dependencies for it to work but when i run my app , it throws an error that i couldn't find a solution for
This is the error :
java.lang.AssertionError: CALL 'public final fun <get-currentComposer> (): androidx.compose.runtime.Composer<*> declared in androidx.compose.runtime.ComposerKt' type=androidx.compose.runtime.Composer<*> origin=FOR_LOOP_ITERATOR
All dependencies that i added
def jetpackDef = "1.0.0-alpha09"
// Jetpack compose navigation
implementation "androidx.navigation:navigation-compose:1.0.0-alpha04"
implementation "androidx.compose.ui:ui:$jetpackDef"
// Tooling support (Previews, etc.)
implementation "androidx.compose.ui:ui-tooling:$jetpackDef"
// Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
implementation "androidx.compose.foundation:foundation:$jetpackDef"
// Material Design
implementation "androidx.compose.material:material:$jetpackDef"
// Material design icons
implementation "androidx.compose.material:material-icons-core:$jetpackDef"
implementation "androidx.compose.material:material-icons-extended:$jetpackDef"
// Integration with observables
implementation "androidx.compose.runtime:runtime-livedata:$jetpackDef"
implementation "androidx.compose.runtime:runtime-rxjava2:$jetpackDef"
implementation "androidx.compose.runtime:runtime:$jetpackDef"
buildFeatures {
// Enables Jetpack Compose for this module
compose true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
Any Help would be appreciated guys , thank you .

I got it working by settings the composeOptions, I got this error when I had kotlinCompilerExtensionVersion = "1.0.0-alpha08", whilst rest of the libraries where alpha09.
composeOptions {
kotlinCompilerVersion = "1.4.21"
kotlinCompilerExtensionVersion = "1.0.0-alpha09"
}

There seems to be an issue with compose version alpha09, downgrade to alpha08 and it should work.
Source:
https://issuetracker.google.com/issues/176046527

For me JuliusScrip's answer didn't completely solve it: if you use ext.kotlin_version in your build.gradle(:project) file, you'll need to update that too:
buildscript {
ext.kotlin_version = "1.4.21"
...
}
For anyone needing more details, this video is completely about this error: https://www.youtube.com/watch?v=itvv6m4haGY&feature=emb_logo

Related

Unable to compile jetpack compose in multi module app

I am trying to setup jetpack compose in my multi module application.But i am facing so many issues. Below is one of them.
Could not perform incremental compilation: Could not connect to Kotlin compile daemon
Could not connect to kotlin daemon. Using fallback strategy.
exception: java.io.EOFException
implementation 'androidx.activity:activity-compose:1.3.1'
implementation "androidx.compose.ui:ui:$compose_ui_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
implementation 'androidx.compose.material:material:1.2.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
ext {
kotlin_version = '1.6.21'
compose_ui_version = '1.2.0'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.2.0'
}
Your versions are all pretty much out of date. Please have a look at how to set up Compose in the Quick Start page. Using composeBom (2023.01.00) will help you in making sure your versions are correct. There is also the Compose to Kotlin Compatibility Map.
In short:
composeOptions {
kotlinCompilerExtensionVersion = "1.4.0"
}
ext {
kotlin_version = '1.8.0'
compose_compiler_version = '1.4.0'
}

Problem with updating compose version to 1.3.0-alpha01

I have an Android Studio project that works fine when I'm using Kotlin version 1.6.21 and compose version 1.2.0-rc01. The problem arises when I want to update both dependencies to the latest versions, which are 1.7.10 for Kotlin and 1.3.0-alpha01 for compose. The error that I get is:
Could not resolve all files for configuration ':app:kotlin-extension'.
Could not find androidx.compose.compiler:compiler:1.3.0-alpha01.
Searched in the following locations:
https://dl.google.com/dl/android/maven2/androidx/compose/compiler/compiler/1.3.0-alpha01/compiler-1.3.0-alpha01.pom
https://repo.maven.apache.org/maven2/androidx/compose/compiler/compiler/1.3.0-alpha01/compiler-1.3.0-alpha01.pom
Required by:
project :app
Any help?
Edit:
Now I'm using these versions:
kotlinCompilerExtensionVersion '1.7.10'
implementation "androidx.compose.ui:ui:1.3.0-alpha01"
implementation "androidx.compose.material:material:1.3.0-alpha01"
implementation "androidx.compose.compiler:compiler:1.2.0"
Compose compiler and the other compose dependencies have different releases.
Currently the latest version of compose.compiler is 1.3.1.
You easily use different versions in your build.gradle script:
buildscript {
ext {
compose_compiler = '1.3.1'. //compiler
compose_version = '1.2.0'. //stable compose dependencies
compose_alpha = '1.3.0-alpha01' //alpha releases
}
//...
}
And then:
composeOptions {
kotlinCompilerExtensionVersion compose_compiler
}
dependencies {
//stable releases
implementation "androidx.compose.material:material:$compose_version"
//alpha releases
implementation "androidx.compose.ui:ui:$compose_alpha"
implementation "androidx.compose.material3:material3:$compose_alpha"
}
An alternate Compose compiler version can be defined with composeOptions:
android {
composeOptions {
kotlinCompilerExtensionVersion "1.2.0"
}
}
There's no need to add it as an implementation, which it definitely isn't.
runtimeOnly might eventually work, but it won't make it into the package.
There is a compatibility map for "Compose Compiler Version" compatible with "Compatible Kotlin Version" it can be found here:
so if you want to update to today's latest compose you specify:
...
composeOptions {
kotlinCompilerExtensionVersion "1.3.0"
}
...
and then in the map you find the compatible kotlin version which is 1.7.10
and set that in the other gradle file:
plugins {
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
...
}
I am using compose_version is 1.3.0-beta01 which is working fine for me. You may use like below:
Step1: Project build.gadle is look like below:
build script {
ext {
compose_version = '1.3.0-beta01'
}
}
plugins {
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}
Step2: module build.gadle looks like
composeOptions {
kotlinCompilerExtensionVersion compose_version
kotlinCompilerVersion '1.5.21'
}

Jetpack Compose Build Issue

I keep getting this error while trying to build the project. I have tried all of the other solutions which I found on stackoverflow regarding similar issue with Jetpack compose. Have anyone been able to build succussfully as of Jan 10, 2022. Thank You!
Could not resolve all files for configuration ':app:kotlin-extension'.
Could not find androidx.compose:compose-compiler:1.0.5.
Searched in the following locations:
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerVersion = "1.4.21-2"
// kotlinCompilerExtensionVersion = "1.0.0-alpha11"
// kotlinCompilerExtensionVersion "1.1.0-rc02"
kotlinCompilerExtensionVersion "1.0.5"
}
implementation "androidx.compose.runtime:runtime:1.1.0-rc01"
implementation "androidx.compose.ui:ui:1.0.5"
// Tooling support (Previews, etc.)
implementation "androidx.compose.ui:ui-tooling:1.0.5"
// Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
implementation "androidx.compose.foundation:foundation:1.0.5"
// Material Design
implementation "androidx.compose.material:material:1.0.5"
// Material design icons
implementation "androidx.compose.material:material-icons-core:1.0.5"
implementation "androidx.compose.material:material-icons-extended:1.0.5"
// Integration with observables
implementation "androidx.compose.runtime:runtime-livedata:1.0.5"
implementation "androidx.compose.runtime:runtime-rxjava2:1.0.5" ```

Jetpack Compose Preview not showing

I seem to be having trouble with Preview in compose, the layout panel doesn't appear when I annotate a compose method with #preview. I assume I'm missing a dependency, but I've copied and pasted the code from here https://developer.android.com/jetpack/compose/setup. Any suggestions? (tried the usual clear cache, reopen project etc) :)
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.0.0-alpha10'
kotlinCompilerVersion '1.4.21'
}
}
dependencies {
implementation 'androidx.compose.ui:ui:1.0.0-alpha10'
// Tooling support (Previews, etc.)
implementation 'androidx.compose.ui:ui-tooling:1.0.0-alpha10'
// Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
implementation 'androidx.compose.foundation:foundation:1.0.0-alpha10'
// Material Design
implementation 'androidx.compose.material:material:1.0.0-alpha10'
// Material design icons
implementation 'androidx.compose.material:material-icons-core:1.0.0-alpha10'
implementation 'androidx.compose.material:material-icons-extended:1.0.0-alpha10'
// Integration with observables
implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-alpha10'
implementation 'androidx.compose.runtime:runtime-rxjava2:1.0.0-alpha10'
// UI Tests
androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.0.0-alpha10'
implementation 'com.google.android.material:material:1.2.1'
}
Here is my attempt at using preview (in AS it says Function "DefaultPreview" is never used)
import androidx.compose.ui.tooling.preview.Preview
.....
#Preview
#Composable
fun DefaultPreview() {
Text(text = "Hello!")
}
buildFeatures {
compose true
}
Write the above piece of code inside the Android block in the build.gradle file.
Your problem will then be solved.
For me it was some kind of mixture
Be sure you have latest Android Studio version
Within project/build.gradle be sure you have
dependencies {
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.20'
}
Within app/build.gradle be sure you have
android {
composeOptions {
kotlinCompilerExtensionVersion'1.1.1'
}
buildFeatures {
compose true
}
}
dependencies {
implementation("androidx.compose.ui:ui:1.1.1")
// Tooling support (Previews, etc.)
debugImplementation "androidx.compose.ui:ui-tooling:1.1.1"
implementation "androidx.compose.ui:ui-tooling-preview:1.1.1"
// Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
implementation("androidx.compose.foundation:foundation:1.1.1")
// Material Design
implementation("androidx.compose.material:material:1.1.1")
// Material design icons
implementation("androidx.compose.material:material-icons-core:1.1.1")
implementation("androidx.compose.material:material-icons-extended:1.1.1")
// Integration with observables
implementation("androidx.compose.runtime:runtime-livedata:1.1.1")
implementation("androidx.compose.runtime:runtime-rxjava2:1.1.1")
}
File -> Invalidate Ccaches and restart
Run project once
Very important:
check for correct #Preview import - should be:
import androidx.compose.ui.tooling.preview.Preview
Example:
#Composable
fun SimpleComposable() {
Text("Hello World")
}
#Preview
#Composable
fun ComposablePreview() {
SimpleComposable()
}
#Preview function should be without params.
I'm going to leave this up in case others run into the same issue as I did (it is user error, but I also think the documentation could be clearer).
There are two versions of Android Canary, beta and arctic fox (alpha). Make sure you are using arctic fox if you want to use the latest version of the compose libraries. I've found the compose library 'androidx.compose.ui:ui-tooling:1.0.0-alpha08' (and higher) doesn't work very well with the beta version of canary.
I think you can find something you want in configuration
For me, I missed one more dependency in my module's gradle
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
https://developer.android.com/jetpack/compose/tooling
Updating to the latest version of AS solved the error for me.
Try the latest version from here https://developer.android.com/studio/preview
The Jetpack Compose (rc01, rc02) has an issue with #Preview. You can solve it by adding the following code in your build.gradle file:
android {
...
}
configurations.all {
resolutionStrategy {
force("androidx.compose.ui:ui-tooling:1.0.0-beta09")
force("androidx.compose.ui:ui-tooling-data:1.0.0-beta09")
force("androidx.compose.ui:ui-tooling-preview:1.0.0-beta09")
}
}
dependencies {
...
}
Example: https://github.com/AlexZhukovich/ComposePlayground/blob/main/app/build.gradle
I had to add
debugImplementation 'androidx.customview:customview-poolingcontainer:1.0.0-rc01'
debugImplementation 'androidx.customview:customview:1.1.0'
Source
This is what worked for me add these dependencies if missed any
In the new AS compose runtime is missing and probably moved to something else and added UI tooling from the top when these were added studio started working with preview.
implementation("androidx.compose.runtime:runtime:1.2.0")
implementation("androidx.compose.ui:ui-tooling:1.2.0")
all together
implementation("androidx.activity:activity-compose:1.5.1")
implementation("androidx.compose.ui:ui:1.2.0")
implementation("androidx.compose.runtime:runtime:1.2.0")
implementation("androidx.compose.ui:ui-tooling-preview:1.2.0")
implementation("androidx.compose.ui:ui-tooling:1.2.0")
For me I just didn't have the following in my gradle file:
composeOptions {
kotlinCompilerExtensionVersion '1.0.3'
}
and
buildFeatures {
compose true
}
in project's build.gradle specify:
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"
}
I had the same issue, and also had some compiling problems. My conclusion is that you need a good "variable versions" schema, for example is important use the same version for some dependencies, for example the Kotlin version.
I did something like this project: Jetpack Compose Navigation
where they using a global definition of version variables like this:
buildscript {
ext {
// Sdk and tools
compileSdkVersion = 33
minSdkVersion = 21
targetSdkVersion = 32
// App dependencies
appCompatVersion = '1.5.1'
activityComposeVersion = '1.6.0'
composeCompilerVersion = "1.3.0"
composeVersion = '1.2.1'
coreTestingVersion = '2.1.0'
espressoVersion = '3.4.0'
gradleVersion = '7.2.2'
kotlinVersion = '1.7.10'
ktlintVersion = '0.45.2'
ktxVersion = '1.9.0'
materialVersion = '1.6.1'
navigationComposeVersion = '2.5.2'
}
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
dependencies {
classpath "com.android.tools.build:gradle:$gradleVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath 'com.google.gms:google-services:4.3.14'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.2'
}
}
And then use it in your app build.gradle too:
dependencies {
implementation project(path: ':libtoolscommon')
implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
implementation "androidx.core:core-ktx:$rootProject.ktxVersion"
implementation "com.google.android.material:material:$rootProject.materialVersion"
implementation 'androidx.preference:preference-ktx:1.2.0'
// Compose
implementation "androidx.compose.runtime:runtime:$rootProject.composeVersion"
implementation "androidx.compose.ui:ui:$rootProject.composeVersion"
implementation "androidx.compose.foundation:foundation:$rootProject.composeVersion"
implementation "androidx.compose.material:material:$rootProject.composeVersion"
implementation "androidx.compose.material:material-icons-extended:$rootProject.composeVersion"
implementation "androidx.activity:activity-compose:$rootProject.activityComposeVersion"
implementation "androidx.navigation:navigation-compose:$rootProject.navigationComposeVersion"
debugImplementation "androidx.compose.ui:ui-tooling:$rootProject.composeVersion"
// Testing dependencies
androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
androidTestImplementation "androidx.test.espresso:espresso-contrib:$rootProject.espressoVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$rootProject.espressoVersion"
// Compose testing dependencies
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$rootProject.composeVersion"
debugImplementation "androidx.compose.ui:ui-test-manifest:$rootProject.composeVersion"
implementation "androidx.compose.material3:material3:1.0.0-beta03"
implementation "androidx.compose.material3:material3-window-size-class:1.0.0-beta03"
implementation 'androidx.window:window:1.1.0-alpha03'
---
}
And also try to have the last version of this libraries.
If you have a whole project in Kotlin, it should be like this:
implementation(libs.androidx.core.ktx)
implementation(libs.kotlin.stdlib)
implementation(libs.kotlinx.coroutines.android)
implementation(libs.androidx.compose.ui.tooling.preview)
debugImplementation(libs.androidx.compose.ui.tooling)
implementation(libs.androidx.compose.materialWindow)
implementation(libs.androidx.compose.material.iconsExtended)
implementation(libs.androidx.lifecycle.runtime)
implementation(libs.androidx.lifecycle.viewModelCompose)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.window)
Expect it to show in the 'Split' or 'Design' mode after a build. (Not right over code like it seems in the tutorial.)

When trying jetpack compose it show error: compiler backend and cannot be loaded by the old compiler

Class 'androidx.compose.ui.platform.ComposeView' is compiled by a new Kotlin compiler backend and cannot be loaded by the old compiler
This my onCreate method :
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_delivered, container, false).apply {
findViewById<ComposeView>(R.id.compose_view).setContent {
MaterialTheme {
Surface {
Text("Hello")
}
}
}
}
}
Compose version:
accompanistVersion = "0.1.9"
composeVersion = '0.1.0-dev17'
app.gradle
buildFeatures {
compose true
dataBinding true
}
composeOptions {
kotlinCompilerVersion rootProject.kotlinVersion
kotlinCompilerExtensionVersion rootProject.composeVersion
}
// Compose
implementation "androidx.compose.runtime:runtime:$rootProject.composeVersion"
implementation "androidx.compose.ui:ui:$rootProject.composeVersion"
implementation "androidx.compose.foundation:foundation:$rootProject.composeVersion"
implementation "androidx.compose.foundation:foundation-layout:$rootProject.composeVersion"
implementation "androidx.compose.material:material:$rootProject.composeVersion"
implementation "androidx.compose.ui:ui-viewbinding:$rootProject.composeVersion"
implementation "androidx.ui:ui-tooling:$rootProject.composeVersion"
implementation "androidx.compose.runtime:runtime-livedata:$rootProject.composeVersion"
implementation "com.google.android.material:compose-theme-adapter:$rootProject.composeVersion"
implementation "dev.chrisbanes.accompanist:accompanist-coil:$rootProject.accompanistVersion"
The default app/build.grade, when creating a new project with an "Empty Compose Activity", includes the following options.
android {
// other options
kotlinOptions {
jvmTarget = '1.8'
useIR = true
}
// more options
}
Adding these options (specifically useIR = true) seemed to fix the error for me.
The useIR option references a new JVM backend for Kotlin for which the documentation specifically states the following.
If you enable Jetpack Compose, you will automatically be opted in to the new JVM backend without needing to specify the compiler option in kotlinOptions.
Which is seemingly incorrect.
Please add this task in your app/build.gradle file:
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += ["-Xallow-jvm-ir-dependencies", "-Xskip-prerelease-check"]
}
}
Following the steps in the official setup guide lead me to the same problem.
Adding the necessary dependencies/configuration for the compose library fixed this issue for me.
.kts version
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.freeCompilerArgs = listOf(
*kotlinOptions.freeCompilerArgs.toTypedArray(),
"-Xallow-jvm-ir-dependencies",
"-Xskip-prerelease-check")
useIR = true
}
This is the dependency which causes respective error in my case:
androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.0.5'
My guess that playing with it should help
find kotlin compatible version here
Update dependency in build.gradle
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${version}"
here ${version} is kotlin version.
Configure a suitable JDK version in Gradle Setting.
Run below commands
gradle clean
gradle build
For me, it resolved my issue.
Please let me know if it doesn't work for you.

Categories

Resources