使用DexClassLoader启动活动

时间:2017-01-05 16:15:21

标签: java android reflection dexclassloader

我的要求是启动一个活动,该活动存在于资产文件夹内的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;
}

我哪里弄错了?任何帮助都非常有用。

1 个答案:

答案 0 :(得分:-1)

  

我的要求是启动一个活动,该活动存在于资产文件夹内的aar文件中,或者可能位于SD卡中。

一般情况下这并不实用。

  

我在哪里做错了?

首先,资产是开发计算机上的文件。它不是设备上的文件。 Location可用file:///android_asset/,但一般情况下不可用。

其次,您假设您的反射将适用于您要支持的所有设备。最好的情况是,当您控制硬件时,这种方法很有效。

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

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