一、提高app 安装速度

一、 提高APP 安装速度方案

提高app安装速度,主要是从dex2oat着手,主要涉及修改点为PackageManagerservice.javaexecutePostCommitSteps方法。

Facebook 、微信安装速度的参考解决方案如下:
修改类如下:
frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java

 
@@ -17209,7 +17209,12 @@ public class PackageManagerService extends PackageManagerServiceExAbs
                 // method because `pkg` may not be in `mPackages` yet.
                 //
                 // Also, don't fail application installs if the dexopt step fails.
-                DexoptOptions dexoptOptions = new DexoptOptions(packageName,
+                DexoptOptions dexoptOptions;
+               if("com.tencent.mm".equals(pkg.packageName)||"com.facebook.katana".equals(pkg.packageName)){
+                   dexoptOptions = new DexoptOptions(pkg.packageName,REASON_INSTALL, "verify", null,
+                                                DexoptOptions.DEXOPT_BOOT_COMPLETE |DexoptOptions.DEXOPT_INSTALL_WITH_DEX_METADATA_FILE);
+               }else{
+               dexoptOptions = new DexoptOptions(packageName,
                         REASON_INSTALL,
                         DexoptOptions.DEXOPT_BOOT_COMPLETE
                                 | DexoptOptions.DEXOPT_INSTALL_WITH_DEX_METADATA_FILE);
@@ -17218,6 +17223,7 @@ public class PackageManagerService extends PackageManagerServiceExAbs
                         getOrCreateCompilerPackageStats(pkg),
                         mDexManager.getPackageUseInfoOrDefault(packageName),
                         dexoptOptions);
+               }
                 Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
             }
 
-- 
2.17.1

二、MTK 平台参考方案

Android O版本、AndroidN版本

1.ROM过大可以通过调整--compiler-filter,减少apk生成odex文件所占据空间的大小,通常来说odex+vdex文件越大,说明dex2oat优化的类越多越彻底,性能也相对较好;

2.Apk安装时间过长可以通过调整--compiler-filter,加快安装;

修改dex2oat的--compiler-filter选项;

oat_location_为要修改apk的关键字,通常使用包名,比如微信:com.tencent.mm;
/art/dex2oat/dex2oat.cc

 void ParseArgs(int argc, char** argv) {

    original_argc = argc;

    original_argv = argv;

 

    InitLogging(argv, Runtime::Abort);

 

    // Skip over argv[0].

    argv++;

    argc--;

 

    if (argc == 0) {

      Usage("No arguments specified");

    }

……

      } else if (option.starts_with("--dirty-image-objects=")) {

        dirty_image_objects_filename_ = option.substr(strlen("--dirty-image-objects=")).data();

      }  else if (!compiler_options_->ParseCompilerOption(option, Usage)) {

        Usage("Unknown argument %s", option.data());

      }

    }

 

          /** MTK begin */

            if(oat_location_ != "" && (oat_location_.find("mtk14456") != std::string::npos || oat_location_.find("com.tencent.mm")!= std::string::npos)){

                        LOG(INFO) << "MTK oat_location_=" << oat_location_ << ",SetCompilerFilter=CompilerOptions::kQuicken";

                        compiler_options_->SetCompilerFilter(CompilerFilter::kQuicken);

            }

            /** MTK end */

           

    ProcessOptions(parser_options.get());

 

    // Insert some compiler things.

    InsertCompileOptions(argc, argv);

  }

下面以Android O版本,微信6.6.2为例:

步骤:

1.下载weixin.apk,重命名为weixin.zip;解压缩,把所有的dex文件push到sdcard

2.adb shell,然后执行下面的命令:

./system/bin/dex2oat --oat-file=/sdcard/mtk14456tools_everything.odex --dex-file=/sdcard/classes.dex --dex-file=/sdcard/classes2.dex --dex-file=/sdcard/classes3.dex --dex-file=/sdcard/classes4.dex --dex-file=/sdcard/classes5.dex --compiler-filter=everything

--compiler-filter参数如下:
--compiler-filter=(assume-verified|extract|verify|quicken|space-profile|space|speed-profile|speed|everything-profile|everything)

通过上面可以得出下面数据:

Time为安装apk时dex2oat的时间;

Odex表示生成odex文件的大小;

Vdex表示生成vdex文件的大小;

表格统计数据是根据下面log和生成在sdcard的odex和vdex得出:

01-01 00:33:58.596 4332-4332/? I/dex2oat: StrippedCommandLine:./system/bin/dex2oat --oat-file=/sdcard/mtk14456tools_everything.odex --dex-file=/sdcard/classes.dex --dex-file=/sdcard/classes2.dex --dex-file=/sdcard/classes3.dex --dex-file=/sdcard/classes4.dex --dex-file=/sdcard/classes5.dex --compiler-filter=everything

01-01 00:34:51.958 4332-4332/? I/dex2oat: dex2oat took 53.364s (341.743s cpu) (threads: 8) arena alloc=27MB (28629200B) java alloc=24MB (25341840B) native alloc=128MB (135004032B) free=29MB (31195264B)

你可能感兴趣的:(一、提高app 安装速度)