你好,游客 登录 注册 搜索
背景:
阅读新闻

Android recovery 复制大量预装 APK的方法

[日期:2012-01-29] 来源:Linux社区  作者:lhc180 [字体:大 中 小]
要求在产品中预装大量的第三方app,apk文件有600M多,加上相关资源文件,共计4G。
如何把如此多的文件在安装时内置到系统成了问题。解决方法有三:
 
1 在update.zip中实现复制。写updater-script 通过使用script 复制。见我的另一篇自定义updater-script的文章。
  缺点:script脚本需要自己写,不能随make生成。
 
2 在update.zip中实现复制。在recovery.c中实现。
  缺点:SDCARD fat对zip文件有大小限制。
 
3 在第一次系统启动后实现自动安装。缺点:太慢,大概需要30分。
 
方法二的实现:
 
 

 

  1. 方法二的实现: 
  2.  
  3. 实现的位置在流程中见图片。 
  4. 在install_package()的结尾的try_update_binary函数结尾(); 在src/bootable/recovery/install.c 
  5.  
  6. 下面是具体实现: 
  7.  
  8. //copy some res file to /data/ 
  9.  
  10. static char *res_list[] = { "/sdcard/ res1.zip", "/sdcard/ res2.zip"}; 
  11.  
  12. static void unzip_res_to_data(void) 
  13. { 
  14.     int i = 0; 
  15.  
  16.     for(i = 0; i < sizeof( res_list)/sizeof(char *); ++i) 
  17.     { 
  18.         ZipArchive zip_res; 
  19.         int err = mzOpenZipArchive( res_list[i], &zip_res); 
  20.         if (err != 0) { 
  21.             LOGI("Can't open %s\n",  res_list[i]); 
  22.         } 
  23.         else { 
  24.             LOGI("start update %s\n", res_list[i]); 
  25.             // To create a consistent system image, never use the clock for timestamps. 
  26.             struct utimbuf timestamp = { 1217592000, 1217592000 };  // 8/1/2008 default 
  27.             bool success = mzExtractRecursive(&zip_res, "res-private", "/data/res-private", 
  28.                     MZ_EXTRACT_FILES_ONLY, &timestamp, 
  29.                     NULL, NULL); 
  30.             LOGI("update %s %s\n", res_list[i], ((success==true)?"success":"failed")); 
  31.             mzCloseZipArchive(&zip_res); 
  32.         } 
  33.     } 
  34.  
  35.     dirSetHierarchyPermissions("/data/res-private", 1000, 1000, 0777, 0666); 
  36. } 
  37.  
  38. //copy some app file to /data/app 
  39. void cpfiles(){ 
  40.  
  41.     ZipArchive zip_apps; 
  42.     int err = mzOpenZipArchive("/sdcard/myapps.zip", &zip_apps); 
  43.     if (err != 0) { 
  44.         LOGI("Can't open %s\n", "/sdcard/myapps.zip"); 
  45.     } 
  46.     else { 
  47.         //here need fix mount for your device 
  48.         if (mount("/dev/block/mmcblk0p13", "/data", "ext4", 
  49.                     MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) { 
  50.             fprintf(stderr, "%s: failed to mount", strerror(errno)); 
  51.         } 
  52.  
  53.         LOGI("start update 3rd-apps\n"); 
  54.         // To create a consistent system image, never use the clock for timestamps. 
  55.         struct utimbuf timestamp = { 1217592000, 1217592000 };  // 8/1/2008 default 
  56.         bool success = mzExtractRecursive(&zip_appss, "app", "/data/app", 
  57.                 MZ_EXTRACT_FILES_ONLY, &timestamp, 
  58.                 NULL, NULL); 
  59.         dirSetHierarchyPermissions("/data/app", 1000, 1000, 0771, 0644); 
  60.         LOGI("update myapps %s\n", ((success==true)?"success":"failed")); 
  61.         mzCloseZipArchive(&zip_apps); 
  62.  
  63. //cp res to /data/ 
  64.         unzip_res_to_data(); 
  65.  
  66.         scan_mounted_volumes(); 
  67.         const MountedVolume* vol = find_mounted_volume_by_mount_point("/data"); 
  68.         if (vol == NULL) { 
  69.             fprintf(stderr, "unmount of %s failed; no such volume\n", "/data"); 
  70.         } else { 
  71.             unmount_mounted_volume(vol); 
  72.         } 
  73.     } 
  74.  
  75. } 
  76.  
  77.  
  78. // If the package contains an update binary, extract it and run it. 
  79. static int 
  80. try_update_binary(const char *path, ZipArchive *zip) { 
  81.   
  82. ....... 
  83.  
  84.     cpfiles(); 
  85.     return INSTALL_SUCCESS; 
  86. } 
linux
相关资讯       Android开发教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款