实现Android插件化的核心技术简介(三):Android dynamic register activities

实现Android插件化的核心技术(三):Android dynamic register activities

Android activities are monitored by Instrumentation.

Android activities受Instrumentation监控。

Each activity was started by Activity's startActivityForResult. (1)

ActivitystartActivityForResult方法启动,通过instrumentation的execStartActivity方法激活生命周期。

public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {
    if (mParent == null) {
        Instrumentation.ActivityResult ar =
            mInstrumentation.execStartActivity( // Override entry 1
                this, mMainThread.getApplicationThread(), mToken, this,
                intent, requestCode, options);
        ...
    }
}

And instantiated by ActivityThread's performLaunchActivity. (2)

ActivityThreadperformLaunchActivity方法中通过instrumentation的newActivity方法实例化。

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    ...

    Activity activity = null;
    try {
        java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
        activity = mInstrumentation.newActivity( // Override entry 2
                cl, component.getClassName(), r.intent);
        StrictMode.incrementExpectedActivityCount(activity.getClass());
        r.intent.setExtrasClassLoader(cl);
        r.intent.prepareToEnterProcess();
        if (r.state != null) {
            r.state.setClassLoader(cl);
        }
    } catch (Exception e) {
        if (!mInstrumentation.onException(activity, e)) {
            throw new RuntimeException(
                "Unable to instantiate activity " + component
                + ": " + e.toString(), e);
        }
    }
    ...
}

To dynamic register activities, we register a stub activity in host's AndroidManifest.xml to cheat (1) to manager the activity life circle for us. And then cheat (2) to create the plugin activity instance.

动态注册Activity,首先在宿主manifest中注册一个命名特殊的占坑activity来欺骗(1)以获得生命周期,再欺骗(2)来获得插件activity实例。

So all the things to do is wrapper a instrumentation and replace the host one.

要做的就是封装一个instrumentation,替换掉宿主的。

Fake code:

<!-- Stub Activities -->
<activity android:name=".A.0" android:launchMode="standard"/>
ActivityThread thread = currentActivityThread();
Instrumentation base = thread.@mInstrumentation;
Instrumentation wrapper = new InstrumentationWrapper(base);
thread.@mInstrumentation = wrapper;

class InstrumentationWrapper extends Instrumentation {
    public ActivityResult execStartActivity(..., Intent intent, ...) {
        fakeToStub(intent);
        base.execStartActivity(args);
    }

    @Override
    public Activity newActivity(ClassLoader cl, String className, Intent intent) {
        className = restoreToReal(intent, className);
        return base.newActivity(cl, className, intent);
    }
} 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值