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

release 包混淆后,请求接口提示 Missing type parameter #396

Closed
appdev opened this issue Aug 9, 2022 · 3 comments
Closed

release 包混淆后,请求接口提示 Missing type parameter #396

appdev opened this issue Aug 9, 2022 · 3 comments

Comments

@appdev
Copy link

appdev commented Aug 9, 2022

项目接口、Parser相关配置都使用 Kotlin 开发
gradle 配置文件中,开启混淆后,访问任何接口都会提示 Missing type parameter。关闭混淆正常。

日志可以看到。解析器中没有泛型
开启后,没有泛型的
image
关闭混淆后,带有泛型的
image

@liujingxing
Copy link
Owner

经过一番筛查,发现是打包时,开启混淆优化导致的,开启后,inline + reified方法内部,获取泛型类型会失败,这应该属于kotlin的一个bug,暂时有以下两个解决方案:

  • 添加-dontoptimizeproguard-rules.pro文件中
  • 使用proguard-android.txt替代proguard-android-optimize.txt文件

以上两个方案,都会关闭混淆优化,google官方的建议是使用第二种关闭混淆优化。

至于为啥开启优化后,获取泛型类型为啥会失败,我在proguard-android-optimize.txt文件中,找到这么一段话

Adding optimization introduces certain risks, since for example not all optimizations performed by ProGuard works on all versions of Dalvik. The following flags turn off various optimizations known to have issues, but the list may not be complete or up to date. (The "arithmetic" optimization can be used if you are only targeting Android 2.0 or later.) Make sure you test thoroughly if you go this route.

简单翻译下,就是添加优化会带来一定的风险,如果开启,就一定要详细测试

在下个版本中,会使用typeOf方法获取泛型类型,以规避这个问题

@liujingxing
Copy link
Owner

liujingxing commented Aug 15, 2022

补充下必现方式

1、定义一个顶级函数,返回泛型具体类型

//TypeToken是Gson库里的类
public inline fun <reified T> type() = object : TypeToken<T>() {}.type

2、在伴生对象中调用type方法

object TypeUtil {

    fun typeOf() {
        val type = type<List<String>>()
    }
}

3、在任意地方调用typeOf方法

注:环境 kotlin v1.7.10,其它版本未测试

最后打包时,开启混淆优化,打包后,安装测试即可

@liujingxing
Copy link
Owner

liujingxing commented Aug 15, 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

No branches or pull requests

2 participants