歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android recovery 復制大量預裝 APK的方法

要求在產品中預裝大量的第三方app,apk文件有600M多,加上相關資源文件,共計4G。 如何把如此多的文件在安裝時內置到系統成了問題。解決方法有三:   1 在update.zip中實現復制。寫updater-script 通過使用script 復制。見我的另一篇自定義updater-script的文章。   缺點:script腳本需要自己寫,不能隨make生成。   2 在update.zip中實現復制。在recovery.c中實現。   缺點:SDCARD fat對zip文件有大小限制。   3 在第一次系統啟動後實現自動安裝。缺點:太慢,大概需要30分。   方法二的實現:    

 

650) this.width=650;">

  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. } 
Copyright © Linux教程網 All Rights Reserved