Android加载Split资源新方案

2021-05-19  本文已影响0人  DonaldDu

在学习qigsaw的过程中发现其加载和检查资源流程相对麻烦,从而造成效率低下。

加载新资源后不需要重复加载,所以加载资源调用次数非常少,基本都是检查逻辑。每次获取资源前都要检查Split是否加载,所以调用非常频繁,执行速度太慢则会影响流畅度。

启动一个DEMO项目(两个页面,SplashActivity:3View,MainActivity:9View),检查方法调用了338次。

qigsaw当前实现做一次检查大概250us(1ms=1000us),如果使用缓存基本可以达到25us(测试设备API30模拟器)。

新方案

为了提高运行速度,在以下方面做了优化:

新发现

以下是API28+构建新AssetManager主要逻辑

    public AssetManager build() {
        // Retrieving the system ApkAssets forces their creation as well.
        final ApkAssets[] systemApkAssets = getSystem().getApkAssets();
        ...
        final ApkAssets[] apkAssets = new ApkAssets[totalApkAssetCount];

        System.arraycopy(systemApkAssets, 0, apkAssets, 0, systemApkAssets.length);

        // Append user ApkAssets after system ApkAssets.
        for (int i = 0, n = mUserApkAssets.size(); i < n; i++) {
            apkAssets[i + systemApkAssets.length] = mUserApkAssets.get(i);
        }
        ...

        // Calling this constructor prevents creation of system ApkAssets, which we took care
        // of in this Builder.
        final AssetManager assetManager = new AssetManager(false /*sentinel*/);
        assetManager.mApkAssets = apkAssets;
        ...
    }

用法

public interface IResourcesLoader {
    void initFastCompare(@NonNull AssetManager asset, @NonNull Collection<String> resPaths);

    /**
     * with pre inited resPaths
     */
    void loadResources(@NonNull AssetManager asset);
}
public class ResourcesLoader {
    @NonNull
    public static final IResourcesLoader instance;

    static {
        if (Build.VERSION.SDK_INT >= 28) {
            instance = new ResourcesLoader28();
        } else if (Build.VERSION.SDK_INT >= 21) {
            instance = new ResourcesLoader21();
        } else {
            throw new IllegalStateException("unsupported api");
        }
    }
}

提供了工具类,通过ResourcesLoader.instance直接调用。通过initFastCompare传入需要加载的apk资源,通过loadResources来检查并自动加载资源。

以下是自定义实现Qigsaw的SplitResourcesLoader来替换默认资源加载器的代码。

@AutoService(SplitResourcesLoader.class)
public class SplitResourcesLoaderCompat implements SplitResourcesLoader {

    @Override
    public void loadResources(@NonNull Context context, @NonNull Resources resources) {
        ResourcesLoader.instance.loadResources(resources.getAssets());
    }

    @Override
    public void loadResources(@NonNull Context context, @NonNull Resources preResources, @NonNull String splitApkPath) {
        ResourcesLoader.instance.initFastCompare(preResources.getAssets(), Collections.singleton(splitApkPath));
    }
}

相关链接

github link

ResourcesLoader jitpack版本

上一篇 下一篇

猜你喜欢

热点阅读