Shadow icon indicating copy to clipboard operation
Shadow copied to clipboard

插件使用宿主依赖的sdk

Open chenlin139 opened this issue 3 years ago • 7 comments

宿主依赖了Glide图片加载的sdk,配置了sdk的白名单,在插件中直接使用glide就会找不到

Process: com.example.myapplication, PID: 14666 java.lang.NoClassDefFoundError: Failed resolution of: Lcom/bumptech/glide/Glide; at com.example.testplugin.MainActivity$1.onClick(MainActivity.java:42) at android.view.View.performClick(View.java:7584) at android.view.View.performClickInternal(View.java:7539) at android.view.View.access$3900(View.java:840) at android.view.View$PerformClick.run(View.java:28747) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:269) at android.app.ActivityThread.main(ActivityThread.java:8296) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:992) Caused by: java.lang.ClassNotFoundException: Didn't find class "com.bumptech.glide.Glide" on path: DexPathList[[zip file "/data/user/0/com.example.myapplication/files/testplugin-plugin-debug.apk"],nativeLibraryDirectories=[/data/user/0/com.example.myapplication/files/testplugin-plugin-debug.apk_lib, /system/lib64, /system_ext/lib64, /vendor/lib64, /odm/lib64]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207) at com.tencent.shadow.core.loader.classloaders.PluginClassLoader.loadClass(Unknown Source:79) at java.lang.ClassLoader.loadClass(ClassLoader.java:312) at com.example.testplugin.MainActivity$1.onClick(MainActivity.java:42)  at android.view.View.performClick(View.java:7584)  at android.view.View.performClickInternal(View.java:7539)  at android.view.View.access$3900(View.java:840)  at android.view.View$PerformClick.run(View.java:28747)  at android.os.Handler.handleCallback(Handler.java:938)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:269)  at android.app.ActivityThread.main(ActivityThread.java:8296)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:992) 

chenlin139 avatar Jun 08 '22 07:06 chenlin139

https://github.com/Tencent/Shadow/blob/e7988d6d26d4804d598fc64e5e9e9c852edbb2d2/CONTRIBUTING.md#issue

shifujun avatar Jun 08 '22 07:06 shifujun

麻烦看下 https://github.com/lidongdong139/Shadow/tree/shadow_dev

chenlin139 avatar Jun 08 '22 15:06 chenlin139

有代码就很容易看到问题了嘛。你这里写的白名单定义是错的。 https://github.com/lidongdong139/Shadow/commit/21b3b127302f16f088f1ae24826c5db2eb9096e0#diff-cbe91803ec49d83dc968436974c6b473885874323e7517ee3623a7a7f86d0ff9R70

这是.**用法的测试用例: https://github.com/Tencent/Shadow/blob/e7988d6d26d4804d598fc64e5e9e9c852edbb2d2/projects/sdk/core/loader/src/test/kotlin/com/tencent/shadow/core/loader/classloaders/PluginClassLoaderTest.kt#L138-L150

估计你需要参考的是: https://github.com/Tencent/Shadow/blob/e7988d6d26d4804d598fc64e5e9e9c852edbb2d2/projects/sdk/core/loader/src/test/kotlin/com/tencent/shadow/core/loader/classloaders/PluginClassLoaderTest.kt#L222-L232

类似这样的问题建议自己Debug一下SDK代码,不能过多依赖猜测。Shadow不是一个商业产品,就是拿出来代码和大家交流的。

shifujun avatar Jun 09 '22 03:06 shifujun

类能找到了,但是插件中不能使用activity对象吗? Glide.with(this).load("");//报错 // Glide.with(getBaseContext()).load(""); 只能这种方式 https://github.com/Tencent/Shadow/commit/5c4bd6722d82fcf3c45f45e84e195e8a67db9548

chenlin139 avatar Jun 09 '22 15:06 chenlin139

你可以将插件Activity理解为宿主Activity持有的一个普通对象(就是ShadowActivity类型)。所以调用宿主里的Glide.with(<Activity>)方法,肯定不能随便给它一个普通对象吧?

但如果是插件中的原本存在的Glide.with(<Activity>)方法,在Transform之后也会被重构为Glide.with(<ShadowActivity>)方法,这样插件中原本的代码就可以不用额外修改了。

使用Shadow就是为了让插件的这部分代码同系统中受限制API隔离开,而Activity类型就是一种受限制的API,所以插件不能再和宿主通过这些类型交互了。

shifujun avatar Jun 10 '22 00:06 shifujun

这样的话宿主里引用到activity相关的方法都不能用了,原有业务都得改掉。要用FramentActivity的getSupportFragmentManager显示弹窗都没法实现了。不能去绕过实现吗?

chenlin139 avatar Jun 10 '22 08:06 chenlin139

所以你不应该让插件访问宿主中受限制的API。

shifujun avatar Jun 10 '22 09:06 shifujun