No super method getLifecycle() after migrating to androidx - android

I have been trying to migrate my app to use androidx but I seem to encounter a strange error. From my activity which extends AppCompatActivity when I call getLifeCycle() it throws the following exception
Caused by: java.lang.NoSuchMethodError: No super method getLifecycle()Landroidx/lifecycle/Lifecycle; in class Landroidx/core/app/ComponentActivity; or its super classes
at androidx.fragment.app.FragmentActivity.getLifecycle(FragmentActivity.java:324)
I believe that AppCompatActivity should be implementing LifecycleOwner but its not. Am I doing something wrong?
Here's my gradle dependencies
implementation files("libs/jsoup-1.8.3.jar")
implementation "com.github.philjay:MPAndroidChart:v3.0.2"
implementation 'androidx.gridlayout:gridlayout:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.android.material:material:1.1.0-alpha01'
implementation "androidx.constraintlayout:constraintlayout:2.0.0-alpha2"
implementation 'androidx.constraintlayout:constraintlayout-solver:2.0.0-alpha2'
implementation 'androidx.cardview:cardview:1.0.0'
implementation "com.google.firebase:firebase-messaging:17.3.4"
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.0.1'
implementation "androidx.lifecycle:lifecycle-runtime:2.0.0"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.0.0" // use kapt for Kotlin
implementation "de.hdodenhof:circleimageview:2.2.0"
implementation 'androidx.core:core:1.1.0-alpha01'
implementation "com.thoughtbot:expandablerecyclerview:1.0"
implementation "androidx.lifecycle:lifecycle-livedata:2.0.0"
implementation "androidx.lifecycle:lifecycle-viewmodel:2.0.0"
implementation "com.github.franmontiel:FullScreenDialog:1.0.1"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "com.github.apl-devs:appintro:v4.2.3"
implementation "com.google.firebase:firebase-crash:16.2.1"
implementation "com.google.firebase:firebase-core:16.0.5"

You should change the androidx.appcompat:appcompat version to 1.1.0-alpha04

Just tried it in verision 1.1.0-alpha01 of androidx.appcompat:appcompat and it is now working.
The LifecycleOwner interface is now implemented by ComponentActivity which is extended by FragmentActivity -> AppCompatActivity. You should now be able to get the lifecycle object from your activities.

Reverting back to version 1.0.1 works for me.
implementation "androidx.core:core:1.0.1"

In my case my problem was with the version 1.0.0-alpha05 of androidx activity, I revert to 1.0.0-alpha03 and it works fine

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