Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 內置APK

android 內置APK

編輯:關於Android編程

一、如何將帶源碼的APK預置進系統?

1) 在 packages/apps 下面以需要預置的 APK的 名字創建一個新文件夾,以預置一個名為Test的APK 為例

2) 將 Test APK的Source code 拷貝到 Test 文件夾下,刪除 /bin 和 /gen 目錄

3) 在 Test 目錄下創建一個名為 Android.mk的文件,內容如下:

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)



LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-subdir-java-files)



LOCAL_PACKAGE_NAME := Test

include $(BUILD_PACKAGE)

4) 打開文件 device/mediatek/common/device.mk

將 Test 添加到 PRODUCT_PACKAGES 裡面。

PRODUCT_PACKAGES += Test

5) 重新 build 整個工程

二、如何將無源碼的 APK 預置進系統?

1) 在 packages/apps 下面以需要預置的 APK 名字創建文件夾,以預置一個名為Test的APK為例

2) 將 Test.apk 放到 packages/apps/Test 下面

3) 在 packages/apps/Test 下面創建文件 Android.mk,文件內容如下:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

# Module name should match apk name to be installed

LOCAL_MODULE := Test

LOCAL_MODULE_TAGS := optional



LOCAL_SRC_FILES := $(LOCAL_MODULE).apk

LOCAL_MODULE_CLASS := APPS

LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)



LOCAL_PREBUILT_JNI_LIBS:= /

@lib/armeabi/libtest.so  /

@lib/armeabi/libtest2.so 



LOCAL_CERTIFICATE := PRESIGNED

include $(BUILD_PREBUILT)

若無so,刪除LOCAL_PREBUILT_JNI_LIBS

若有so,使用LOCAL_PREBUILT_JNI_LIBS列出所有so的路徑,不要忘記使用@。@標識符會將apk中的so抽離出來build進apk同級目錄下的lib文件夾中

若apk支持不同cpu類型的so,針對so的部分的處理:

Ifeq ($(TARGET_ARCH),arm)

LOCAL_PREBUILT_JNI_LIBS := /

@lib/armeabi-v7a/xxx.so/

@ lib/armeabi-v7a/xxxx.so

else ifeq ($(TARGET_ARCH),x86)

LOCAL_PREBUILT_JNI_LIBS := /

@lib/x86/xxx.so

else ifeq ($(TARGET_ARCH),arm64)

LOCAL_PREBUILT_JNI_LIBS := /

@lib/armeabi-v8a/xxx.so

…

即將和TARGET_ARCH對應的so抽離出來

4) 打開文件 device/mediatek/common/device.mk

將 Test 添加到 PRODUCT_PACKAGES 裡面。

PRODUCT_PACKAGES += Test

5) 重新 build 整個工程

注:如果App使用System Level的permission,需要預置到/system/priv-app底下 (原在/system/app)。

修改Android.mk,增加LOCAL_PRIVILEGED_MODULE := true,以聲明app需要放在/system/priv-app下。

三、如何預置APK使得用戶可以卸載,恢復出廠設置時不能恢復?

1) 在 packages/apps 下面以需要預置的 APK 名字創建文件夾,以預置一個名為Test的APK為例

2) 將 Test.apk 放到 packages/apps/Test 下面

3) 在 packages/apps/Test 下面創建文件 Android.mk,文件內容如下:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)



# Module name should match apk name to be installed

LOCAL_MODULE := Test

LOCAL_MODULE_TAGS := optional



LOCAL_SRC_FILES := $(LOCAL_MODULE).apk

LOCAL_MODULE_CLASS := APPS

LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)

# LOCAL_PRIVILEGED_MODULE := true

LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)



LOCAL_CERTIFICATE := PRESIGNED

include $(BUILD_PREBUILT)

4) 打開文件 device/mediatek/common/device.mk

將 Test 添加到 PRODUCT_PACKAGES 裡面。

PRODUCT_PACKAGES += Test

5) 重新 build 整個工程

注意:這個比不能卸載的多了一句

LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)

四、如何預置APK使得用戶可以卸載,並且恢復出廠設置時能夠恢復?

1在 vendor/mediatek/proprietary/binary/3rd-party/free下面以需要預置的 APK 名字創建文件夾,以預置一個名為Test的APK為例

2 將Test.apk 放入vendor/mediatek/proprietary/binary/3rd-party/free/Test下面

3 在vendor/mediatek/proprietary/binary/3rd-party/free/Test 下面創建文件 Android.mk,文件內容如下


LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)



# Module name should match apk name to be installed

LOCAL_MODULE := Test

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(LOCAL_MODULE).apk



LOCAL_MODULE_CLASS := APPS

LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)

LOCAL_CERTIFICATE := PRESIGNED



LOCAL_MODULE_PATH := $(TARGET_OUT)/vendor/operator/app

include $(BUILD_PREBUILT)

2 打開文件device/mediatek/common/device.mk

將 Test 添加到 PRODUCT_PACKAGES 裡面。

PRODUCT_PACKAGES += Test

3 然後重新build整個工程

請注意:

若需要apk作為32bit的apk運行,則需要在Android.mk中定義

LOCAL_MULTILIB :=32

若有lib
intel芯片 上面的方法只能導x86的

若沒x86

可以在編譯時把apk拷貝到system/usr/share/apks
編譯腳本中加入:

function build_android()
{
        echo
        echo '[ Build android platform ]'
        echo

        START_TIME=`date +%s`

        check_git_dependece
        if [ $BUILD_PRODUCT = "generic" ]
        then
                rm ./buildspec.mk
                echo make -j$CPU_JOB_NUM
                echo
                make -j$CPU_JOB_NUM
        else
                get_build_mode
                gen_build_ver
                set_default_hw_version
                gen_git_verion
                source build/envsetup.sh
                if [[ $BUILD_PRODUCT = "prom890bap" ]]
                then
                echo "copy prom890bap apk"
                mkdir -p out/target/product/$BUILD_PRODUCT/system/usr/share/apks
                cp device/intel/baytrail/prom890bap/apk/*.apk out/target/product/$BUILD_PRODUCT/system/usr/share/apks/

然後在第一次啟動時安裝apk

diff --git a/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java b/frameworks/base/services/
index 5af3af5..b3825df 100755
--- a/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -2041,6 +2041,10 @@ public class PackageManagerService extends IPackageManager.Stub {
             }
             //delete tmp files
             deleteTempPackageFiles();
+           if(SystemProperties.getInt("ro.yifang.install_apk",0)==1){
+           System.out.println("==============ro.yifang.install_apk==========");
+           PackageManagerArchosUtils.installAppslibIfNeeded(isFirstBoot());
+           }   



PackageManagerArchosUtils代碼如下


+package com.android.server.pm;
+
+import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
+import android.content.ComponentName;
+import android.content.pm.PackageParser;
+import android.os.Environment;
+import android.os.FileUtils;
+import android.os.SystemProperties;
+import android.util.Log;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.Iterator;
+import java.util.HashMap;
+import java.util.Map;
+import java.io.OutputStream;
+import java.util.Properties;
+
+public class PackageManagerArchosUtils {
+       static final String TAG = "PackageManagerArchosUtils";
+
+       static private final boolean ZONEDBG = true;
+
+       static private final String GOOGLE_MARKET_PKG = "com.android.vending"; // New package name since GMSr5
+       static private final String GOOGLE_BOOKS_PKG = "com.google.android.apps.books";
+       static private final String GOOGLE_VIDEO_PKG = "com.google.android.videos";
+       static private final String GOOGLE_YOUTUBE_PKG = "com.google.android.youtube";
+       static private final String GOOGLE_GMS_PREFIX = "com.google.android";
+       static private final String GOOGLE_CHROME_PREFIX = "com.android.chrome"; // for some reason Chrome doesn't use the G
+
+       static private final String OPEN_SOURCE_COMMON_ROOT = "com.android.";
+       static private final String OPEN_SOURCE_CALENDAR = "com.android.calendar";
+       //static private final String OPEN_SOURCE_DESKCLOCK = "com.android.deskclock"; // using the open source version for 
+       //static private final String OPEN_SOURCE_EMAIL = "com.android.email";             // ICS: no more google version, o
+       //static private final String OPEN_SOURCE_EXCHANGE = "com.android.exchange";   // ICS: no more google version, open-
+       static private final String OPEN_SOURCE_GALLERY3D = "com.android.gallery3d";
+       static private final String OPEN_SOURCE_LATINIME = "com.android.inputmethod.latin";
+       static private final String OPEN_SOURCE_QUICKSEARCHBOX = "com.android.quicksearchbox";
+       static private final String OPEN_SOURCE_VIDEOEDITOR = "com.android.videoeditor";
+
+       static private final int ZONE_REST_OF_THE_WORLD = 0;
+       static private final int ZONE_GOOGLE_BOOKS_AND_VIDEOS = 1; //As of 08/2011 => USA only // 01/2012 => Add Videos, rem
+       static private final int ZONE_NO_YOUTUBE = 2; //As of 08/2011 => Belgium and turkey // XX/2011 =>Turkey only // 01/2
+       static private final int ZONE_NO_GOOGLE_APPS = 3; //As of 08/2011 => China
+       // 05/2012: Two new zones:
+       static private final int ZONE_GOOGLE_BOOKS = 4; //As of 05/2012 => Italy  // 08/2012 still Italy only here...
+       static private final int ZONE_GOOGLE_VIDEOS = 5; //As of 05/2012 => France and Japan // 08/2012 Movies and not books
+
+       static private final String ZONE_PROPERTIE = "ro.board.zone";
+       static private final int DEFAULT_ZONE = ZONE_REST_OF_THE_WORLD; // in case the reading or parsing fails, fallback to
+       static private int sZone = -1; // Static to avoid reading too many times. It won't change over time anyway. -1 means
+
+       /**
+        * Get the zone flag in the read only properties.
+        * Actually read the first time only, then stored in a static int. (it won't change)
+        * @return zone code
+        */
+       public static int getZone() {
+               // Read zone if it has not been done yet
+               if (sZone == -1) {
+                       final String zoneString = SystemProperties.get(ZONE_PROPERTIE, Integer.toString(DEFAULT_ZONE));
+                       // First check if this is a number (the current scheme as of 08/2011, may evolve to country code in 
+                       sZone = DEFAULT_ZONE;
+                       try {
+                               sZone = Integer.parseInt(zoneString);
+                       }catch (Exception e) {
+                               if(ZONEDBG) Log.e(TAG, "Failed to parse ro.board.zone", e);
+                       }
+                       if(ZONEDBG) Log.d(TAG, "sZone = "+sZone);
+               }
+               return sZone;
+       }
+
+       /**
+        * Check if this package is authorized on this device, checking country flags
+        * @param pkg
+        * @return false if package must not be installed
+        */
+/*     public static boolean isPackageAuthorizedOnThisDevice(PackageParser.Package pkg) {
+
+               if (pkg.packageName==null) { //sanity check, should never happen
+                       return false;
+               }
+
+               // After the transition from previous Market (com.google.android.finsky) to newer market (com.android.vendin
+               // make sure the previous one is not installed on the device anymore (as advised by google)
+               if ((pkg.packageName.startsWith("com.google.android.finsky"))) {
+                       return false;
+               }
+
+               // The banning mechanism is only for applications from the firmware.
+               // In case the application is installed by Market (or end-user, or adb) it must not be banned, because
+               // GoogleApps like Music2 and Books may become available from the Market in some countries (see #867)
+               if ((pkg.mPath!=null) && (pkg.mPath.startsWith("/data/"))) {
+                       return true; //authorized
+               }
+
+               final int zone = getZone();
+
+
+        // 2012-09-25: new GMS AppHider scheme do the work for us now. We only need to handle the open-source versions
+        // for China that still has no GMS at all
+        final boolean appHider = SystemProperties.getBoolean("ro.com.google.apphider", false);
+
+               // First rule: BAN google books if needed
+               if (!appHider && (sZone != ZONE_GOOGLE_BOOKS_AND_VIDEOS) && (sZone != ZONE_GOOGLE_BOOKS)) {
+                       if (pkg.packageName.equals(GOOGLE_BOOKS_PKG)) {
+                               if(ZONEDBG) Log.d(TAG, "Banning "+pkg.packageName);
+                               return false; //ban
+                       }
+               }
+
+               // Second rule: BAN google videos if needed
+               if (!appHider && (sZone != ZONE_GOOGLE_BOOKS_AND_VIDEOS) && (sZone != ZONE_GOOGLE_VIDEOS))  {
+                       if (pkg.packageName.equals(GOOGLE_VIDEO_PKG)) {
+                               if(ZONEDBG) Log.d(TAG, "Banning "+pkg.packageName);
+                               return false; //ban
+                       }
+               }
+
+               // Third rule: BAN youtube app in Turkey //NOT USED ANYMORE
+               if (!appHider && (zone == ZONE_NO_YOUTUBE)) {
+                       if (pkg.packageName.equals(GOOGLE_YOUTUBE_PKG)) {
+                               if(ZONEDBG) Log.d(TAG, "Banning "+pkg.packageName);
+                               return false; //ban
+                       }
+               }
+
+               // Fourth rule: BAN all GMS (a.k.a. Google Apps) in China (even if app hider is ON!)
+               if (zone == ZONE_NO_GOOGLE_APPS) {
+                       if (pkg.packageName.startsWith(GOOGLE_GMS_PREFIX)) {
+                               if(ZONEDBG) Log.d(TAG, "Banning google app "+pkg.packageName);
+                               return false; //ban
+                       } else if (pkg.packageName.equals(GOOGLE_MARKET_PKG)) {
+                               if(ZONEDBG) Log.d(TAG, "Banning market app "+pkg.packageName);
+                               return false; //ban
+                       } else if (pkg.packageName.startsWith(GOOGLE_CHROME_PREFIX)) {
+                               if(ZONEDBG) Log.d(TAG, "Banning google app "+pkg.packageName);
+                               return false; //ban
+                       }
+                       
+               }
+               // be sure to NOT install the open-source version of the google apps that are embedded in the firmware for c
+               else {
+                       // First test on the common root to avoid doing too many string comparison for ALL (!) the packages
+                       if (pkg.packageName.startsWith(OPEN_SOURCE_COMMON_ROOT)) {
+
+                               if (pkg.packageName.equals(OPEN_SOURCE_CALENDAR)      ||
+                                               //pkg.packageName.equals(OPEN_SOURCE_DESKCLOCK)     ||   // using the open s
+                                               //pkg.packageName.equals(OPEN_SOURCE_EMAIL)         ||   // ICS: no more goo
+                                               //pkg.packageName.equals(OPEN_SOURCE_EXCHANGE)      ||   // ICS: no more goo
+                                               //pkg.packageName.equals(OPEN_SOURCE_GALLERY3D)     ||
+                                               //pkg.packageName.equals(OPEN_SOURCE_LATINIME)      ||
+                                               pkg.packageName.equals(OPEN_SOURCE_QUICKSEARCHBOX)||
+                                               pkg.packageName.equals(OPEN_SOURCE_VIDEOEDITOR)      )
+                               {
+                                       if(ZONEDBG) Log.d(TAG, "Banning open source version "+pkg.packageName);
+                                       return false; //ban
+                               }
+                       }
+               }
+
+               // default: authorized
+               return true;
+       }*/
+
+       /**
+        * Install Appslib if needed, i.e. if AndroidMarket is not authorized for the device's zone
+        * The Appslib APK is stored in a secret location. We install it by copying it in /data/app.
+        */
+       public static void installAppslibIfNeeded(boolean bool) {
+               // Needed only in case google apps (hence market) are not authorized
+               if (bool) {
+                       Map map = null;
+                       try{
+                               File file = new File("/system/usr/share/apks/");
+                               File[] files  = null;
+                               Log.d(TAG, "------- installAppslibIfNeeded ------ file = "+file);
+                               if(null!=file){
+                                       files = file.listFiles();
+                               }
+                               Log.d(TAG, "------- installAppslibIfNeeded ------ files = "+files);
+                               if(null!=files)
+                               for(File filename : files){
+                                       Log.d(TAG, "------- installAppslibIfNeeded ------ filename = "+(filename!=null?filen
+                                       if(null!=filename&&filename.getName().toLowerCase().endsWith(".apk")){
+                                       //  if(null!=filename){
+                                               /*final File apkHidden = new File("/system/usr/share/apks/"+filename.getName
+                                               final File apkInstall = new File(Environment.getDataDirectory() + "/app", fi
+                                               Log.d(TAG, "------- installAppslibIfNeeded ------ Environment.getDataDirecto
+                                               Log.d(TAG, "------- installAppslibIfNeeded ------ apkHidden.exists() = "+apk
+                                               // Check if the APK to install is where expected
+                                               if (apkHidden.exists() == false) {
+                                                       if(ZONEDBG) Log.d(TAG, "installAppslib: " + apkHidden + " not found"
+                                                               return;
+                                               }                                               
+                                               File appfile = new File(Environment.getDataDirectory() + "/app/");
+                                               File[] appfiles = null;
+                                               if(null!=appfile){
+                                                       appfiles = appfile.listFiles();
+                                               }
+
+                                               if(null!=appfiles){
+                                                       Log.d(TAG, "------- installAppslibIfNeeded ------ appfiles = "+appfi
+                                                       boolean isExists = false;
+                                                       for(File appname : appfiles){
+                                                               if(null!=appname&&filename.getName().equals(appname.getName(
+                                                                       isExists = true;
+                                                                       break;
+                                                               }
+                                                       }
+                                                       if(isExists){
+                                                               continue;
+                                                       }
+                                               }
+                                               // Install it! (i.e. copy to /data/app)
+                                               try {
+                                                       InputStream is = new FileInputStream(apkHidden);
+                                                       OutputStream os = new FileOutputStream(apkInstall);
+                                                       byte[] data = new byte[is.available()];
+                                                       is.read(data);
+                                                       os.write(data);
+                                                       is.close();
+                                                       os.close();
+                                                       //Don't forget to set good permissions!
+                                                       FileUtils.setPermissions( apkInstall.getPath(), FileUtils.S_IRWXU|Fi
+                                                       if(ZONEDBG) Log.d(TAG, "------- installAppslib: Copied " + apkHidden
+                                               } catch (IOException e) {
+                                               // Unable to create/copy file
+                                                       if(ZONEDBG) Log.e(TAG, "installAppslib: Error installing " + apkHidd
+                                                       try {
+                                                               if(null==map){
+                                                                       map = new HashMap();
+                                                               }
+                                                               map.put(filename.getName().substring(0, name.getName().lastI
+                                                       }catch(Exception exp){
+                                                               Log.d(TAG, "------- APK installation failure can not be save
+                                                       }*/
+                                                       
+                                                       boolean isInstallationSuccess = installationAPK(filename.getName());
+                                                       if(isInstallationSuccess){
+                                                               try {
+                                                                       if(null==map){
+                                                                               map = new HashMap();
+                                                                       }
+                                                                       map.put(filename.getName().substring(0, filename.get
+                                                               }catch(Exception exp){
+                                                                       Log.d(TAG, "------- APK installation failure can not
+                                                               }
+                                                       }
+                                                       
+                                               
+                                       }
+                               }
+                       }catch(Exception ex){
+                               Log.d(TAG, "------- installApps error ------ ",ex);
+                       }finally{
+                               saveInstallationFailureAPK(map,"/data/system/install_jd_app_error.txt");
+                       }
+                       
+               }else{
+                       continueInstallationFailureAPK("/data/system/install_jd_app_error.txt");
+               }
+       }
+
+       public static boolean installationAPK(String filename){
+               final File apkHidden = new File("/system/usr/share/apks/"+filename);
+               final File apkInstall = new File(Environment.getDataDirectory() + "/app", filename);
+               Log.d(TAG, "------- installAppslibIfNeeded ------ Environment.getDataDirectory() = "+Environment.getDataDire
+               Log.d(TAG, "------- installAppslibIfNeeded ------ apkHidden.exists() = "+apkHidden.exists());
+               // Check if the APK to install is where expected
+               if (apkHidden.exists() == false) {
+                       if(ZONEDBG) Log.d(TAG, "installAppslib: " + apkHidden + " not found");
+                               return false;
+                       }                                               
+               File appfile = new File(Environment.getDataDirectory() + "/app/");
+               File[] appfiles = null;
+               if(null!=appfile){
+                       appfiles = appfile.listFiles();
+               }
+
+               if(null!=appfiles){
+                       Log.d(TAG, "------- installAppslibIfNeeded ------ appfiles = "+appfiles.length);
+                       boolean isExists = false;
+                       for(File appname : appfiles){
+                               if(null!=appname&&filename.equals(appname.getName())){
+                                       isExists = true;
+                                               break;
+                               }
+                       }
+                       if(isExists){
+                               return false;
+                       }
+               }
+               // Install it! (i.e. copy to /data/app)
+               try {
+                       InputStream is = new FileInputStream(apkHidden);
+                       OutputStream os = new FileOutputStream(apkInstall);
+                       byte[] data = new byte[is.available()];
+                       is.read(data);
+                       os.write(data);
+                       is.close();
+                       os.close();
+                       //Don't forget to set good permissions!
+                       FileUtils.setPermissions( apkInstall.getPath(), FileUtils.S_IRWXU|FileUtils.S_IRGRP|FileUtils.S_IROT
+                       if(ZONEDBG) Log.d(TAG, "------- installAppslib: Copied " + apkHidden +" into "+ apkInstall);
+                       } catch (IOException e) {
+                               // Unable to create/copy file
+                               if(ZONEDBG) Log.e(TAG, "installAppslib: Error installing " + apkHidden +" into "+ apkInstall
+                               return true;
+               }
+               return false;
+       }
+
+       public static void continueInstallationFailureAPK(String filepath){
+               Properties pre = new Properties();
+               FileInputStream in = null;
+               try {
+                       File f = new File(filepath);
+                       Log.d(TAG, "------- Have failed to install APK do? answer is : "+f.exists());
+                       if(!f.exists()){
+                               return;
+                       }
+                       Map map = new HashMap();
+                       boolean isAllInstalltionSuccess = true;
+                       in = new FileInputStream(f);
+                       pre.load(in);
+                       Set
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved