gpt4 book ai didi

java - 使用 DexClassLoader 启动 Activity

转载 作者:行者123 更新时间:2023-12-01 06:07:23 31 4
gpt4 key购买 nike

我的要求是启动一个 Activity,该 Activity 存在于 Assets 文件夹内的 aar 文件中或可能位于 SDCard 中。但我遇到了以下异常:

01-05 21:06:18.717 3150-3150/com.example.nayakc2.dynamicloading W/System.err: java.lang.ClassNotFoundException: Didn't find class "com.example.nayakc2.aarsample.LibActivity" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib, /vendor/lib]]
01-05 21:06:18.717 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
01-05 21:06:18.717 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
01-05 21:06:18.717 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
01-05 21:06:18.717 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at com.example.nayakc2.dynamicloading.MainActivity.startActivityFromAar(MainActivity.java:70)
01-05 21:06:18.719 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at com.example.nayakc2.dynamicloading.MainActivity.onOptionsItemSelected(MainActivity.java:56)
01-05 21:06:18.720 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.app.Activity.onMenuItemSelected(Activity.java:3204)
01-05 21:06:18.720 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:408)
01-05 21:06:18.720 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:198)
01-05 21:06:18.721 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113)
01-05 21:06:18.721 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113)
01-05 21:06:18.721 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
01-05 21:06:18.721 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:206)
01-05 21:06:18.721 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:776)
01-05 21:06:18.721 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
01-05 21:06:18.722 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
01-05 21:06:18.722 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
01-05 21:06:18.722 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.support.v7.view.menu.MenuPopup.onItemClick(MenuPopup.java:127)
01-05 21:06:18.722 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.widget.AdapterView.performItemClick(AdapterView.java:310)
01-05 21:06:18.722 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.widget.AbsListView.performItemClick(AbsListView.java:1155)
01-05 21:06:18.722 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.widget.AbsListView$PerformClick.run(AbsListView.java:3120)
01-05 21:06:18.722 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.widget.AbsListView.onTouchUp(AbsListView.java:4047)
01-05 21:06:18.722 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.widget.AbsListView.onTouchEvent(AbsListView.java:3806)
01-05 21:06:18.722 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.support.v7.widget.ListViewCompat.onTouchEvent(ListViewCompat.java:124)
01-05 21:06:18.722 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.View.dispatchTouchEvent(View.java:9943)
01-05 21:06:18.722 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2663)
01-05 21:06:18.722 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2344)
01-05 21:06:18.723 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
01-05 21:06:18.723 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
01-05 21:06:18.723 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
01-05 21:06:18.723 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
01-05 21:06:18.723 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.widget.PopupWindow$PopupDecorView.dispatchTouchEvent(PopupWindow.java:2266)
01-05 21:06:18.724 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.View.dispatchPointerEvent(View.java:10163)
01-05 21:06:18.724 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4434)
01-05 21:06:18.726 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4302)
01-05 21:06:18.726 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
01-05 21:06:18.726 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
01-05 21:06:18.726 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
01-05 21:06:18.727 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3995)
01-05 21:06:18.727 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
01-05 21:06:18.727 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4052)
01-05 21:06:18.727 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
01-05 21:06:18.727 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
01-05 21:06:18.727 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
01-05 21:06:18.727 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
01-05 21:06:18.727 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
01-05 21:06:18.728 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6210)
01-05 21:06:18.728 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6184)
01-05 21:06:18.728 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6145)
01-05 21:06:18.728 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6313)
01-05 21:06:18.728 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
01-05 21:06:18.728 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.os.MessageQueue.nativePollOnce(Native Method)
01-05 21:06:18.728 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.os.MessageQueue.next(MessageQueue.java:323)
01-05 21:06:18.728 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.os.Looper.loop(Looper.java:136)
01-05 21:06:18.728 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6077)
01-05 21:06:18.728 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-05 21:06:18.728 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
01-05 21:06:18.728 3150-3150/com.example.nayakc2.dynamicloading W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

这是我实现此目的的代码:

private void startActivityFromAar() {
File file = new File("file:///android_asset/sample.aar");
DexClassLoader dexLoader = new DexClassLoader(file.getAbsolutePath(),
getCacheDir().getAbsolutePath(), null, getClassLoader());
setAPKClassLoader(dexLoader);

try {
Class<?> activityClass = dexLoader.loadClass("com.example.nayakc2.aarsample.LibActivity");
Intent intent = new Intent(this, activityClass);
startActivity(intent);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private void setAPKClassLoader(ClassLoader classLoader)
{
try {
Field mMainThread = getField(Activity.class, "mMainThread");
Object mainThread = mMainThread.get(this);
Class threadClass = mainThread.getClass();
Field mPackages = getField(threadClass, "mPackages");

Map<String,?> map = (Map<String,?>) mPackages.get(mainThread);
WeakReference<?> ref = (WeakReference<?>) map.get(getPackageName());
Object apk = ref.get();
Class apkClass = apk.getClass();
Field mClassLoader = getField(apkClass, "mClassLoader");

mClassLoader.set(apk, classLoader);
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private Field getField(Class<?> cls, String name)
{
for (Field field: cls.getDeclaredFields())
{
if (!field.isAccessible()) {
field.setAccessible(true);
}
if (field.getName().equals(name)) {
return field;
}
}
return null;
}

我哪里做错了?任何帮助都会有很大帮助。

最佳答案

My requirement is to start an Activity which is present in an aar file inside your asset folder or may be in SDCard.

这一般来说是不切实际的。

Where do I do mistake?

首先, Assets 是开发计算机上的文件。它不是设备上的文件。 file:///android_asset/ 可在 WebView 中使用,但不适用于一般情况。

其次,您假设您的反射适用于您要支持的所有设备。充其量,当您控制硬件时,这种方法才有效。

第三,该 Activity 不会出现在 list 中,除非您在构建主应用程序时以某种方式自行安排。

第四,该 Activity 将无权访问 AAR 中的资源,例如其编译所针对的资源。它将尝试从您的主应用加载这些资源,并且这些资源很可能具有不同的 ID 值。

关于java - 使用 DexClassLoader 启动 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41489708/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com