Learning Summary of Android Plug-ins (Continuous Updates)

Posted by aissa on Sun, 07 Jul 2019 23:59:08 +0200

Android Plug-in Learning

Android Plug-in Benefits

  1. Implementing Hot Update Operation
  2. Business Hot Plug-in
  3. Increasing Start-up Speed

Basic Realization

Using a host APK, i.e. shell program, dynamic loading function and plug-in package call are implemented to realize dynamic update function.

Opening chapter

For this block of content learning, I have just come into contact, hoping to learn from me step by step, from shallow to deep, better understand the dynamic loading process, the ultimate goal is to achieve a relatively complete plug-in framework.
å

Basic knowledge

DexClassLoader can be used to load Dex, jar, Apk

Introduction

1. DL(Dynamic-load-apk)

The framework implements the plug-in framework in a proxy manner
As the author has written relevant articles, I will supplement them by citing the content of his articles here as the basis.
Research on Dynamic Loading Mechanism of Android apk
First, according to the above article for the first time in practice, understanding and analysis in the process of practice.
In the above article, it mainly talks about how to realize the agent and how to transform the Activity class.

Principle of agency:

1. Get the plug-in package location first, and then call the PackageManger.getPackageArchiveInfo() function to get the PackageInfo in it.

    protected void launchTargetActivity() {
        PackageInfo packageInfo = getPackageManager().getPackageArchiveInfo(
                mDexPath, PackageManager.GET_ACTIVITIES);
        if ((packageInfo.activities != null)
                && (packageInfo.activities.length > 0)) {
            String activityName = packageInfo.activities[0].name;  // The main thing is to get the open Active name
            mClass = activityName;
            Log.d("TAG", mClass);
            launchTargetActivity(mClass);  // Input as a parameter
        }
    }

2. There are mainly three parts.
a. Obtain the proxy class through reflection calls

Class<?> localClass = dexClassLoader.loadClass(className);
Constructor<?> localConstructor = localClass.getConstructor(new Class[] {});
Object instance = localConstructor.newInstance(new Object[] {});

b. Enter the current Activity and save the proxy class

Method setProxy = localClass.getMethod("setProxy",
new Class[] { Activity.class });
setProxy.setAccessible(true);
setProxy.invoke(instance, new Object[] { this });  // Enter the current Activity

In the proxy class:

public void setProxy(Activity proxyActivity) {
        mProxyActivity = proxyActivity;
}

c. Use the Active Object of the host APP to invoke the related function, namely the onCreate function.

Method onCreate = localClass.getDeclaredMethod("onCreate", new Class[] { Bundle.class });
onCreate.setAccessible(true);
Bundle bundle = new Bundle();
bundle.putInt(FROM, FROM_EXTERNAL);
onCreate.invoke(instance, new Object[] { bundle });

3. Agent class BaseActivity:

@Override
    public void setContentView(View view) {
        if (mProxyActivity == this) {
            super.setContentView(view);
        } else {
            mProxyActivity.setContentView(view);  // Implementing related operations mainly with the imported Activity class
        }
    }

Be careful:
In order to access plug-in packages under the relevant path, the relevant permissions are required

<! - Create and delete file permissions in SDCD --> ___________.
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <! - Write data permissions to SDCard - >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Ending

A Summary of Current Learning

2007-04-19 16:47:10 Updated Basic DL Analysis, Next Step Learn Resource File Access

Topics: Android hot update shell