protected Predicate<String> getNoCompressPredicate() { return PackagingUtils.getNoCompressPredicate(aaptOptions, manifest); }
@Override public boolean checkEntry(String archivePath) throws ZipAbortException { return parentFilter.checkEntry(archivePath) && !archivePath.endsWith(DOT_CLASS); } }
@NonNull public static Predicate<String> getDefaultNoCompressPredicate() { return getNoCompressPredicateForExtensions(DEFAULT_DONT_COMPRESS_EXTENSIONS); }
/** * Checks an output {@link File} object. * This checks the following: * - the file is not an existing directory. * - if the file exists, that it can be modified. * - if it doesn't exists, that a new file can be created. * @param file the File to check * @throws PackagerException If the check fails */ private void checkOutputFile(File file) throws PackagerException { if (file.isDirectory()) { throw new PackagerException("%s is a directory!", file); } if (file.exists()) { // will be a file in this case. if (!file.canWrite()) { throw new PackagerException("Cannot write %s", file); } } else { try { if (!file.createNewFile()) { throw new PackagerException("Failed to create %s", file); } } catch (IOException e) { throw new PackagerException( "Failed to create '%1$ss': %2$s", file, e.getMessage()); } } }
/** * Adds a file to the APK at a given path * @param file the file to add * @param archivePath the path of the file inside the APK archive. * @throws PackagerException if an error occurred * @throws com.android.builder.packaging.SealedPackageException if the APK is already sealed. * @throws DuplicateFileException if a file conflicts with another already added to the APK * at the same location inside the APK archive. */ @Override public void addFile(File file, String archivePath) throws PackagerException, SealedPackageException, DuplicateFileException { if (mIsSealed) { throw new SealedPackageException("APK is already sealed"); } try { doAddFile(file, archivePath); } catch (DuplicateFileException e) { mBuilder.cleanUp(); throw e; } catch (Exception e) { mBuilder.cleanUp(); throw new PackagerException(e, "Failed to add %s", file); } }
@NonNull public static Predicate<String> getNoCompressPredicate( @NonNull AaptOptions aaptOptions, @NonNull File manifest) { checkNotNull(aaptOptions); checkState(manifest.exists()); NativeLibrariesPackagingMode packagingMode = getNativeLibrariesLibrariesPackagingMode(manifest); return getNoCompressPredicateForExtensions( getAllNoCompressExtensions(aaptOptions, packagingMode)); }
@Override public boolean checkEntry(String archivePath) throws ZipAbortException { mLogger.verbose("=> %s", archivePath); File duplicate = checkFileForDuplicate(archivePath); if (duplicate != null) { throw new DuplicateFileException(archivePath, duplicate, mInputFile); } else { mAddedFiles.put(archivePath, mInputFile); } return true; } }
/** * Checks a file to make sure it should be packaged as standard resources. * @param filePath OS-independent path of the file (including extension), relative to the * archive * @param allowClassFiles whether to allow java class files * @return true if the file should be packaged as standard java resources */ public static boolean checkFileForApkPackaging( @NonNull String filePath, boolean allowClassFiles) { String fileName = new File(filePath).getName(); // ignore hidden files and backup files return !isOfNonResourcesExtensions(Files.getFileExtension(fileName), allowClassFiles) && !filePath.equals("META-INF/MANIFEST.MF") && !isUsedForSigning(filePath) && !isMavenMetadata(filePath); }
@Input public String getNativeLibrariesPackagingModeName() { return PackagingUtils.getNativeLibrariesLibrariesPackagingMode(manifest).toString(); }
/** * Provides an {@link Action} for the archive entry. * @param archivePath the archive entry path in the archive. * @param extractCode whether to extract class files * @return the action to implement. */ @NonNull public static Action getAction(@NonNull String archivePath, boolean extractCode) { // Manifest files are never merged. if (JarFile.MANIFEST_NAME.equals(archivePath)) { return Action.IGNORE; } // split the path into segments. String[] segments = archivePath.split("/"); // empty path? skip to next entry. if (segments.length == 0) { return Action.IGNORE; } return PackagingUtils.checkFileForApkPackaging(archivePath, extractCode) ? Action.COPY : Action.IGNORE; }
throw new PackagerException("%s is a directory!", file); throw new PackagerException("Cannot write %s", file); throw new PackagerException("Failed to create %s", file); throw new PackagerException("Failed to delete newly created %s", file); throw new PackagerException( "Failed to create '%1$ss': %2$s", file, e.getMessage());
throw new SealedPackageException("APK is already sealed"); throw new PackagerException(e, "Failed to add merged file %s", inputFile); mIsSealed = true; } catch (Exception e) { throw new PackagerException(e, "Failed to seal APK"); } finally { mBuilder.cleanUp();
@Override public boolean checkEntry(String archivePath) throws ZipAbortException { mLogger.verbose("=> %s", archivePath); File duplicate = checkFileForDuplicate(archivePath); if (duplicate != null) { // we have a duplicate but it might be the same source file, in this case, // we just ignore the duplicate, and of course, we don't add it again. File potentialDuplicate = new File(mInputFile, archivePath); if (!duplicate.getAbsolutePath().equals(potentialDuplicate.getAbsolutePath())) { throw new DuplicateFileException(archivePath, duplicate, mInputFile); } return false; } else { mAddedFiles.put(archivePath, mInputFile); } return true; } }
private boolean skipEntry( @NonNull ZipEntry entry, @NonNull String path) { if (entry.isDirectory() || JarFile.MANIFEST_NAME.equals(path) || !validator.validateJarPath(path)) { return true; } // split the path into segments. String[] segments = path.split("/"); // empty path? skip to next entry. if (segments.length == 0) { return true; } return !PackagingUtils.checkFileForApkPackaging(path, false /*allowClassFiles*/); }
/** * Checks the merger folder is: * - a directory. * - if the folder exists, that it can be modified. * - if it doesn't exists, that a new folder can be created. * @param file the File to check * @throws PackagerException If the check fails */ private static void checkMergingFolder(File file) throws PackagerException { if (file.isFile()) { throw new PackagerException("%s is a file!", file); } if (file.exists()) { // will be a directory in this case. if (!file.canWrite()) { throw new PackagerException("Cannot write %s", file); } FileUtils.deleteFolder(file); } if (!file.mkdirs()) { throw new PackagerException("Failed to create %s", file); } }
DuplicateFileException { if (mIsSealed) { throw new SealedPackageException("APK is already sealed"); } catch (Exception e) { mBuilder.cleanUp(); throw new PackagerException(e, "Failed to add %s", zipFile); } finally { try {
/** * Checks an input {@link File} object. * This checks the following: * - the file is not an existing directory. * - that the file exists and can be read. * @param file the File to check * @throws FileNotFoundException if the file is not here. * @throws PackagerException If the file is a folder or a file that cannot be read. */ private static void checkInputFile(File file) throws FileNotFoundException, PackagerException { if (file.isDirectory()) { throw new PackagerException("%s is a directory!", file); } if (file.exists()) { if (!file.canRead()) { throw new PackagerException("Cannot read %s", file); } } else { throw new FileNotFoundException(String.format("%s does not exist", file)); } }
/** * Checks an input {@link File} object. * This checks the following: * - the file is not an existing directory. * - that the file exists (if <var>throwIfDoesntExist</var> is <code>false</code>) and can * be read. * @param file the File to check * @throws FileNotFoundException if the file is not here. * @throws PackagerException If the file is a folder or a file that cannot be read. */ private static void checkInputFile(File file) throws FileNotFoundException, PackagerException { if (file.isDirectory()) { throw new PackagerException("%s is a directory!", file); } if (file.exists()) { if (!file.canRead()) { throw new PackagerException("Cannot read %s", file); } } else { throw new FileNotFoundException(String.format("%s does not exist", file)); } }
throw e; } catch (Exception e) { throw new PackagerException(e, "Failed to add %s", sourceFolder); throw new PackagerException("%s is not a folder", sourceFolder);
private void doAddFile( @NonNull File file, @NonNull String archivePath) throws PackagerException { Preconditions.checkNotNull(mApkCreator, "mApkCreator == null"); mAddedFiles.put(archivePath, file); try { mApkCreator.writeFile(file, archivePath); } catch (IOException e) { throw new PackagerException(e); } }