Android - Jetpack compose missing references - android

I am trying to follow the official google Jetpack compose tutorial on this url but Android studio have an issue resolving the modifier Height()
I am 100% sure that I have imported the correct dependencies as per the tutorial, however, this modifier is no where to be found. It is not deprecated as it is still references in the official docs here
fun NewsStory() {
val image = +imageResource(R.drawable.header)
Column(
modifier = Spacing(16.dp)
) {
Container(modifier = Height(180.dp) wraps Expanded) {
DrawImage(image)
}
HeightSpacer(16.dp)
Text("A day in Shark Fin Cove")
Text("Davenport, California")
Text("December 2018")
}
}
Am I missing something?

I assume you are using dev02(because Google's documentation is not updated) dependency for ui-layout dependency, because Height() is added in dev03. Change your ui-layout dependency to dev03:
implementation 'androidx.ui:ui-layout:0.1.0-dev03'
It should work

I needed to add the following to get things running:
//app/build.gradle
...
dependencies {
...
//Jetpack Compose
implementation 'androidx.compose:compose-compiler:0.1.0-dev09'
implementation 'androidx.compose:compose-runtime:0.1.0-dev09'
implementation 'androidx.ui:ui-layout:0.1.0-dev09'
implementation 'androidx.ui:ui-material:0.1.0-dev09'
implementation 'androidx.ui:ui-tooling:0.1.0-dev09'
implementation 'androidx.ui:ui-framework:0.1.0-dev09'
}

This is my setup as far as Jetpack Compose Dependencies go. You don't need all of them so feel free to skip the super specific ones - https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example/blob/master/build.gradle#L24
Pasting it here for reference
'compose': [
'composeComplier': "androidx.compose:compose-compiler:${versions.compose}",
'composeRuntime': "androidx.compose:compose-runtime:${versions.compose}",
'core': "androidx.ui:ui-core:${versions.compose}",
'foundation': "androidx.ui:ui-foundation:${versions.compose}",
'tooling': "androidx.ui:ui-tooling:${versions.compose}",
'layout': "androidx.ui:ui-layout:${versions.compose}",
'material': "androidx.ui:ui-material:${versions.compose}",
'savedInstanceState': "androidx.ui:ui-saved-instance-state:${versions.compose}",
'uiTest': "androidx.ui:ui-test:${versions.compose}",
'uiLiveData': "androidx.ui:ui-livedata:${versions.compose}"
],

As Geoff Langenderfer said changing to dev09 from dev03 worked.
//app/build.gradle
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.3.72"
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.ui:ui-framework:0.1.0-dev09'
implementation 'androidx.ui:ui-layout:0.1.0-dev09'
implementation 'androidx.ui:ui-material:0.1.0-dev09'
implementation 'androidx.ui:ui-tooling:0.1.0-dev09'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

try
import androidx.compose.ui.Modifier

Related

Usage of TextField Causes NochSuchFieldError

Simply adding a TextField to a composable in Android causes a NoSuchFieldError when executing. The application works perfectly fine when I remove the TextField (the application does not contain another TextField so far).
I think it might be a dependency issue (hence the dependencies below). But I am at my wits end. What is happening?
I have already tried to invalidate cache and restart, as well as clean and rebuild the application.
Code:
TextField(
value = "text",
onValueChange = {
}
)
Error:
java.lang.NoSuchFieldError: No static field $stable of type I in class Landroidx/compose/foundation/text/KeyboardActions; or its superclasses (declaration of 'androidx.compose.foundation.text.KeyboardActions' appears in /data/app/~~uEpWT9N7xCnbk8jjsQk_yg==/xx.xxx.xxx.dev-Q5085dpAd5iQ0tK6W7b8rA==/base.apk)
at androidx.compose.material.TextFieldKt.TextField(TextField.kt:207)
Dependencies:
dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation "androidx.compose.ui:ui:1.3.3"
implementation "androidx.compose.material:material:1.3.1"
implementation "androidx.compose.material:material-icons-extended:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:1.3.3"
implementation "androidx.navigation:navigation-compose:2.5.3"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation 'androidx.activity:activity-compose:1.6.1'
implementation 'androidx.paging:paging-common-ktx:3.1.1'
implementation "androidx.paging:paging-compose:1.0.0-alpha17"
implementation "com.google.accompanist:accompanist-systemuicontroller:0.29.0-alpha"
implementation "org.jetbrains.lets-plot:lets-plot-kotlin:3.0.0"
implementation "androidx.room:room-runtime:2.5.0"
implementation "androidx.room:room-paging:2.5.0"
annotationProcessor "androidx.room:room-compiler:2.5.0"
kapt "androidx.room:room-compiler:2.5.0"
testImplementation 'junit:junit:5.8.1'
testImplementation "io.mockk:mockk:1.13.4"
testImplementation 'org.junit.jupiter:junit-jupiter'
debugImplementation "androidx.compose.ui:ui-tooling:1.3.3"
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation "io.insert-koin:koin-android:3.2.0"
implementation "io.insert-koin:koin-androidx-compose:3.2.0"
}
Your accompanist version does not match the compose UI version.
As you can see in this page, if you use compose UI 1.3.X you should use accompanist 0.28.0

Why would I use $version instead of '2.7.0'?

I would like to understand the practise of not putting the version codes in your dependencies.
Why would I choose one practise over the other:
dependencies {
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-moshi:$moshi_version"
implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
}
vs
dependencies {
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-moshi:2.9.0"
implementation "com.squareup.okhttp3:okhttp:4.9.0"
}
Simply because it may be that the version is important for several dependencies. For example, like here:
implementation "androidx.navigation:navigation-runtime-ktx:$navigation_version"
implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
So you only need to adjust the version once and don't run the risk of forgetting a change.

java.lang.NoSuchMethodError startRestartGroup after upgrading Jetpack Compose to 1.0.0‑beta07

I just updated the compose version to 1.0.0‑beta07, and its showing this run time error
No interface method startRestartGroup(ILjava/lang/String;)Landroidx/compose/runtime/Composer; in class Landroidx/compose/runtime/Composer; or its super classes (declaration of 'androidx.compose.runtime.Composer'
....
at com.google.accompanist.coil.CoilImage__CoilKt.CoilImage(Coil.kt:245)
at com.google.accompanist.coil.CoilImage.CoilImage(Coil.kt:1)
below is my gradle dependencies file
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation "androidx.compose.compiler:compiler:$compose_version"
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation "androidx.compose.runtime:runtime:$compose_version"
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
implementation 'androidx.activity:activity-compose:1.3.0-alpha08'
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha05"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation "io.coil-kt:coil-gif:1.2.1"
implementation "io.coil-kt:coil-svg:1.2.1"
implementation "com.google.accompanist:accompanist-coil:0.6.2"
and version compose_version = '1.0.0-beta07'
Every library needs to be recompiled against beta07 to work, as per the Compose release notes:
Note: Libraries dependent on Compose will need to recompile with version 1.0.0‑beta07. Otherwise, libraries may encounter a NoSuchMethodError, such as:
java.lang.NoSuchMethodError: No interface method startReplaceableGroup(ILjava/lang/String;)V in class Landroidx/compose/runtime/Composer; or its super classes. (Ia34e6)
In your case you have to update the accompanist libraries with the 0.10.0.
Change:
implementation "com.google.accompanist:accompanist-coil:0.6.2"
to
implementation "com.google.accompanist:accompanist-coil:0.10.0"
Coil, Glide and Image Loading Core have been removed
After being deprecated in v0.14.0, it's time to remove the coil, glide and imageloading-core libraries. Some discussion about this can be seen on the Kotlin Slack: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1627482619344000. the recommendation is to move to https://coil-kt.github.io/coil/compose/
So Change ;
implementation "com.google.accompanist:accompanist-coil:0.6.2"
to
implementation("io.coil-kt:coil-compose:1.3.2")
Also;
CoilImage removed too, you can use;
Image(
painter = rememberImagePainter("https://www.example.com/image.jpg"),
contentDescription = null,
modifier = Modifier.size(128.dp)
)
Make sure that all your compose libraries are all using the same version 1.0.0-beta07. That should resolve your issue.

Why does the new LiveData builder from Kotlin May update not working in my project

Here are my dependencies:
//Room and Lifecycle Libraries
kapt "androidx.room:room-compiler:2.2.0-alpha02"
kapt 'androidx.room:room-compiler:2.2.0-alpha02'
kapt "androidx.lifecycle:lifecycle-compiler:2.2.0-alpha03"
implementation "androidx.room:room-runtime:2.2.0-alpha02"
implementation 'androidx.room:room-runtime:2.2.0-alpha02'
implementation "androidx.lifecycle:lifecycle-viewmodel:2.2.0-alpha03"
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0-alpha03"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha03"
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha03"
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
As you can see, I implemented the final versions and yet I do not have access to the liveData builder. How can I fix this?
From the documentation:
For liveData, use androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha01 or higher.
So you're just missing the dependency:
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha03"

After migrating to AndroidX, Error inflating class android.support.design.widget.AppBarLayout

After using Android Studio to migrate my project to AndroidX (and manually fixing a lot of import errors), I'm getting no compile errors but when the app starts I get a crash with:
Error inflating class android.support.design.widget.AppBarLayout.
The offending line in the layout file is:
<android.support.design.widget.AppBarLayout
My dependencies in build.gradle are:
dependencies {
def lifecycle_version = '2.1.0-alpha02'
// used below--will be different for androidx (migrated 2019-02-04)
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha03'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.2-alpha01'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.2-alpha01'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.okio:okio:1.15.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.5'
implementation 'com.jakewharton.rxrelay2:rxrelay:2.1.0'
// Relay class
implementation 'com.jakewharton.rx2:replaying-share:2.1.0'
// ReplayingShare
implementation 'com.jakewharton.rxbinding2:rxbinding:2.2.0'
// RxBinding
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" // see def above
// includes ViewModel and LiveData
implementation 'org.apache.commons:commons-lang3:3.8.1'
// for tuples like Triple
implementation 'com.androidplot:androidplot-core:1.5.6'
// AndroidPlot
}
I'm guessing that I'm missing something but I can't find what it is.
You need to use com.google.android.material.appbar.AppBarLayout.
Version 1.0.0 is already out So you can use implementation 'androidx.appcompat:appcompat:1.0.0'
Add dependency implementation 'com.google.android.material:material:1.0.0'
See Material Component integration for latest release version.
And use
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.google.android.material.appbar.AppBarLayout>
For other artifact and Class Mapping see the AndroidX migration Doc.
Please go through this old to new class mappings
eg;- Use com.google.android.material.appbar.AppBarLayout instead of android.support.design.widget.AppBarLayout
For AppBarLayout
For Toolbar
Androidx inflating class <android.support.design.widget.TabLayout/> will not work
it's not exist so replace it with <com.google.android.material.tabs.TabLayout/>
it will work fine
and don't forgot to add
implementation 'com.google.android.material:material:1.1.0-alpha07'
to your dependencies
According to the AndroidX migration docs, the androidx replacement for AppBarLayout is com.google.android.material.appbar.AppBarLayout. Try replacing your AppBarLayout tag with this instead.
As for why compiling/building works, I assume it's something to do with Jetifier, but I'm not certain.
if you are using Kotlin DSL you have to add this to build.gradle.kts
implementation("com.google.android.material:material:1.1.0")
implementation("androidx.appcompat:appcompat:1.1.0")
and in your layout use <com.google.android.material.appbar.AppBarLayout/>

Categories

Resources