android.content.pm.PackageParser.PackageParserException Java Examples

The following examples show how to use android.content.pm.PackageParser.PackageParserException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PackageManagerServiceUtils.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example #2
Source File: DexMetadataHelper.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * 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) {
            }
        }
    }
}
 
Example #3
Source File: SplitAssetDependencyLoader.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@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]);
}
 
Example #4
Source File: ApkSignatureVerifier.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #5
Source File: DexMetadataHelper.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example #6
Source File: SplitAssetDependencyLoader.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #7
Source File: SplitAssetDependencyLoader.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@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];
}
 
Example #8
Source File: DefaultSplitAssetLoader.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #9
Source File: DefaultSplitAssetLoader.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #10
Source File: ApkTargetMapping.java    From GPT with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #11
Source File: PackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
    }
}
 
Example #12
Source File: PackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
    }
}
 
Example #13
Source File: PackageManagerException.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
public static PackageManagerException from(PackageParserException e)
        throws PackageManagerException {
    throw new PackageManagerException(e.error, e.getMessage(), e.getCause());
}
 
Example #14
Source File: SplitAssetDependencyLoader.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public AssetManager getBaseAssetManager() throws PackageParserException {
    loadDependenciesForSplit(0);
    return mCachedAssetManagers[0];
}
 
Example #15
Source File: DefaultSplitAssetLoader.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public AssetManager getSplitAssetManager(int splitIdx) throws PackageParserException {
    return getBaseAssetManager();
}