codepush热更新包减小体积-图片资源优化

场景

codepush更新包需要上传bundle+assets,当需要上传资源包体积比较大的情况下,会消耗大量用户流量且有下载失败风险,如出现紧急情况热更新下发率低下会造成极大的影响;那么如何减少更新包体积呢?

改造方案

  • 如使用拆包方案,大部分情况下只上传业务bundle,体积大概在50k以下,拆包方案参考RN拆包解决方案(一) bundle拆分
  • assets资源优化,出现大量素材资源的情况下需要优化处理,本次着重讲解图片资源加载优化

codepush增量更新图片资源

codepush已经对图片资源进行增量优化,我们来看下它的实现流程:

  • 示例1:当前版本1.0.0(1.png、2.png)-应用程序沙盒,热更新后包1.1.0(1.png、2.png)-文档沙盒,codepush需全量下载1.1.0包中的图片
  • 示例2:当前版本热更新后为1.1.0(1.png、2.png)-文档沙盒,热更新后包1.2.0(1.png、2.png、3.png)-文档沙盒,如果前后版本(1.png、2.png)md5一致,codepush只会增量下载3.png,将1.1.0中的(1.png、2.png)图片拷贝到1.2.0所在文档沙盒目录中

由此可见,首次热更新仍然需要全量下载消耗大量用户流量,还有更好的方案吗?

assets加载优化

我们可以修改RN图片加载流程,通过文档沙盒目录和本地应用程序目录结合,在更新后,判断当前bundle所在文档沙盒路径下是否存在资源,如果存在直接加载;如果没有,就从本地应用程序沙盒路径中加载代替,如果能这样处理,在没有变更图片资源的情况下,codepush只需要上传bundle文件,资源图片不需要一块打包;若要想修改RN图片加载流程,首先需要了解require图片原理

require引入图片原理

require方式返回的是一个整型, 对应一个define函数, 在bundle中体现为

//引用的地方  require方式
_react2.default.createElement(_reactNative.Image, { source: require(305                                      ), __source: { // 305 = ./Images/diary_mood_icon_alcohol_32.png
            fileName: _jsxFileName,
            lineNumber: 30
          }
        }),
 // uri 方式
_react2.default.createElement(_reactNative.Image, { source: { uir: 'https://www.baidu.com/img/bd_logo1.png', width: 100, height: 100 }, __source: {
            fileName: _jsxFileName,
            lineNumber: 31
          }
        })
//define地方
__d(/* RN472/Images/diary_mood_icon_alcohol_32.png */function(global, require, module, exports) {module.exports=require(161                                         ).registerAsset({"__packager_asset":true,"httpServerLocation":"/assets/Images","width":16,"height":16,"scales":[2,3],"hash":"7824b2f2a263b0bb181ff482a88fb813","name":"diary_mood_icon_alcohol_32","type":"png"}); // 161 = react-native/Libraries/Image/AssetRegistry
}, 305, null, "RN472/Images/diary_mood_icon_alcohol_32.png");

我们看到打包的时候,require图片会转换成如下格式的对象保存:

{
    "__packager_asset":true,  //是否是asset目录下的资源
    "httpServerLocation":"/assets/Images", //server目录地址
    "width":16, 
    "height":16,
    "scales":[2,3], //图片scales   
    "hash":"7824b2f2a263b0bb181ff482a88fb813", //文件hash值
    "name":"diary_mood_icon_alcohol_32", //文件名
    "type":"png" //文件类型
}

我们看到引用的地方require(305)其实是执行了require(161)的registerAsset的方法。查看161的define

__d(/* AssetRegistry */function(global, require, module, exports) {
'use strict';

var assets = [];

function registerAsset(asset) {
  return assets.push(asset);
}

function getAssetByID(assetId) {
  return assets[assetId - 1];
}

module.exports = { registerAsset: registerAsset, getAssetByID: getAssetByID };
}, 161, null, "AssetRegistry");

161对应的就是AssetRegistry, AssetRegistry.registerAsset把图片信息保存在成员数组assets中
查看Image.ios.js的render函数

  render: function() {
     const source = resolveAssetSource(this.props.source) || { uri: undefined, width: undefined, height: undefined };
    ...
    return (
      <RCTImageView
        {...this.props}
        style={style}
        resizeMode={resizeMode}
        tintColor={tintColor}
        source={sources}
      />
    );

通过resolveAssetSource函数

function resolveAssetSource(source: any): ?ResolvedAssetSource {
  if (typeof source === 'object') {
    return source;
  }

  var asset = AssetRegistry.getAssetByID(source);
  if (!asset) {
    return null;
  }

  const resolver = new AssetSourceResolver(getDevServerURL(), getBundleSourcePath(), asset);
  if (_customSourceTransformer) {
    return _customSourceTransformer(resolver);
  }
  
  return resolver.defaultAsset();

调用AssetRegistry.getAssetByID方法取出对应的信息,传递到原生。

//传递到原生的source信息格式
{
    "__packager_asset" = 1;
    height = 16;
    scale = 2;
    uri = "//Users/xxx/Library/Developer/CoreSimulator/Devices/2A0C4BE4-807B-4000-83EB-342B720A14DE/data/Containers/Bundle/Application/F84F1359-CBCD-4184-B3FD-2C7833B83A60/RN472.app/react-app/assets/Images/diary_mood_icon_alcohol_32@2x.png";
    width = 16;
}

iOS原生通过解析uri信息,获取对应的图片

//RCTImageView.m
- (void)setImageSources:(NSArray<RCTImageSource *> *)imageSources
{
  if (![imageSources isEqual:_imageSources]) {
    _imageSources = [imageSources copy];
    _needsReload = YES;
  }
}

原理摘自链接
由此可见,原生解析完uri就保存了图片信息,我们可以在setImageSources的地方更改图片信息后重新保存

创建RCTImageView分类

创建RCTImageView分类对setImageSources方法进行hook,检查图片资源是否在目标文档沙盒目录中,如未找到,选择本地同名资源文件替换

@implementation RCTImageView (Bundle)

+ (void)load {
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
      [self swizzleMethod:@selector(bundle_setImageSources:) withMethod:@selector(setImageSources:)];
  });
}

//检查资源文件是否在沙盒目录中,如未找到,选择本地同名资源文件替换
- (void)bundle_setImageSources:(NSArray<RCTImageSource *> *)imageSources
{
    NSMutableArray<RCTImageSource *> *newImagesources = [[NSMutableArray<RCTImageSource *> alloc]init];
    for (RCTImageSource *imageSource in imageSources) {
        NSString *imageUrl = imageSource.request.URL.absoluteString;
        if ([imageUrl hasPrefix:@"http"] || [imageUrl hasPrefix:@"data:image/"]) {//网络素材和base646图片不予处理
            [newImagesources addObject:imageSource];
            continue;
        }
        if ([imageUrl hasPrefix:@"file://"]) {
            imageUrl = [imageUrl substringFromIndex:7];
        }
        imageUrl = [imageUrl stringByURLDecoded];
        if ([[NSFileManager defaultManager] fileExistsAtPath:imageUrl]) {//文件存在直接使用
            [newImagesources addObject:imageSource];
            continue;
        }
        NSRange range = [imageUrl rangeOfString:@"assets/"];
        if (range.length > 0 && range.location > 0) {//若文件不存在,检查是否在应用程序沙盒中存在同名文件
            NSString *releateBundlePath = [imageUrl substringFromIndex:range.location];
            NSString *mainPath = [[NSBundle mainBundle] bundlePath];
            //将文档沙盒路径替换成应用程序沙盒路径获取图片
            NSString *localImageUrl = [mainPath stringByAppendingPathComponent:releateBundlePath];
            //转换成RCTImageSource
            RCTImageSource *newImageSource = [RCTConvert RCTImageSource:@{
                                                                          @"__packager_asset":@1,
                                                                          @"height":@(imageSource.size.height),
                                                                          @"width":@(imageSource.size.width),
                                                                          @"scale":@(imageSource.scale),
                                                                          @"uri":localImageUrl
                                                                          }];
            [newImagesources addObject:newImageSource];
        }
    }
    [self bundle_setImageSources:newImagesources];
}

此方案虽然可行,但是需要对原生代码进行hook,且Android端也同样需要实现,对原生不熟悉的同学不大友好,还有别的方案吗?

最终方案

使用hook的方式对react-native js进行修改,保证了项目与node_modules耦合度降低
实现方式:
(1)通过 hook 的方式重新定义 defaultAsset() 方法。
(2)检查图片资源是否在目标文档沙盒目录中,如未找到,选择本地同名资源文件替换。
核心代码如下:

import { NativeModules } from 'react-native';    
import AssetSourceResolver from "react-native/Libraries/Image/AssetSourceResolver";
import _ from 'lodash';
 
let iOSRelateMainBundlePath = '';
let _sourceCodeScriptURL = '';
 
// ios 平台下获取 jsbundle 默认路径
const defaultMainBundePath = AssetsLoad.DefaultMainBundlePath;
 
function getSourceCodeScriptURL() {
    if (_sourceCodeScriptURL) {
        return _sourceCodeScriptURL;
    }
    // 调用Native module获取 JSbundle 路径
    // RN允许开发者在Native端自定义JS的加载路径,在JS端可以调用SourceCode.scriptURL来获取 
    // 如果开发者未指定JSbundle的路径,则在离线环境下返回asset目录
    let sourceCode =
        global.nativeExtensions && global.nativeExtensions.SourceCode;
    if (!sourceCode) {
        sourceCode = NativeModules && NativeModules.SourceCode;
    }
    _sourceCodeScriptURL = sourceCode.scriptURL;
    return _sourceCodeScriptURL;
}
 
// 获取bundle目录下所有drawable 图片资源路径
let drawablePathInfos = [];
AssetsLoad.searchDrawableFile(getSourceCodeScriptURL(),
     (retArray)=>{
      drawablePathInfos = drawablePathInfos.concat(retArray);
});
// hook defaultAsset方法,自定义图片加载方式
AssetSourceResolver.prototype.defaultAsset = _.wrap(AssetSourceResolver.prototype.defaultAsset, function (func, ...args) {
     if (this.isLoadedFromServer()) {
         return this.assetServerURL();
     }
     if (Platform.OS === 'android') {
         if(this.isLoadedFromFileSystem()) {
             // 获取图片资源路径
             let resolvedAssetSource = this.drawableFolderInBundle();
             let resPath = resolvedAssetSource.uri;
             // 获取JSBundle文件所在目录下的所有drawable文件路径,并判断当前图片路径是否存在
             // 如果存在,直接返回
             if(drawablePathInfos.includes(resPath)) {
                 return resolvedAssetSource;
             }
             // 判断图片资源是否存在本地文件目录
             let isFileExist = AssetsLoad.isFileExist(resPath);
             // 存在直接返回
             if(isFileExist) {
                 return resolvedAssetSource;
             } else {
                 // 不存在,则根据资源 Id 从apk包下的drawable目录加载
                 return this.resourceIdentifierWithoutScale();
             }
         } else {
             // 则根据资源 Id 从apk包下的drawable目录加载
             return this.resourceIdentifierWithoutScale();
         }
 
     } else {
         let iOSAsset = this.scaledAssetURLNearBundle();
         let isFileExist =  AssetsLoad.isFileExist(iOSAsset.uri);
         isFileExist = false;
         if(isFileExist) {
             return iOSAsset;
         } else {
             let oriJsBundleUrl = 'file://'+ defaultMainBundePath +'/' + iOSRelateMainBundlePath;
             iOSAsset.uri = iOSAsset.uri.replace(this.jsbundleUrl, oriJsBundleUrl);
             return iOSAsset;
         }
     }
});

该方案参考链接

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,036评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,046评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,411评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,622评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,661评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,521评论 1 304
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,288评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,200评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,644评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,837评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,953评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,673评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,281评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,889评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,011评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,119评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,901评论 2 355

推荐阅读更多精彩内容