leakcanary icon indicating copy to clipboard operation
leakcanary copied to clipboard

Update version of lifecycle-viewmodel dependency that LeakCanary compiles against

Open mauriciogg opened this issue 1 year ago • 5 comments

Description

Hello,

We are encountering an issue related to https://github.com/square/leakcanary/issues/2314 that manifest itself as a build issue rather than a runtime issue when building with Bazel as opposed to Gradle so 2108693 does not fix the issue. Under bazel there is an extra check to ensure that desugaring wont result in runtime issues like the one fixed in 2108693. This is a tricky one to fix on the build system since com.squareup.leakcanary:leakcanary-object-watcher-android-androidx does not declare any dependency on androidx (i understand this is the correct behavior). Because of this, we have to possible solutions: 1) manually craft the target for leakcanary we use in bazel to depend on the androidx library or 2) disable the checks alltogether. Both are undesirable for different reasons. Wouldnt adding a default implementation for create(modelClass, extras that simply calls create(modelClass) here fix the issue both at build time and runtime? Can https://github.com/square/leakcanary/issues/2314 be reopened?

Steps to Reproduce

Building an android_binary target in bazel that depends on androidx.lifecycle:lifecycle-viewmodel (version >= 2.5.1) / @mvn//:androidx_lifecycle_lifecycle_viewmodel com.squareup.leakcanary:leakcanary_android_core / @mvn//:com_squareup_leakcanary_leakcanary_android_core

will result in the following failure: singlejar_local: androidx/lifecycle/ViewModelProvider$Factory needed on the classpath for desugaring leakcanary/internal/ViewModelClearedWatcher$Companion$install$provider$1. Please add the missing dependency to the target containing the latter.

Expected behavior: [What you expect to happen] Build succeeds

Version Information

  • LeakCanary version: 2.10
  • Android OS version: <24
  • Gradle version: n/a

mauriciogg avatar Mar 08 '23 01:03 mauriciogg

@mauriciogg Can you open a PR? Would be much easier if you come up with the fix for your specific issue that you know works

pyricau avatar Mar 20 '23 23:03 pyricau

@mauriciogg Can you open a PR? Would be much easier if you come up with the fix for your specific issue that you know works @pyricau looks like this is more effort than initially anticipated, in order to fix this the viewmodel dependency needs to be updated in order to bring androidx.lifecycle.viewmodel.CreationExtras and that triggers a bunch of other issues. We have a workaround so its not high priority for us. I can close this or leave it open and address once the viewmodel version is updated.

mauriciogg avatar Mar 21 '23 22:03 mauriciogg

the viewmodel dependency needs to be updated and that triggers a bunch of other issues.

What issues?

Does the generated bytecode end up not being compatible with older versions of the lifecycle library?

pyricau avatar Mar 22 '23 05:03 pyricau

the viewmodel dependency needs to be updated and that triggers a bunch of other issues.

What issues?

Does the generated bytecode end up not being compatible with older versions of the lifecycle library?

Different version of the kotlin compiler used to compile the lib:

I'm updating androidX-fragment from 1.0.0 to 1.5.1 here which is the first version to depend on lifecycle-viewmodel which includes the class androidx.lifecycle.viewmodel.CreationExtras needed to override the new create method. This results in the following error:

> Task :leakcanary-object-watcher-android-androidx:compileDebugKotlin FAILED
w: Runtime JAR files in the classpath should have the same version. These files were found in the classpath:
    /Users/mgalindo/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk8/1.6.0/baf82c475e9372c25407f3d132439e4aa803b8b8/kotlin-stdlib-jdk8-1.6.0.jar (version 1.6)
    /Users/mgalindo/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.6.0/da6bdc87391322974a43ccc00a25536ae74dad51/kotlin-stdlib-jdk7-1.6.0.jar (version 1.6)
    /Users/mgalindo/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.4.21/4a668382d7c38688d3490afde93b6a113ed46698/kotlin-stdlib-1.4.21.jar (version 1.4)
    /Users/mgalindo/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.4.21/7f48a062aa4b53215998780f7c245a4276828e1d/kotlin-stdlib-common-1.4.21.jar (version 1.4)
w: Some runtime JAR files in the classpath have an incompatible version. Consider removing them from the classpath
e: Incompatible classes were found in dependencies. Remove them from the classpath or use '-Xskip-metadata-version-check' to suppress errors
e: /Users/mgalindo/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.6.0/da6bdc87391322974a43ccc00a25536ae74dad51/kotlin-stdlib-jdk7-1.6.0.jar!/META-INF/kotlin-stdlib-jdk7.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1.
e: /Users/mgalindo/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk8/1.6.0/baf82c475e9372c25407f3d132439e4aa803b8b8/kotlin-stdlib-jdk8-1.6.0.jar!/META-INF/kotlin-stdlib-jdk8.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1.
e: /Users/mgalindo/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlinx/kotlinx-coroutines-android/1.6.1/4e61fcdcc508cbaa37c4a284a50205d7c7767e37/kotlinx-coroutines-android-1.6.1.jar!/META-INF/kotlinx-coroutines-android.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1.
e: /Users/mgalindo/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlinx/kotlinx-coroutines-core-jvm/1.6.1/97fd74ccf54a863d221956ffcd21835e168e2aaa/kotlinx-coroutines-core-jvm-1.6.1.jar!/META-INF/kotlinx-coroutines-core.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1.

I'm not super familiar with gradle so I might not be doing the right thing though.

mauriciogg avatar Mar 22 '23 18:03 mauriciogg

ah so looks like we'd need to bump the kotlin version of LeakCanary first, which implies new major release. That's something I do want to do but it'll take time.

pyricau avatar Mar 23 '23 13:03 pyricau