liangjingkanji / Net

Android 基于协程/OkHttp网络请求工具
http://liangjingkanji.github.io/Net/
MIT License
1.91k stars 214 forks source link

lifecycle版本升级后报错 #231

Closed qwer2y closed 6 months ago

qwer2y commented 6 months ago

问题描述

lifecycle版本升级到2.8.0之后运行报错,2.7.0没问题 错误信息如下:

java.lang.NoSuchMethodError: No virtual method setTagIfAbsent(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object; in class Landroidx/lifecycle/ViewModel; or its super classes (declaration of 'androidx.lifecycle.ViewModel' appears in /data/app/~~7JuOQ-oMpwQg7WmqajuK3A==/com.demo.test-dfkSlRfuBzB5vTH99OiBMg==/base.apk!classes32.dex)
                    at androidx.lifecycle.ScopeKt.scopeNetLife(Scope.kt:54)
                    at androidx.lifecycle.ScopeKt.scopeNetLife$default(Scope.kt:49)
                    at com.haller.traveler.ui.fragment.HomeFragment$createObserver$2.invoke(HomeFragment.kt:190)
                    at com.haller.traveler.ui.fragment.HomeFragment$createObserver$2.invoke(HomeFragment.kt:77)
                    at com.drake.brv.PageRefreshLayout.onRefresh(PageRefreshLayout.kt:650)
                    at com.scwang.smart.refresh.layout.SmartRefreshLayout$2.onAnimationEnd(SmartRefreshLayout.java:1298)
                    at android.animation.Animator$AnimatorListener.onAnimationEnd(Animator.java:719)
                    at android.animation.Animator$AnimatorCaller$$ExternalSyntheticLambda1.call(Unknown Source:4)
                    at android.animation.Animator.callOnList(Animator.java:677)
                    at android.animation.Animator.notifyListeners(Animator.java:616)
                    at android.animation.Animator.notifyEndListeners(Animator.java:641)
                    at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1306)
                    at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1585)
                    at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:328)
                    at android.animation.AnimationHandler.-$$Nest$mdoAnimationFrame(Unknown Source:0)
                    at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:86)
                    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1550)
                    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1561)
                    at android.view.Choreographer.doCallbacks(Choreographer.java:1117)
                    at android.view.Choreographer.doFrame(Choreographer.java:992)
                    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1535)
                    at android.os.Handler.handleCallback(Handler.java:958)
                    at android.os.Handler.dispatchMessage(Handler.java:99)
                    at android.os.Looper.loopOnce(Looper.java:257)
                    at android.os.Looper.loop(Looper.java:368)
                    at android.app.ActivityThread.main(ActivityThread.java:8826)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:572)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)

期望行为

如何复现

fork仓库并复现问题可以快速解决, 猜测只会让问题晦涩难懂, 耽误所有人时间

截图

异常堆栈信息或者手机截图/视频(拖拽到输入框即可上传)

版本

liangjingkanji commented 6 months ago

你fork仓库复现问题吧, demo里面并没有单独引入此库

liangjingkanji commented 6 months ago

看问题是为了适配ViewModel, Net使用了内部函数(非反射), 但是新版本好像删除了该方法

qwer2y commented 6 months ago

确实我发现是我单独引入了lifecycle一系列库的原因,demo中只使用了androidx.appcompat:appcompat库。现在demo中的appcompat库版本是1.3.1,对应的lifecycle版本是2.3.1,将appcompat升级到最新的1.6.1后对应的版本是2.5.1。

lifecycle库在2.8.0使用kotlin重写了并且删除了setTagIfAbsent()方法,报错在Scope文件里的三个扩展方法,估计以后应该还是得适配

https://developer.android.com/jetpack/androidx/releases/lifecycle?hl=zh-cn#2.8.0 https://issuetracker.google.com/issues/322407038

shanlinbawang commented 5 months ago

别关闭啊,lifecycle总要升级的,升级后就报错了,以后还是得适配呢。。。。