Java Code Examples for com.android.utils.FileUtils#mkdirs()

The following examples show how to use com.android.utils.FileUtils#mkdirs() . 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: AwbApkPackageTask.java    From atlas with Apache License 2.0 6 votes vote down vote up
static File copyJavaResourcesOnly(File destinationFolder, File zip64File) throws IOException {
    File cacheDir = new File(destinationFolder, ZIP_64_COPY_DIR);
    File copiedZip = new File(cacheDir, zip64File.getName());
    FileUtils.mkdirs(copiedZip.getParentFile());

    try (ZipFile inFile = new ZipFile(zip64File);
         ZipOutputStream outFile =
                 new ZipOutputStream(
                         new BufferedOutputStream(new FileOutputStream(copiedZip)))) {

        Enumeration<? extends ZipEntry> entries = inFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            if (!zipEntry.getName().endsWith(SdkConstants.DOT_CLASS)) {
                outFile.putNextEntry(new ZipEntry(zipEntry.getName()));
                try {
                    ByteStreams.copy(
                            new BufferedInputStream(inFile.getInputStream(zipEntry)), outFile);
                } finally {
                    outFile.closeEntry();
                }
            }
        }
    }
    return copiedZip;
}
 
Example 2
Source File: AwbAndroidJavaCompile.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Override
protected void compile(IncrementalTaskInputs inputs) {
    getLogger().info(
            "Compiling with source level {} and target level {}.",
            getSourceCompatibility(),
            getTargetCompatibility());
    if (isPostN()) {
        if (!JavaVersion.current().isJava8Compatible()) {
            throw new RuntimeException("compileSdkVersion '" + compileSdkVersion + "' requires "
                    + "JDK 1.8 or later to compile.");
        }
    }

    if (awbBundle.isDataBindEnabled() && !analyticsed) {
        processAnalytics();
        analyticsed = true;
    }

    // Create directory for output of annotation processor.
    FileUtils.mkdirs(annotationProcessorOutputFolder);

    mInstantRunBuildContext.startRecording(InstantRunBuildContext.TaskType.JAVAC);
    compile();
    mInstantRunBuildContext.stopRecording(InstantRunBuildContext.TaskType.JAVAC);
}
 
Example 3
Source File: AtlasDexArchiveBuilderTransform.java    From atlas with Apache License 2.0 5 votes vote down vote up
@NonNull
private static File getPreDexFolder(
        @NonNull TransformOutputProvider output, @NonNull DirectoryInput directoryInput) {

    return FileUtils.mkdirs(
            output.getContentLocation(
                    directoryInput.getName(),
                    ImmutableSet.of(ExtendedContentType.DEX_ARCHIVE),
                    directoryInput.getScopes(),
                    Format.DIRECTORY));
}
 
Example 4
Source File: AtlasMergeJavaResourcesTransform.java    From atlas with Apache License 2.0 5 votes vote down vote up
/**
 * Save the incremental merge state.
 *
 * @param state the state
 * @throws IOException failed to save the state
 */
private void saveMergeState(@NonNull IncrementalFileMergerState state) throws IOException {
    File incrementalFile = incrementalStateFile();

    FileUtils.mkdirs(incrementalFile.getParentFile());
    try (ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(incrementalFile))) {
        o.writeObject(state);
    }
}
 
Example 5
Source File: AtlasMergeJavaResourcesTransform.java    From atlas with Apache License 2.0 5 votes vote down vote up
private void saveAwbMergeState(@NonNull IncrementalFileMergerState state, AwbBundle awbBundle) throws IOException {
    File incrementalFile = incrementalAwbStateFile(awbBundle);

    FileUtils.mkdirs(incrementalFile.getParentFile());
    try (ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(incrementalFile))) {
        o.writeObject(state);
    }
}
 
Example 6
Source File: AwbDataBindingMergeArtifactsTask.java    From atlas with Apache License 2.0 5 votes vote down vote up
private void extractBinFilesFromJar(File outFolder, File jarFile) throws IOException {
    File jarOutFolder = getOutFolderForJarFile(outFolder, jarFile);
    if (jarOutFolder.exists()) {
        FileUtils.deleteDirectoryContents(jarOutFolder);
    }else {
        FileUtils.mkdirs(jarOutFolder);
    }

    try (Closer localCloser = Closer.create()) {
        FileInputStream fis = localCloser.register(new FileInputStream(jarFile));
        ZipInputStream zis = localCloser.register(new ZipInputStream(fis));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                continue;
            }

            String name = entry.getName();

            if (!isResource(name)) {
                continue;
            }
            // get rid of the path. We don't need it since the file name includes the domain
            name = new File(name).getName();
            File out = new File(jarOutFolder, name);
            //noinspection ResultOfMethodCallIgnored
            FileOutputStream fos = localCloser.register(new FileOutputStream(out));
            ByteStreams.copy(zis, fos);
            zis.closeEntry();
        }
    }
}
 
Example 7
Source File: AtlasBundleInstantApp.java    From atlas with Apache License 2.0 5 votes vote down vote up
@TaskAction
public void taskAction() throws IOException {
    FileUtils.mkdirs(bundleDirectory);
    File bundleFile = new File(bundleDirectory, bundleName);
    FileUtils.deleteIfExists(bundleFile);
    File baseFeatureApk = new File(bundleDirectory, "baseFeature.apk");
    if (!apkFile.exists()){
        File[]apkFiles = apkFile.getParentFile().listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.getName().endsWith(".apk");
            }
        });
        if (apkFiles != null){
            apkFile = apkFiles[0];
        }
    }
    if (apkFile.exists()) {
        try {
            make(baseFeatureApk, apkFile, bundleFile, scope.getVariantConfiguration().getSigningConfig());
        } catch (SigningException e) {
            e.printStackTrace();
        }
    }else {
        getLogger().error(apkFile.getAbsolutePath()+" is not exist!");
    }
}
 
Example 8
Source File: JarMergerWithOverride.java    From atlas with Apache License 2.0 5 votes vote down vote up
private void init() throws IOException {
    if (closer == null) {
        FileUtils.mkdirs(jarFile.getParentFile());

        closer = Closer.create();

        FileOutputStream fos = closer.register(new FileOutputStream(jarFile));
        jarOutputStream = closer.register(new JarOutputStream(fos));
    }
}
 
Example 9
Source File: AtlasDexArchiveBuilderTransform.java    From atlas with Apache License 2.0 4 votes vote down vote up
@NonNull
private static File getAwbPreDexFolder(
        @NonNull File output, @NonNull DirectoryInput directoryInput) {
    return FileUtils.mkdirs(new File(output, directoryInput.getName()));
}
 
Example 10
Source File: AwbApkPackageTask.java    From atlas with Apache License 2.0 4 votes vote down vote up
public File splitFullAction(@NonNull ApkData apkData, @Nullable File processedResources)
        throws IOException {

    File incrementalDirForSplit = new File(getIncrementalFolder(awbBundle), apkData.getFullName());

    /*
     * Clear the intermediate build directory. We don't know if anything is in there and
     * since this is a full build, we don't want to get any interference from previous state.
     */
    if (incrementalDirForSplit.exists()) {
        FileUtils.deleteDirectoryContents(incrementalDirForSplit);
    } else {
        FileUtils.mkdirs(incrementalDirForSplit);
    }

    File cacheByPathDir = new File(incrementalDirForSplit, ZIP_DIFF_CACHE_DIR);
    FileUtils.mkdirs(cacheByPathDir);
    FileCacheByPath cacheByPath = new FileCacheByPath(cacheByPathDir);

    /*
     * Clear the cache to make sure we have do not do an incremental build.
     */
    cacheByPath.clear();

    Set<File> androidResources = getAndroidResources(apkData, processedResources);

    appVariantOutputContext.getVariantContext().getProject().getLogger().warn(awbBundle.getName()+" androidResources File:"+androidResources.iterator().next().getAbsolutePath());

    FileUtils.mkdirs(outputDirectory);

    File outputFile = getOutputFile(awbBundle);

    /*
     * Additionally, make sure we have no previous package, if it exists.
     */
    FileUtils.deleteIfExists(outputFile);

    ImmutableMap<RelativeFile, FileStatus> updatedDex =
            IncrementalRelativeFileSets.fromZipsAndDirectories(dexFolders);
    ImmutableMap<RelativeFile, FileStatus> updatedJavaResources = getJavaResourcesChanges();
    ImmutableMap<RelativeFile, FileStatus> updatedAssets =
            IncrementalRelativeFileSets.fromZipsAndDirectories(assets.getFiles());
    ImmutableMap<RelativeFile, FileStatus> updatedAndroidResources =
            IncrementalRelativeFileSets.fromZipsAndDirectories(androidResources);
    ImmutableMap<RelativeFile, FileStatus> updatedJniResources =
            IncrementalRelativeFileSets.fromZipsAndDirectories(jniFolders);

    Collection<BuildOutput> manifestOutputs = BuildOutputs.load(TaskOutputHolder.TaskOutputType.MERGED_MANIFESTS, awbManifestFolder.getSingleFile().getParentFile());

    doTask(
            apkData,
            incrementalDirForSplit,
            outputFile,
            cacheByPath,
            manifestOutputs,
            updatedDex,
            updatedJavaResources,
            updatedAssets,
            updatedAndroidResources,
            updatedJniResources);

    /*
     * Update the known files.
     */
    KnownFilesSaveData saveData = KnownFilesSaveData.make(incrementalDirForSplit);
    saveData.setInputSet(updatedDex.keySet(), KnownFilesSaveData.InputSet.DEX);
    saveData.setInputSet(updatedJavaResources.keySet(), KnownFilesSaveData.InputSet.JAVA_RESOURCE);
    saveData.setInputSet(updatedAssets.keySet(), KnownFilesSaveData.InputSet.ASSET);
    saveData.setInputSet(updatedAndroidResources.keySet(), KnownFilesSaveData.InputSet.ANDROID_RESOURCE);
    saveData.setInputSet(updatedJniResources.keySet(), KnownFilesSaveData.InputSet.NATIVE_RESOURCE);
    saveData.saveCurrentData();
    File file;
    String outputFileName = outputFile.getName();
    file = getAwbPackageOutputFile(appVariantOutputContext.getVariantContext(), outputFileName);
    FileUtils.copyFileToDirectory(outputFile, file.getParentFile());
    return new File(file.getParentFile(), outputFileName);
}
 
Example 11
Source File: AwbApkPackageTask.java    From atlas with Apache License 2.0 4 votes vote down vote up
private void doTask(
        @NonNull ApkData apkData,
        @NonNull File incrementalDirForSplit,
        @NonNull File outputFile,
        @NonNull FileCacheByPath cacheByPath,
        @NonNull Collection<BuildOutput> manifestOutputs,
        @NonNull ImmutableMap<RelativeFile, FileStatus> changedDex,
        @NonNull ImmutableMap<RelativeFile, FileStatus> changedJavaResources,
        @NonNull ImmutableMap<RelativeFile, FileStatus> changedAssets,
        @NonNull ImmutableMap<RelativeFile, FileStatus> changedAndroidResources,
        @NonNull ImmutableMap<RelativeFile, FileStatus> changedNLibs)
        throws IOException {

    ImmutableMap.Builder<RelativeFile, FileStatus> javaResourcesForApk =
            ImmutableMap.builder();
    javaResourcesForApk.putAll(changedJavaResources);

    if (dexPackagingPolicy == DexPackagingPolicy.INSTANT_RUN_MULTI_APK) {
        changedDex = ImmutableMap.copyOf(
                Maps.filterKeys(
                        changedDex,
                        Predicates.compose(
                                Predicates.in(dexFolders.getFiles()),
                                RelativeFile::getBase
                        )));
    }
    final ImmutableMap<RelativeFile, FileStatus> dexFilesToPackage = changedDex;

    String abiFilter = apkData.getFilter(com.android.build.OutputFile.FilterType.ABI);

    // find the manifest file for this split.
    BuildOutput manifestForSplit =
            OutputScope.getOutput(manifestOutputs, TaskOutputHolder.TaskOutputType.MERGED_MANIFESTS, apkData);

    FileUtils.mkdirs(outputFile.getParentFile());

    try (IncrementalPackager packager =
                 new IncrementalPackagerBuilder()
                         .withOutputFile(outputFile)
                         .withSigning(null)
                         .withCreatedBy(androidBuilder.getCreatedBy())
                         .withMinSdk(miniSdkVersion)
                         // TODO: allow extra metadata to be saved in the split scope to avoid
                         // reparsing
                         // these manifest files.
                         .withNativeLibraryPackagingMode(
                                 PackagingUtils.getNativeLibrariesLibrariesPackagingMode(manifestForSplit == null ? awbManifestFolder.getSingleFile() :
                                         manifestForSplit.getOutputFile()))
                         .withNoCompressPredicate(
                                 PackagingUtils.getNoCompressPredicate(
                                         aaptOptionsNoCompress, manifestForSplit == null ? awbManifestFolder.getSingleFile():manifestForSplit.getOutputFile()))
                         .withIntermediateDir(incrementalDirForSplit)
                         .withProject(appVariantOutputContext.getScope().getGlobalScope().getProject())
                         .withDebuggableBuild(debug)
                         .withAcceptedAbis(
                                 abiFilter == null ? supportAbis : ImmutableSet.of(abiFilter))
                         .withJniDebuggableBuild(jniDebug)
                         .build()) {
        packager.updateDex(dexFilesToPackage);
        packager.updateJavaResources(changedJavaResources);
        packager.updateAssets(changedAssets);
        packager.updateAndroidResources(changedAndroidResources);
        packager.updateNativeLibraries(changedNLibs);
        // Only report APK as built if it has actually changed.
        if (packager.hasPendingChangesWithWait()) {
            // FIX-ME : below would not work in multi apk situations. There is code somewhere
            // to ensure we only build ONE multi APK for the target device, make sure it is still
            // active.
            instantRunContext.addChangedFile(FileType.MAIN, outputFile);
        }
    }

    /*
     * Save all used zips in the cache.
     */
    Stream.concat(
            dexFilesToPackage.keySet().stream(),
            Stream.concat(
                    changedJavaResources.keySet().stream(),
                    Stream.concat(
                            changedAndroidResources.keySet().stream(),
                            changedNLibs.keySet().stream())))
            .map(RelativeFile::getBase)
            .filter(File::isFile)
            .distinct()
            .forEach(
                    (File f) -> {
                        try {
                            cacheByPath.add(f);
                        } catch (IOException e) {
                            throw new IOExceptionWrapper(e);
                        }
                    });
}
 
Example 12
Source File: AppVariantOutputContext.java    From atlas with Apache License 2.0 4 votes vote down vote up
public File getIPatchFile(String versionName) {
    if (!getIPatchFolder().exists()){
        FileUtils.mkdirs(getIPatchFolder());
    }
    return new File(getIPatchFolder(),versionName + "@" + versionName + ".ipatch");
}
 
Example 13
Source File: MergeResources.java    From javafxmobile-plugin with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Obtains the temporary directory for {@code aapt} to use.
 *
 * @return the temporary directory
 */
private File getAaptTempDir() {
    return FileUtils.mkdirs(new File(getIncrementalFolder(), "aapt-temp"));
}
 
Example 14
Source File: MergeAwbResource.java    From atlas with Apache License 2.0 2 votes vote down vote up
/**
 * Obtains the temporary directory for {@code aapt} to use.
 *
 * @return the temporary directory
 */
@NonNull
private File getAaptTempDir() {
    return FileUtils.mkdirs(new File(getIncrementalFolder(), "aapt-temp"));
}