Android最简单的热更新方式 - Robust 使用填坑

原文链接:https://blog.csdn.net/qq_35849743/article/details/102562335

看了Tinker和Robust,普通的修复的话,Robust真的是最简单,最好的。

但是看着别人写的,经过了无数次的失败,终于成功了。

java.lang.ClassNotFoundException:

Didn't find class "xxx.xxx.xxx.PatchesInfoImpl" on path:

DexPathList[[],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

这个问题整了我几天,坑就是从这里开始填的:

1.包名

Robust这块有bug,包名不是com开头的新增类打不进去。

2.PatchManipulateImp 

public class PatchManipulateImp extends PatchManipulate {

    @Override

    protected List fetchPatchList(Context context) {

        Patch patch = new Patch();

        patch.setName("patch test");

        patch.setLocalPath(FileDirectoryUtil.getPatchFile());

        patch.setPatchesInfoImplClassFullName("com.xxx.xxx.PatchesInfoImpl");

        List patches = new ArrayList<>();

        patches.add(patch);

        return patches;

    }


    ......

}

① 这里的setLocalPath只需要写到文件名,后缀里面在get的时候已经自动加在了后面(虽然即使你加了其实也没影响,不过名称就多了一个.jar,对于我这种强迫症的人看着很不舒服)。

② setPatchesInfoImplClassFullName里面写的包名com.xxx.xxx是可以随意写的(由于上面的问题,所以还是以com开头为好),不需要和项目的包名一致,但是必须得和robust.xml里的patchPackname一致,还有,后面一定是 PatchesInfoImpl 结尾!!! 

③ 在执行过程中,是先执行 ensurePatchExist方法,这里先验证文件是否存在,如下:

    @Override

    protected boolean ensurePatchExist(Patch patch) {

        boolean exist = new File(patch.getLocalPath()).exists();

        return exist;

    }

 然后再执行verifyPatch

@Override

protected boolean verifyPatch(Context context, Patch patch) {

    //放到app的私有目录

    patch.setTempPath(FileDirectoryUtil.getPatchFile());

    //in the sample we just copy the file

    try {

        copy(patch.getLocalPath(), patch.getTempPath());

    }catch (Exception e){

        e.printStackTrace();

        throw new RuntimeException("copy source patch to local patch error, no patch       

            execute in path "+patch.getTempPath());

    }

    return true;

}

这里是在干嘛?在复制patch.jar为patch_temp.jar。因为最后使用patch_temp.jar执行的!!!不是用patch.jar !!!

android热修复之Robust使用(真正的入门)

这篇文章直接把两个方法直接写死为true,把LocalPath和TempPath都设置了。然后最后给手机推的是patch_temp.jar(那你设置的LocalPath有什么意义?)

我得到的是patch.jar,由于我是直接复制到手机里,所以理由当然的就把patch.jar直接复制过去了..... 

所以结果当然是,屡试不成功。

3.权限

 虽然我没有踩这个坑,但是还是提醒一下,因为有人遇到过,一定要动态的申请权限后再执行。


基于这个错误基本上就是这些。

下面再提醒一些:

1.  一定要加

multiDexEnabled true

implementation 'com.android.support:multidex:1.0.3'

  2. 一定要打正式包

    signingConfigs {

        release  {

            storeFile file(jks地址)

            storePassword "xxx"

            keyAlias "xxx"

            keyPassword "xxx"

        }

    }

    dexOptions {

        // 支持大工程模式

        jumboMode = true

    }

    buildTypes {

        debug {

            debuggable true

            minifyEnabled false

            signingConfig signingConfigs.release

        }

        release {

            signingConfig signingConfigs.release

            minifyEnabled true

            // 移除无用的resource文件

            shrinkResources true

            // Zipalign优化

            zipAlignEnabled true

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }

    }

3.打APK和打补丁一定要注意开关这两个

// 生成补丁的时候开启,必须在其它之前

apply plugin: 'auto-patch-plugin'

// 生成APK的时候开启

apply plugin: 'robust'

4.每一个打完包后mapping.txt和methodsMap.robust一定要保存好,建议保存到以版本号命名的文件夹。

5.执行时一定要start()

protected void runRobust(){

    new PatchExecutor(getApplicationContext(),new PatchManipulateImp(),new               

        RobustCallbackSample()).start();

}


可能上面的问题很多人都没遇到过,特别是最后一个,因为复制粘贴哪里有这种问题。代码最好是自己敲一遍,虽然敲的过程中会遇到很多很多坑,但是只有经历了这些坑,才会知其所以然。

有官方Demo的一样要用官方的Demo测试。虽然官方其实很多地方说的很模糊,然而恰恰正是那些模糊的地方,然使用者踩了很多坑。

比如,上面的 patch_temp.jar.....

————————————————

版权声明:本文为CSDN博主「Theoneee」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_35849743/article/details/102562335

你可能感兴趣的:(Android最简单的热更新方式 - Robust 使用填坑)