1. 程式人生 > >新增額外的路徑到Android系統中

新增額外的路徑到Android系統中

1.開機啟動掃描某一路徑下的app

在frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java,有如下程式碼。

// Collected privileged system packages.
final File privilegedAppDir = new File(Environment.getRootDirectory(), "priv-app");
      scanDirLI(privilegedAppDir, PackageParser.PARSE_IS_SYSTEM
                    | PackageParser.PARSE_IS_SYSTEM_DIR
                    | PackageParser.PARSE_IS_PRIVILEGED, scanFlags, 0);

// Collect ordinary system packages.
final File systemAppDir = new File(Environment.getRootDirectory(), "app");
      scanDirLI(systemAppDir, PackageParser.PARSE_IS_SYSTEM
                    | PackageParser.PARSE_IS_SYSTEM_DIR, scanFlags, 0);

// Collect all vendor packages.
File vendorAppDir = new File("/vendor/app");
     try {
                vendorAppDir = vendorAppDir.getCanonicalFile();
         } catch (IOException e) {
                // failed to look up canonical path, continue with original one
         }
         scanDirLI(vendorAppDir, PackageParser.PARSE_IS_SYSTEM
                    | PackageParser.PARSE_IS_SYSTEM_DIR, scanFlags, 0);
如果我們要掃描另外的路徑,只需在原始碼中新增自己的路徑即可,如
File MyAppDir = new File("/mysystem/app");
     scanDirLI(MyAppDir, PackageParser.PARSE_IS_SYSTEM
                    | PackageParser.PARSE_IS_SYSTEM_DIR, scanFlags, 0);
File MyPrivAppDir = new File("/mysystem/priv_app");
     scanDirLI(MyPrivAppDir, PackageParser.PARSE_IS_SYSTEM
                    | PackageParser.PARSE_IS_SYSTEM_DIR,                                  
                    | PackageParser.PARSE_IS_PRIVILEGED, scanFlags, 0);

在locationIsprivileged方法中,還需新增一下如下程式碼

static boolean locationIsPrivileged(File path) {
     try {
         final String privilegedAppDir = new File(Environment.getRootDirectory(), "priv-app")
                 .getCanonicalPath();
         final String my_privilegedAppDir = new File("/mysystem/priv-app")
                 .getCanonicalPath();
         return path.getCanonicalPath().startsWith(privilegedAppDir)||path.getCanonicalPath().startsWith(my_privilegedAppDir);
     } catch (IOException e) {
         Slog.e(TAG, "Unable to access code path " + path);
     }
     return false;
}
參考http://www.woaitqs.cc/android/2016/07/28/android-plugin-get-apk-info.html

2.掃描自定義的庫路徑

開啟bionic/linker/linker.cpp檔案,看到如下程式碼,LP64就是64位的機器,看你用的機器是32位的還是64位的,新增自己的路徑即可。

static const char* const kDefaultLdPaths[] = {
#if defined(__LP64__)
  "/vendor/lib64",
  "/system/lib64",
#else
  "/vendor/lib",
  "/system/lib",
#endif
  nullptr
};

3.掃描自定義的硬體抽象庫路徑

開啟hardware/libhardware/hardware.c,有你如下程式碼,新增自己的路徑即可。

#if defined(__LP64__)
#define HAL_LIBRARY_PATH1 "/system/lib64/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib64/hw"
#else
#define HAL_LIBRARY_PATH1 "/system/lib/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib/hw"
#endif

並在下列函式中新增相應的路徑。

static int hw_module_exists(char *path, size_t path_len, const char *name,
                            const char *subname)
{
    snprintf(path, path_len, "%s/%s.%s.so",
             HAL_LIBRARY_PATH2, name, subname);
    if (access(path, R_OK) == 0)
        return 0;

    snprintf(path, path_len, "%s/%s.%s.so",
             HAL_LIBRARY_PATH1, name, subname);
    if (access(path, R_OK) == 0)
        return 0;

    return -ENOENT;
}


4.掃描自定義的韌體路徑