我可以使用 ClassLoader 加载片段,同时使用 APK 的权限吗?

Can I use ClassLoader to load a fragment but also with the permissions from the APK?

提问人:Javier 提问时间:11/4/2023 更新时间:11/4/2023 访问量:14

问:

我有一个应用程序,我想从第二个应用程序(我将开发)动态加载一个片段,背后的想法是,如果应用程序不是“呼叫应用程序”,Google Play 不允许某些权限,例如默认呼叫应用程序,所以我的想法是创建一个新的 APK,然后用户必须从我的网站或其他地方下载 apk, 主应用程序不会有下载 apk 的逻辑(因为这可能会被标记为恶意软件),然后这个想法是使用 Classloader(我设法做到了)加载 Fragment,或使用像这样的库: https://github.com/Catherine22/ClassLoader 问题是假设我在第一个 APK 中没有互联网权限, 我在第二个 APK 中拥有互联网权限,如果我在主应用程序中加载“第二个 apk”片段,我将收到一个错误,因为在主应用程序中我没有互联网权限。

虽然我知道这看起来是一个很好的安全措施,但我只想知道是否有可能为我的用户提供更多功能和选项,Tasker 会做一些类似于使用旧版本伴侣控制 WIFI 的事情,它可能和我想要的不一样,但这给了我一个想法,如果他们想要的话,可以给我的用户提供更多功能。 这可能吗? 我使用了这个从SO使用应用程序内部的外部应用程序片段/活动的截取代码

var requiredClass: Class<*>? = null
            val apkPath = packageManager.getApplicationInfo("com.test.composetest", 0).sourceDir
            val dexTemp: File = getDir("temp_folder", 0)
            val fullName = "com.test.composetest.TestFragment"

            var isLoaded = true;

            // Check if class loaded
            try {
                requiredClass = Class.forName(fullName);
            } catch (e: ClassNotFoundException) {
                isLoaded = false;
            }

            if (!isLoaded) {
                val classLoader = DexClassLoader(
                    apkPath, dexTemp.absolutePath, null,
                    applicationContext.classLoader
                )

                requiredClass = classLoader.loadClass(fullName);
            }

            if (requiredClass != null) {
                // Try to cast to required interface to ensure that it's can be cast
                val holder = FragmentHolder::class.cast(requiredClass.newInstance())

                if (null != holder) {
                    val fragment: Fragment = holder.getFragment();

                    if (null != fragment) {
                        val trans: FragmentTransaction =
                            supportFragmentManager.beginTransaction()

                        trans.add(R.id.fragmentPlace, fragment, "MyFragment").commit();
                    }
                }
            }
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace();
        } catch (e: ClassNotFoundException) {
            e.printStackTrace();
        } catch (e: InstantiationException) {
            e.printStackTrace();
        } catch (e: IllegalAccessException) {
            e.printStackTrace();
        }
android-fragments 类加载器

评论

0赞 CommonsWare 11/4/2023
这样使用可能会违反 Play 商店 FWIW 的开发者分发条款。“问题是假设我在第一个APK中没有互联网权限,而我在第二个APK中拥有互联网权限,如果我在主应用程序中加载”第二个apk“片段,我将收到一个错误,因为在主应用程序中我没有互联网权限。与其做这些事情,不如让第二个APK实际完成工作。Android 有多种形式的 IPC:启动 Activity、启动或绑定到服务等。ClassLoaderClassLoader
0赞 Javier 11/6/2023
非常感谢@CommonsWare这是有道理的,感谢您的帮助

答: 暂无答案