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

MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26) #2041

Closed
swanfly opened this issue Nov 21, 2023 · 16 comments
Labels
bug Something isn't working

Comments

@swanfly
Copy link

swanfly commented Nov 21, 2023

升级安卓插件版本到8.1.4后,同时升级至fastjson2:2.0.42后,出现如下错误:
MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
所以,将minSdkVersion由16改为26
可以正常运行。
打包后发现,APK包不但增加了8兆,而且还崩溃,日志如下:
java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at com.alibaba.fastjson2.TypeReference.(SourceFile:1)
请问最新的fastjson2,能不能适配minSdkVersion小于26情况。

相关版本信息如下:
id("com.android.application") version "8.1.4" apply false
id("org.jetbrains.kotlin.android") version "1.8.10" apply false
api 'com.alibaba.fastjson2:fastjson2:2.0.42'
distributionUrl=https://services.gradle.org/distributions/gradle-8.0-bin.zip
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

@swanfly swanfly added the bug Something isn't working label Nov 21, 2023
@wenshao
Copy link
Member

wenshao commented Nov 21, 2023

用这个版本试试看 2.0.42.android4

@swanfly
Copy link
Author

swanfly commented Nov 21, 2023

用2.0.42.android4版本,不再提示only supported starting with Android O (--min-api 26)这个错误了
但是,打包成apk后,还是崩溃,日志如下
ava.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at com.alibaba.fastjson2.TypeReference.(SourceFile:1)

@swanfly
Copy link
Author

swanfly commented Nov 21, 2023

我找到了相似的问题,如下:
https://juejin.cn/post/7230727739078148133
是因为Android Gradle 8.0以后 R8是默认full modle,所以会报这个错误。但是混淆规则该怎么写?

@wenshao
Copy link
Member

wenshao commented Nov 26, 2023

我不熟悉android的混淆规则,希望你帮忙探索并且总结分享经验

@swanfly
Copy link
Author

swanfly commented Nov 26, 2023

我正在尝试解决混淆的问题,我将fastjson2放到我的项目里面,发现找不到sun.misc.Unsafe类。
image
通过搜索得知,自jdk 11版本之后,不允许调用直接使用Unsafe。但是可以通过反射使用该类。
参考:https://www.imooc.com/wenda/detail/693459
image
所以,需要你先解决Unsafe类调用不到的问题后,我这才能运行起来,才能尝试解决混淆的问题。

@swanfly
Copy link
Author

swanfly commented Nov 26, 2023

image https://stackoverflow.com/questions/34702403/in-android-how-to-invoke-sun-misc-unsafe-methods-using-java-reflection

我明天不忙的时候再试一下,不用android自带的了,用java sdk试试,看看能不能运行起来。

@swanfly
Copy link
Author

swanfly commented Nov 27, 2023

用自己安装的jdk17,也不行,没招了
image

@swanfly
Copy link
Author

swanfly commented Nov 27, 2023

用VarHandle能不能替代Unsafe?
image
https://juejin.cn/post/6844904174669430797

@wenshao
Copy link
Member

wenshao commented Nov 27, 2023

VarHandler在低版本不兼容,底层也是使用Unsafe实现的。

@swanfly
Copy link
Author

swanfly commented Nov 27, 2023

建议分开处理
if (高版本) {
// 用VarHandler
} else {
// 用Unsafe
}

@wenshao
Copy link
Member

wenshao commented Nov 27, 2023

JDKUtils这个类不做混淆可以么?

@swanfly
Copy link
Author

swanfly commented Nov 27, 2023

我试一下。
这句异常,是在哪个方法中抛出的?你能不能找到?
image

@swanfly
Copy link
Author

swanfly commented Nov 27, 2023

JDKUtils这个类不做混淆可以么?
不光JDKUtils,fastjson2下的所有文件都没有混淆,还是报错。
java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at com.alibaba.fastjson2.TypeReference.(SourceFile:1)

可以确定,是以下代码报的错
JSONArray dataArray = jsonObject.getJSONArray("data");
TypeReference<List> type = new TypeReference<List>() {};
dataArray.to(type.getType());

@swanfly
Copy link
Author

swanfly commented Dec 16, 2023

[fastjson2 2.0.43]支持的minSdkVersion是26。minSdkVersion从16升级到26,apk包增加了8M多,有人和我遇到一样的情况吗?为什么会增加这么多,其中的原理又是什么?求知情者解答一下,不胜感激!

@hellojiawa
Copy link

[fastjson2 2.0.43]支持的minSdkVersion是26。minSdkVersion从16升级到26,apk包增加了8M多,有人和我遇到一样的情况吗?为什么会增加这么多,其中的原理又是什么?求知情者解答一下,不胜感激!

android在minSDK低于23时,会默认压缩so库,当minSDK>=23时,将不再对so进行压缩,可能是这个原因。

@swanfly
Copy link
Author

swanfly commented Apr 7, 2024

[fastjson2 2.0.43]支持的minSdkVersion是26。minSdkVersion从16升级到26,apk包增加了8M多,有人和我遇到一样的情况吗?为什么会增加这么多,其中的原理又是什么?求知情者解答一下,不胜感激!

android在minSDK低于23时,会默认压缩so库,当minSDK>=23时,将不再对so进行压缩,可能是这个原因。

谢谢,明白了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants