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

插件启动服务失败,java.lang.ClassCastException: XXXAccessibilityService cannot be cast to com.tencent.shadow.core.runtime.ShadowService #1229

Closed
mintz1chen opened this issue Aug 21, 2023 · 7 comments

Comments

@mintz1chen
Copy link

插件的服务是继承自android的AccessibilityService
public class XXXAccessibilityService extends AccessibilityService
启动服务是用在maven文件夹里面的manager:

private void callPluginService(final Context context) {
        final String pluginZipPath = "/data/local/tmp/plugin-debug.zip";
        final String partKey = "sample-plugin";
        final String className = "XXXAccessibilityService";

        Intent pluginIntent = new Intent();
        pluginIntent.setClassName(context.getPackageName(), className);

        executorService.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    InstalledPlugin installedPlugin
                            = installPlugin(pluginZipPath, null, true);//这个调用是阻塞的

                    loadPlugin(installedPlugin.UUID, partKey);

                    Intent pluginIntent = new Intent();
                    pluginIntent.setClassName(context.getPackageName(), className);

                    boolean callSuccess = mPluginLoader.bindPluginService(pluginIntent, new PluginServiceConnection() {
                        @Override
                        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                            IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
                            try {
                                String s = iMyAidlInterface.basicTypes(1, 2, true, 4.0f, 5.0, "6");
                                Log.i("SamplePluginManager", "iMyAidlInterface.basicTypes : " + s);
                            } catch (RemoteException e) {
                                throw new RuntimeException(e);
                            }
                        }

                        @Override
                        public void onServiceDisconnected(ComponentName componentName) {
                            throw new RuntimeException("onServiceDisconnected");
                        }
                    }, Service.BIND_AUTO_CREATE);

                    if (!callSuccess) {
                        throw new RuntimeException("bind service失败 className==" + className);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

点击maven文件夹里面的host的启动插件服务,报这个crash了,是哪个步骤错了吗?我对比sample里面起插件的HostAddPluginViewService服务也是这个流程

@shifujun
Copy link
Collaborator

AccessibilityService我没去看代码,可能是SDK内置的吧?像IntentService一样是SDK内置的话,我们在transform时是无法修改它的。所以要将它的子类在transform时特殊处理。

如果是这种情况,你可以参考IntentService的transform代码处理。

@mintz1chen
Copy link
Author

我试一下在transform那里改改看行不行,这个是android sdk里面的服务,我试了调起另外一个extends Service的服务是可以调用的。另外前面我提的另一个问题 #1226 能帮忙看看吗?或者rtx找我:marrkzhong,我给你开个工程权限看看,谢谢

@mintz1chen
Copy link
Author

class AccessibilityServiceTransform : SimpleRenameTransform(
    mapOf("android.accessibilityservice.AccessibilityService" to "com.tencent.shadow.core.runtime.ShadowAccessibilityService")
)

override val mTransformList: List<SpecificTransform> = listOf(
        ApplicationTransform(),
        ActivityTransform(),
        ServiceTransform(),
        IntentServiceTransform(),
        AccessibilityServiceTransform(),
        InstrumentationTransform(),
        FragmentSupportTransform(),

ShadowAccessibilityService实现照搬系统的AccessibilityService,并去掉androidX的注解
重新打sdk之后宿主manager插件都重新构建,加载插件会报错:

java.lang.RuntimeException: java.lang.IllegalStateException: java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: java.lang.ClassCastException: Cannot cast androidx.core.app.CoreComponentFactory to com.tencent.shadow.core.runtime.ShadowAppComponentFactory
                                                                                                    	at com.tencent.shadow.sample.manager.SamplePluginManager$1.run(SamplePluginManager.java:110)
                                                                                                    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
                                                                                                    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
                                                                                                    	at java.lang.Thread.run(Thread.java:930)
                                                                                                    Caused by: java.lang.IllegalStateException: java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: java.lang.ClassCastException: Cannot cast androidx.core.app.CoreComponentFactory to com.tencent.shadow.core.runtime.ShadowAppComponentFactory
                                                                                                    	at android.os.Parcel.createExceptionOrNull(Parcel.java:2451)
                                                                                                    	at android.os.Parcel.createException(Parcel.java:2427)
                                                                                                    	at android.os.Parcel.readException(Parcel.java:2410)
                                                                                                    	at android.os.Parcel.readException(Parcel.java:2352)
                                                                                                    	at com.tencent.shadow.dynamic.manager.BinderPluginLoader.loadPlugin(BinderPluginLoader.java:50)
                                                                                                    	at com.tencent.shadow.sample.manager.FastPluginManager.loadPlugin(FastPluginManager.java:127)
                                                                                                    	at com.tencent.shadow.sample.manager.FastPluginManager.convertActivityIntent(FastPluginManager.java:110)
                                                                                                    	at com.tencent.shadow.sample.manager.FastPluginManager.startPluginActivity(FastPluginManager.java:102)
                                                                                                    	at com.tencent.shadow.sample.manager.SamplePluginManager$1.run(SamplePluginManager.java:108)
                                                                                                    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
                                                                                                    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
                                                                                                    	at java.lang.Thread.run(Thread.java:930) 

@mintz1chen
Copy link
Author

上面这个问题原来是因为更改了插件的runtime的gradle文件里面的compileSdkVersion和minSdkVersion导致的,看来runtime里面的配置要跟plugin一样的话还得改其他东西

@mintz1chen
Copy link
Author

mintz1chen commented Aug 21, 2023

我发现有个问题,这AccessibilityService源码里面用到一些系统接口,在sdk里面没有这些系统接口,会导致编译报错说找不到方法,这样子是只能把这个服务放在宿主里面了?如果插件也要能单独运行的话,插件是也要放一份?

@mintz1chen mintz1chen reopened this Aug 21, 2023
@shifujun
Copy link
Collaborator

尽量在shadow的runtime层复制一份AccessibilityService的实现,让插件继承的父类改为shadow中的实现。像IntentService那样。

如果复制实现遇到有些接口根本调用不了,简单复制不可行。就得考虑更复杂转调那些自己实现不了的接口。可能是转调给一个真正的AccessibilityService。具体要看是什么功能。

@mintz1chen
Copy link
Author

谢谢,最终考虑还是在宿主做个继承AccessibilityService的服务来直接让系统触发,然后再把它里面回调的接口内容同步出去给插件接收处理。这样子你觉得ok不?这个无障碍服务主要是用来做辅助点击的。下午我试过在sdk的runtime里面去实现复制的服务,但是无奈它源码用到的一些系统的类在sdk里面都无法调用,都搬过来的话工作量比较大而且也不知道跑起来有没有问题。

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