类android.content.pm.PackageParser.PackageParserException源码实例Demo

下面列出了怎么用android.content.pm.PackageParser.PackageParserException的API类实例代码及写法,或者点击链接到github查看源代码。

/**
 * Make sure the updated priv app is signed with the same key as the original APK file on the
 * /system partition.
 *
 * <p>The rationale is that {@code disabledPkg} is a PackageSetting backed by xml files in /data
 * and is not tamperproof.
 */
private static boolean matchSignatureInSystem(PackageSetting pkgSetting,
        PackageSetting disabledPkgSetting) {
    try {
        PackageParser.collectCertificates(disabledPkgSetting.pkg, true /* skipVerify */);
        if (pkgSetting.signatures.mSigningDetails.checkCapability(
                disabledPkgSetting.signatures.mSigningDetails,
                PackageParser.SigningDetails.CertCapabilities.INSTALLED_DATA)
                || disabledPkgSetting.signatures.mSigningDetails.checkCapability(
                        pkgSetting.signatures.mSigningDetails,
                        PackageParser.SigningDetails.CertCapabilities.ROLLBACK)) {
            return true;
        } else {
            logCriticalInfo(Log.ERROR, "Updated system app mismatches cert on /system: " +
                    pkgSetting.name);
            return false;
        }
    } catch (PackageParserException e) {
        logCriticalInfo(Log.ERROR, "Failed to collect cert for " + pkgSetting.name + ": " +
                e.getMessage());
        return false;
    }
}
 
源代码2 项目: android_9.0.0_r45   文件: DexMetadataHelper.java
/**
 * Validate that the given file is a dex metadata archive.
 * This is just a sanity validation that the file is a zip archive.
 *
 * @throws PackageParserException if the file is not a .dm file.
 */
private static void validateDexMetadataFile(String dmaPath) throws PackageParserException {
    StrictJarFile jarFile = null;
    try {
        jarFile = new StrictJarFile(dmaPath, false, false);
    } catch (IOException e) {
        throw new PackageParserException(INSTALL_FAILED_BAD_DEX_METADATA,
                "Error opening " + dmaPath, e);
    } finally {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (IOException ignored) {
            }
        }
    }
}
 
@Override
protected void constructSplit(int splitIdx, @NonNull int[] configSplitIndices,
        int parentSplitIdx) throws PackageParserException {
    final ArrayList<ApkAssets> assets = new ArrayList<>();

    // Include parent ApkAssets.
    if (parentSplitIdx >= 0) {
        Collections.addAll(assets, mCachedSplitApks[parentSplitIdx]);
    }

    // Include this ApkAssets.
    assets.add(loadApkAssets(mSplitPaths[splitIdx], mFlags));

    // Load and include all config splits for this feature.
    for (int configSplitIdx : configSplitIndices) {
        assets.add(loadApkAssets(mSplitPaths[configSplitIdx], mFlags));
    }

    // Cache the results.
    mCachedSplitApks[splitIdx] = assets.toArray(new ApkAssets[assets.size()]);
    mCachedAssetManagers[splitIdx] = createAssetManagerWithAssets(mCachedSplitApks[splitIdx]);
}
 
源代码4 项目: android_9.0.0_r45   文件: ApkSignatureVerifier.java
private static Certificate[][] loadCertificates(StrictJarFile jarFile, ZipEntry entry)
        throws PackageParserException {
    InputStream is = null;
    try {
        // We must read the stream for the JarEntry to retrieve
        // its certificates.
        is = jarFile.getInputStream(entry);
        readFullyIgnoringContents(is);
        return jarFile.getCertificateChains(entry);
    } catch (IOException | RuntimeException e) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION,
                "Failed reading " + entry.getName() + " in " + jarFile, e);
    } finally {
        IoUtils.closeQuietly(is);
    }
}
 
源代码5 项目: android_9.0.0_r45   文件: DexMetadataHelper.java
/**
 * Validate the dex metadata files installed for the given package.
 *
 * @throws PackageParserException in case of errors.
 */
public static void validatePackageDexMetadata(PackageParser.Package pkg)
        throws PackageParserException {
    Collection<String> apkToDexMetadataList = getPackageDexMetadata(pkg).values();
    for (String dexMetadata : apkToDexMetadataList) {
        validateDexMetadataFile(dexMetadata);
    }
}
 
private static ApkAssets loadApkAssets(String path, @ParseFlags int flags)
        throws PackageParserException {
    if ((flags & PackageParser.PARSE_MUST_BE_APK) != 0 && !PackageParser.isApkPath(path)) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_NOT_APK,
                "Invalid package file: " + path);
    }

    try {
        return ApkAssets.loadFromPath(path);
    } catch (IOException e) {
        throw new PackageParserException(PackageManager.INSTALL_FAILED_INVALID_APK,
                "Failed to load APK at path " + path, e);
    }
}
 
@Override
public AssetManager getSplitAssetManager(int idx) throws PackageParserException {
    // Since we insert the base at position 0, and PackageParser keeps splits separate from
    // the base, we need to adjust the index.
    loadDependenciesForSplit(idx + 1);
    return mCachedAssetManagers[idx + 1];
}
 
private static ApkAssets loadApkAssets(String path, @ParseFlags int flags)
        throws PackageParserException {
    if ((flags & PackageParser.PARSE_MUST_BE_APK) != 0 && !PackageParser.isApkPath(path)) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_NOT_APK,
                "Invalid package file: " + path);
    }

    try {
        return ApkAssets.loadFromPath(path);
    } catch (IOException e) {
        throw new PackageParserException(INSTALL_FAILED_INVALID_APK,
                "Failed to load APK at path " + path, e);
    }
}
 
@Override
public AssetManager getBaseAssetManager() throws PackageParserException {
    if (mCachedAssetManager != null) {
        return mCachedAssetManager;
    }

    ApkAssets[] apkAssets = new ApkAssets[(mSplitCodePaths != null
            ? mSplitCodePaths.length : 0) + 1];

    // Load the base.
    int splitIdx = 0;
    apkAssets[splitIdx++] = loadApkAssets(mBaseCodePath, mFlags);

    // Load any splits.
    if (!ArrayUtils.isEmpty(mSplitCodePaths)) {
        for (String apkPath : mSplitCodePaths) {
            apkAssets[splitIdx++] = loadApkAssets(apkPath, mFlags);
        }
    }

    AssetManager assets = new AssetManager();
    assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            Build.VERSION.RESOURCES_SDK_INT);
    assets.setApkAssets(apkAssets, false /*invalidateCaches*/);

    mCachedAssetManager = assets;
    return mCachedAssetManager;
}
 
源代码10 项目: GPT   文件: ApkTargetMapping.java
/**
 * getPackage
 *
 * @param parser  android.content.pm.PackageParser
 * @param apkFile APK文件
 * @return PackageParser.Package
 */
private static PackageParser.Package getPackage(android.content.pm.PackageParser parser, File apkFile) {

    DisplayMetrics metrics = new DisplayMetrics();
    metrics.setToDefaults();
    final File sourceFile = new File(apkFile.getAbsolutePath());
    android.content.pm.PackageParser.Package pkg = null;

    // 适配5.0
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
        Method mtd = null;
        try {
            mtd = android.content.pm.PackageParser.class.getDeclaredMethod("parsePackage", File.class,
                    int.class);
        } catch (NoSuchMethodException e1) {
            if (Constants.DEBUG) {
                e1.printStackTrace();
            }

        }

        if (mtd != null) {
            try {
                pkg = parser
                        .parsePackage(apkFile, PackageManager.GET_INTENT_FILTERS | PackageManager.GET_RECEIVERS);
            } catch (PackageParserException e) {
                if (Constants.DEBUG) {
                    e.printStackTrace();
                }
            }
        } else { // L的情况
            pkg = parser.parsePackage(sourceFile, apkFile.getAbsolutePath(), metrics,
                    PackageManager.GET_INTENT_FILTERS | PackageManager.GET_RECEIVERS);
        }
    } else {
        pkg = parser.parsePackage(sourceFile, apkFile.getAbsolutePath(), metrics, PackageManager.GET_INTENT_FILTERS
                | PackageManager.GET_RECEIVERS);
    }
    return pkg;
}
 
源代码11 项目: AndroidComponentPlugin   文件: PackageManager.java
/**
 * Retrieve overall information about an application package defined
 * in a package archive file
 *
 * @param archiveFilePath The path to the archive file
 * @param flags Additional option flags. Use any combination of
 * {@link #GET_ACTIVITIES},
 * {@link #GET_GIDS},
 * {@link #GET_CONFIGURATIONS},
 * {@link #GET_INSTRUMENTATION},
 * {@link #GET_PERMISSIONS},
 * {@link #GET_PROVIDERS},
 * {@link #GET_RECEIVERS},
 * {@link #GET_SERVICES},
 * {@link #GET_SIGNATURES}, to modify the data returned.
 *
 * @return Returns the information about the package. Returns
 * null if the package could not be successfully parsed.
 *
 * @see #GET_ACTIVITIES
 * @see #GET_GIDS
 * @see #GET_CONFIGURATIONS
 * @see #GET_INSTRUMENTATION
 * @see #GET_PERMISSIONS
 * @see #GET_PROVIDERS
 * @see #GET_RECEIVERS
 * @see #GET_SERVICES
 * @see #GET_SIGNATURES
 *
 */
public PackageInfo getPackageArchiveInfo(String archiveFilePath, int flags) {
    final PackageParser parser = new PackageParser();
    final File apkFile = new File(archiveFilePath);
    try {
        PackageParser.Package pkg = parser.parseMonolithicPackage(apkFile, 0);
        if ((flags & GET_SIGNATURES) != 0) {
            parser.collectCertificates(pkg, 0);
            parser.collectManifestDigest(pkg);
        }
        PackageUserState state = new PackageUserState();
        return PackageParser.generatePackageInfo(pkg, null, flags, 0, 0, null, state);
    } catch (PackageParserException e) {
        return null;
    }
}
 
源代码12 项目: AndroidComponentPlugin   文件: PackageManager.java
/**
 * Retrieve overall information about an application package defined
 * in a package archive file
 *
 * @param archiveFilePath The path to the archive file
 * @param flags Additional option flags. Use any combination of
 * {@link #GET_ACTIVITIES},
 * {@link #GET_GIDS},
 * {@link #GET_CONFIGURATIONS},
 * {@link #GET_INSTRUMENTATION},
 * {@link #GET_PERMISSIONS},
 * {@link #GET_PROVIDERS},
 * {@link #GET_RECEIVERS},
 * {@link #GET_SERVICES},
 * {@link #GET_SIGNATURES}, to modify the data returned.
 *
 * @return Returns the information about the package. Returns
 * null if the package could not be successfully parsed.
 *
 * @see #GET_ACTIVITIES
 * @see #GET_GIDS
 * @see #GET_CONFIGURATIONS
 * @see #GET_INSTRUMENTATION
 * @see #GET_PERMISSIONS
 * @see #GET_PROVIDERS
 * @see #GET_RECEIVERS
 * @see #GET_SERVICES
 * @see #GET_SIGNATURES
 *
 */
public PackageInfo getPackageArchiveInfo(String archiveFilePath, int flags) {
    final PackageParser parser = new PackageParser();
    final File apkFile = new File(archiveFilePath);
    try {
        PackageParser.Package pkg = parser.parseMonolithicPackage(apkFile, 0);
        if ((flags & GET_SIGNATURES) != 0) {
            parser.collectCertificates(pkg, 0);
            parser.collectManifestDigest(pkg);
        }
        PackageUserState state = new PackageUserState();
        return PackageParser.generatePackageInfo(pkg, null, flags, 0, 0, null, state);
    } catch (PackageParserException e) {
        return null;
    }
}
 
public static PackageManagerException from(PackageParserException e)
        throws PackageManagerException {
    throw new PackageManagerException(e.error, e.getMessage(), e.getCause());
}
 
@Override
public AssetManager getBaseAssetManager() throws PackageParserException {
    loadDependenciesForSplit(0);
    return mCachedAssetManagers[0];
}
 
@Override
public AssetManager getSplitAssetManager(int splitIdx) throws PackageParserException {
    return getBaseAssetManager();
}
 
 类所在包
 类方法
 同包方法