Search in sources :

Example 1 with BuildOutput

use of com.android.build.gradle.internal.scope.BuildOutput in project atlas by alibaba.

the class AwbApkPackageTask method splitIncrementalAction.

private File splitIncrementalAction(ApkData apkData, @Nullable File processedResources, Map<File, FileStatus> changedInputs) throws IOException {
    Set<File> androidResources = getAndroidResources(apkData, processedResources);
    File incrementalDirForSplit = new File(getIncrementalFolder(awbBundle), apkData.getFullName());
    File cacheByPathDir = new File(incrementalDirForSplit, ZIP_DIFF_CACHE_DIR);
    if (!cacheByPathDir.exists()) {
        FileUtils.mkdirs(cacheByPathDir);
    }
    FileCacheByPath cacheByPath = new FileCacheByPath(cacheByPathDir);
    KnownFilesSaveData saveData = KnownFilesSaveData.make(incrementalDirForSplit);
    final Set<File> assetsFiles = assets.getFiles();
    Set<Runnable> cacheUpdates = new HashSet<>();
    ImmutableMap<RelativeFile, FileStatus> changedDexFiles = KnownFilesSaveData.getChangedInputs(changedInputs, saveData, KnownFilesSaveData.InputSet.DEX, dexFolders.getFiles(), cacheByPath, cacheUpdates);
    ImmutableMap<RelativeFile, FileStatus> changedJavaResources;
    try {
        changedJavaResources = KnownFilesSaveData.getChangedInputs(changedInputs, saveData, KnownFilesSaveData.InputSet.JAVA_RESOURCE, javaResouresFiles.getFiles(), cacheByPath, cacheUpdates);
    } catch (Zip64NotSupportedException e) {
        // copy all changedInputs into a smaller jar and rerun.
        ImmutableMap.Builder<File, FileStatus> copiedInputs = ImmutableMap.builder();
        for (Map.Entry<File, FileStatus> fileFileStatusEntry : changedInputs.entrySet()) {
            copiedInputs.put(copyJavaResourcesOnly(getIncrementalFolder(awbBundle), fileFileStatusEntry.getKey()), fileFileStatusEntry.getValue());
        }
        changedJavaResources = KnownFilesSaveData.getChangedInputs(copiedInputs.build(), saveData, KnownFilesSaveData.InputSet.JAVA_RESOURCE, javaResouresFiles.getFiles(), cacheByPath, cacheUpdates);
    }
    ImmutableMap<RelativeFile, FileStatus> changedAssets = KnownFilesSaveData.getChangedInputs(changedInputs, saveData, KnownFilesSaveData.InputSet.ASSET, assetsFiles, cacheByPath, cacheUpdates);
    ImmutableMap<RelativeFile, FileStatus> changedAndroidResources = KnownFilesSaveData.getChangedInputs(changedInputs, saveData, KnownFilesSaveData.InputSet.ANDROID_RESOURCE, androidResources, cacheByPath, cacheUpdates);
    ImmutableMap<RelativeFile, FileStatus> changedNLibs = KnownFilesSaveData.getChangedInputs(changedInputs, saveData, KnownFilesSaveData.InputSet.NATIVE_RESOURCE, jniFolders.getFiles(), cacheByPath, cacheUpdates);
    File outputFile = getOutputFile(awbBundle);
    Collection<BuildOutput> manifestOutputs = BuildOutputs.load(TaskOutputHolder.TaskOutputType.MERGED_MANIFESTS, awbManifestFolder.getSingleFile().getParentFile());
    doTask(apkData, incrementalDirForSplit, outputFile, cacheByPath, manifestOutputs, changedDexFiles, changedJavaResources, changedAssets, changedAndroidResources, changedNLibs);
    /*
         * Update the cache
         */
    cacheUpdates.forEach(Runnable::run);
    /*
         * Update the save data keep files.
         */
    ImmutableMap<RelativeFile, FileStatus> allDex = IncrementalRelativeFileSets.fromZipsAndDirectories(dexFolders);
    ImmutableMap<RelativeFile, FileStatus> allJavaResources = IncrementalRelativeFileSets.fromZipsAndDirectories(javaResouresFiles);
    ImmutableMap<RelativeFile, FileStatus> allAssets = IncrementalRelativeFileSets.fromZipsAndDirectories(assetsFiles);
    ImmutableMap<RelativeFile, FileStatus> allAndroidResources = IncrementalRelativeFileSets.fromZipsAndDirectories(androidResources);
    ImmutableMap<RelativeFile, FileStatus> allJniResources = IncrementalRelativeFileSets.fromZipsAndDirectories(jniFolders);
    saveData.setInputSet(allDex.keySet(), KnownFilesSaveData.InputSet.DEX);
    saveData.setInputSet(allJavaResources.keySet(), KnownFilesSaveData.InputSet.JAVA_RESOURCE);
    saveData.setInputSet(allAssets.keySet(), KnownFilesSaveData.InputSet.ASSET);
    saveData.setInputSet(allAndroidResources.keySet(), KnownFilesSaveData.InputSet.ANDROID_RESOURCE);
    saveData.setInputSet(allJniResources.keySet(), KnownFilesSaveData.InputSet.NATIVE_RESOURCE);
    saveData.saveCurrentData();
    return outputFile;
}
Also used : FileStatus(com.android.ide.common.res2.FileStatus) RelativeFile(com.android.builder.files.RelativeFile) AndroidBuilder(com.android.builder.core.AndroidBuilder) IncrementalPackagerBuilder(com.android.build.gradle.internal.packaging.IncrementalPackagerBuilder) KnownFilesSaveData(com.android.build.gradle.internal.tasks.KnownFilesSaveData) ZipEntry(java.util.zip.ZipEntry) BuildOutput(com.android.build.gradle.internal.scope.BuildOutput) FileCacheByPath(com.android.builder.files.FileCacheByPath) Zip64NotSupportedException(com.android.apkzlib.zip.compress.Zip64NotSupportedException) ZipFile(java.util.zip.ZipFile) RelativeFile(com.android.builder.files.RelativeFile) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with BuildOutput

use of com.android.build.gradle.internal.scope.BuildOutput in project atlas by alibaba.

the class AwbApkPackageTask method doTask.

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).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);
        }
    });
}
Also used : FileStatus(com.android.ide.common.res2.FileStatus) BuildOutput(com.android.build.gradle.internal.scope.BuildOutput) IOExceptionWrapper(com.android.apkzlib.utils.IOExceptionWrapper) RelativeFile(com.android.builder.files.RelativeFile) IncrementalPackager(com.android.builder.internal.packaging.IncrementalPackager) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) RelativeFile(com.android.builder.files.RelativeFile) File(java.io.File) ImmutableMap(com.google.common.collect.ImmutableMap) IncrementalPackagerBuilder(com.android.build.gradle.internal.packaging.IncrementalPackagerBuilder)

Example 3 with BuildOutput

use of com.android.build.gradle.internal.scope.BuildOutput in project atlas by alibaba.

the class FeatureLibManifestTask method taskAction.

@TaskAction
public void taskAction() throws DocumentException, IOException {
    mainManifestFile = variantConfiguration.getMainManifest();
    ManifestInfo mainManifestFileObject = getManifestFileObject(mainManifestFile);
    final Set<ResolvedArtifactResult> artifacts = manifests.getArtifacts();
    List<ManifestProvider> providers = Lists.newArrayListWithCapacity(artifacts.size() + 2);
    for (ResolvedArtifactResult artifact : artifacts) {
        File manifestFile = artifact.getFile();
        File modifyManifest = featureVariantContext.getModifiedManifest(artifact);
        ManifestFileUtils.updatePreProcessManifestFile(modifyManifest, manifestFile, mainManifestFileObject, true, featureVariantContext.getAtlasExtension().getTBuildConfig().isIncremental());
        providers.add(new MergeManifests.ConfigAction.ManifestProviderImpl(modifyManifest, MergeManifests.getArtifactName(artifact)));
    }
    if (featureManifests != null) {
        final Set<ResolvedArtifactResult> featureArtifacts = featureManifests.getArtifacts();
        for (ResolvedArtifactResult artifact : featureArtifacts) {
            File directory = artifact.getFile();
            File modifyManifest = featureVariantContext.getModifiedManifest(artifact);
            Collection<BuildOutput> splitOutputs = BuildOutputs.load(VariantScope.TaskOutputType.MERGED_MANIFESTS, directory);
            if (splitOutputs.isEmpty()) {
                throw new GradleException("Could not load manifest from " + directory);
            }
            ManifestFileUtils.updatePreProcessManifestFile(modifyManifest, splitOutputs.iterator().next().getOutputFile(), mainManifestFileObject, true, featureVariantContext.getAtlasExtension().getTBuildConfig().isIncremental());
            providers.add(new MergeManifests.ConfigAction.ManifestProviderImpl(modifyManifest, MergeManifests.getArtifactName(artifact)));
        }
    }
    AtlasBuildContext.androidBuilderMap.get(getProject()).manifestProviders = providers;
}
Also used : BuildOutput(com.android.build.gradle.internal.scope.BuildOutput) GradleException(org.gradle.api.GradleException) ManifestInfo(com.taobao.android.builder.tools.manifest.ManifestInfo) ResolvedArtifactResult(org.gradle.api.artifacts.result.ResolvedArtifactResult) File(java.io.File) ManifestProvider(com.android.manifmerger.ManifestProvider) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 4 with BuildOutput

use of com.android.build.gradle.internal.scope.BuildOutput in project atlas by alibaba.

the class AwbApkPackageTask method splitFullAction.

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);
}
Also used : FileStatus(com.android.ide.common.res2.FileStatus) BuildOutput(com.android.build.gradle.internal.scope.BuildOutput) FileCacheByPath(com.android.builder.files.FileCacheByPath) RelativeFile(com.android.builder.files.RelativeFile) KnownFilesSaveData(com.android.build.gradle.internal.tasks.KnownFilesSaveData) ZipFile(java.util.zip.ZipFile) RelativeFile(com.android.builder.files.RelativeFile) File(java.io.File)

Aggregations

BuildOutput (com.android.build.gradle.internal.scope.BuildOutput)4 File (java.io.File)4 RelativeFile (com.android.builder.files.RelativeFile)3 FileStatus (com.android.ide.common.res2.FileStatus)3 ZipFile (java.util.zip.ZipFile)3 IncrementalPackagerBuilder (com.android.build.gradle.internal.packaging.IncrementalPackagerBuilder)2 KnownFilesSaveData (com.android.build.gradle.internal.tasks.KnownFilesSaveData)2 FileCacheByPath (com.android.builder.files.FileCacheByPath)2 IOExceptionWrapper (com.android.apkzlib.utils.IOExceptionWrapper)1 Zip64NotSupportedException (com.android.apkzlib.zip.compress.Zip64NotSupportedException)1 AndroidBuilder (com.android.builder.core.AndroidBuilder)1 IncrementalPackager (com.android.builder.internal.packaging.IncrementalPackager)1 ManifestProvider (com.android.manifmerger.ManifestProvider)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 MtlBaseTaskAction (com.taobao.android.builder.tasks.manager.MtlBaseTaskAction)1 ManifestInfo (com.taobao.android.builder.tools.manifest.ManifestInfo)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 ZipEntry (java.util.zip.ZipEntry)1 GradleException (org.gradle.api.GradleException)1