Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiDex优化 #2

Merged
merged 3 commits into from May 27, 2022
Merged

MultiDex优化 #2

merged 3 commits into from May 27, 2022

Conversation

TxcA
Copy link
Contributor

@TxcA TxcA commented May 27, 2022

  1. 既然是 <API21 专用,那么sample的App需继承MultiDexApplication(或其它实现方法),否则必崩MultiDex

  2. 由于app 依赖 Net-okhttp3 仅编译时依赖 okhttp3,导致启动时若直接调用Net大概率会导致NoClassDefFoundError or NoSuchMethodError。原因为MultiDex并不一定100%能处理好复杂依赖情况。最佳解决办法当然是multiDexKeepFile,这样对于用户可以随时更新依赖的okhttp版本。但是对于一个已经基本宣布不再更新的okhttp:3.12.x,这里可以直接使用api来依赖,降低集成难度。square在okhttp:3.12.10时也说明了这个问题 -> Supporting a full decade of Android releases on our 3.12.x branch is tricky!

  3. 由于API21开始才默认开启TLSv1.1和TLSv1.2,所以新增了一个useLegacyTLS()的方法。用于快捷解决javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb82109a0: Failure in SSL library, usually a protocol errorokhttp issues #4053

   NetConfig.initialize(...){
       ...
       useLegacyTLS()
   }

题外话: 我看了下com.drake.net.compatible.OkHttpClient 是2022-04-22 #d26e1068 新增了一些扩展,用于适配okhttp4.x的一些习惯,但是2022-04-30 Net-okhttp3:3.4.6的包里却没有这些扩展...

@TxcA
Copy link
Contributor Author

TxcA commented May 27, 2022

具体测试情况,可查看 API19device 分支的api19 app.

@TxcA
Copy link
Contributor Author

TxcA commented May 27, 2022

未优化前报错堆栈(应该只会在设备为API<21时出现,21后的ART虚拟机应该直接杜绝了这个问题)

D/NET_LOG: java.lang.NoSuchFieldError: okhttp3.HttpUrl.Companion
        at com.drake.net.request.BaseRequest.setPath(BaseRequest.kt:118)
        at com.xxx.xxx.ui.LauncherActivity$onCreate$1$1$invokeSuspend$$inlined$Get$default$1.invokeSuspend(NetCoroutine.kt:29)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
        at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:749)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
scopeNetLife {
    Get<String>("api/users") {
        param("page", "1")
    }.await()
}

@liangjingkanji
Copy link
Owner

useLegacyTLS改为setLegacyTLS会不会更合适

@TxcA
Copy link
Contributor Author

TxcA commented May 27, 2022

useLegacyTLS改为setLegacyTLS会不会更合适

你还是那么扣细节😂
都行,我觉得采用设置,在这里语义差不多。个人更偏向于use。

@liangjingkanji
Copy link
Owner

为了方便回滚我会将README的修改和fixed: 修复API<21时, 开启MultiDex引起的主包依赖异常合并

提交尽量互相不耦合

@liangjingkanji
Copy link
Owner

liangjingkanji commented May 27, 2022

你结尾说的问题我估计是Net-OkHttp使用的是主仓库的apk安装包

我基本上不使用本兼容库所以就图方便了

@TxcA
Copy link
Contributor Author

TxcA commented May 27, 2022

你结尾说的问题我估计是Net-OkHttp使用的是主仓库的apk安装包

我基本上不使用本兼容库所以就图方便了

没用安装包,直接引用的线上3.4.6,截图里可以看出。
具体提交你调整吧。👌

debug 30分钟,复现修复3小时...
主要本地跑不会出现这个问题,本地强依赖了,估计MultiDex合并时可以识别,线上的就出问题了。

@TxcA
Copy link
Contributor Author

TxcA commented May 27, 2022

你结尾说的问题我估计是Net-OkHttp使用的是主仓库的apk安装包

我基本上不使用本兼容库所以就图方便了

我也不用,但是2022年,我都没想到我还要新开一个工程,定向开发Android4.4...

@liangjingkanji
Copy link
Owner

liangjingkanji commented May 27, 2022

估计是物联网设备开发吧, 之前也是因为有人有物联网开发需求才创建本库的

  1. 你有个提交描述是修改MultiDex实际内容是内嵌OkHttp我也修改了
  2. 继续使用useLegacyTLS

你可以再检查一下是否遗漏

@liangjingkanji
Copy link
Owner

修改提交描述规则

@liangjingkanji liangjingkanji merged commit 381a87e into liangjingkanji:okhttp3 May 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants