Search in sources :

Example 1 with AtlasBuilder

use of com.android.builder.core.AtlasBuilder in project atlas by alibaba.

the class ProcessAwoAndroidResources method doFullTaskAction.

@Override
protected void doFullTaskAction() throws IOException {
    // we have to clean the source folder output in case the package name changed.
    File srcOut = getSourceOutputDir();
    if (srcOut != null) {
        // FileUtils.emptyFolder(srcOut);
        srcOut.delete();
        srcOut.mkdirs();
    }
    @Nullable File resOutBaseNameFile = getPackageOutputFile();
    // If are in instant run mode and we have an instant run enabled manifest
    File instantRunManifest = getInstantRunManifestFile();
    File manifestFileToPackage = instantRunBuildContext.isInInstantRunMode() && instantRunManifest != null && instantRunManifest.exists() ? instantRunManifest : getManifestFile();
    // 增加awb模块编译所需要的额外参数
    addAaptOptions();
    AaptPackageProcessBuilder aaptPackageCommandBuilder = new AaptPackageProcessBuilder(manifestFileToPackage, getAaptOptions()).setAssetsFolder(getAssetsDir()).setResFolder(getResDir()).setLibraries(getLibraries()).setPackageForR(getPackageForR()).setSourceOutputDir(absolutePath(srcOut)).setSymbolOutputDir(absolutePath(getTextSymbolOutputDir())).setResPackageOutput(absolutePath(resOutBaseNameFile)).setProguardOutput(absolutePath(getProguardOutputFile())).setType(getType()).setDebuggable(getDebuggable()).setPseudoLocalesEnabled(getPseudoLocalesEnabled()).setResourceConfigs(getResourceConfigs()).setSplits(getSplits()).setPreferredDensity(getPreferredDensity());
    @NonNull AtlasBuilder builder = (AtlasBuilder) getBuilder();
    // MergingLog mergingLog = new MergingLog(getMergeBlameLogFolder());
    //
    // ProcessOutputHandler processOutputHandler = new ParsingProcessOutputHandler(
    // new ToolOutputParser(new AaptOutputParser(), getILogger()),
    // builder.getErrorReporter());
    ProcessOutputHandler processOutputHandler = new LoggedProcessOutputHandler(getILogger());
    try {
        builder.processAwbResources(aaptPackageCommandBuilder, getEnforceUniquePackageName(), processOutputHandler, getMainSymbolFile());
        if (resOutBaseNameFile != null) {
            if (instantRunBuildContext.isInInstantRunMode()) {
                instantRunBuildContext.addChangedFile(InstantRunBuildContext.FileType.RESOURCES, resOutBaseNameFile);
                // get the new manifest file CRC
                JarFile jarFile = new JarFile(resOutBaseNameFile);
                String currentIterationCRC = null;
                try {
                    ZipEntry entry = jarFile.getEntry(SdkConstants.ANDROID_MANIFEST_XML);
                    if (entry != null) {
                        currentIterationCRC = String.valueOf(entry.getCrc());
                    }
                } finally {
                    jarFile.close();
                }
                // check the manifest file binary format.
                File crcFile = new File(instantRunSupportDir, "manifest.crc");
                if (crcFile.exists() && currentIterationCRC != null) {
                    // compare its content with the new binary file crc.
                    String previousIterationCRC = Files.readFirstLine(crcFile, Charsets.UTF_8);
                    if (!currentIterationCRC.equals(previousIterationCRC)) {
                        instantRunBuildContext.close();
                        instantRunBuildContext.setVerifierResult(InstantRunVerifierStatus.BINARY_MANIFEST_FILE_CHANGE);
                    }
                }
                // write the new manifest file CRC.
                Files.createParentDirs(crcFile);
                Files.write(currentIterationCRC, crcFile, Charsets.UTF_8);
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (ProcessException e) {
        throw new RuntimeException(e);
    }
}
Also used : LoggedProcessOutputHandler(com.android.ide.common.process.LoggedProcessOutputHandler) ProcessOutputHandler(com.android.ide.common.process.ProcessOutputHandler) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) ProcessException(com.android.ide.common.process.ProcessException) AaptPackageProcessBuilder(com.android.builder.core.AaptPackageProcessBuilder) NonNull(com.android.annotations.NonNull) AtlasBuilder(com.android.builder.core.AtlasBuilder) LoggedProcessOutputHandler(com.android.ide.common.process.LoggedProcessOutputHandler) JarFile(java.util.jar.JarFile) OutputFile(org.gradle.api.tasks.OutputFile) File(java.io.File) InputFile(org.gradle.api.tasks.InputFile) Nullable(com.android.annotations.Nullable)

Example 2 with AtlasBuilder

use of com.android.builder.core.AtlasBuilder in project atlas by alibaba.

the class AtlasConfigurationHelper method createBuilderAfterEvaluate.

public void createBuilderAfterEvaluate() throws Exception {
    AndroidBuilder androidBuilder = appPluginHook.getAndroidBuilder();
    if (null == androidBuilder) {
        return;
    }
    AndroidBuilder atlasBuilder = new AtlasBuilder(project.equals(project.getRootProject()) ? project.getName() : project.getPath(), creator, new GradleProcessExecutor(project), new GradleJavaProcessExecutor(project), DefaultGroovyMethods.asType(ReflectUtils.getField(androidBuilder, "mErrorReporter"), ErrorReporter.class), LoggerWrapper.getLogger(AtlasBuilder.class), DefaultGroovyMethods.asType(ReflectUtils.getField(androidBuilder, "mVerboseExec"), Boolean.class));
    ((AtlasBuilder) atlasBuilder).setDefaultBuilder(androidBuilder);
    ((AtlasBuilder) atlasBuilder).setAtlasExtension(atlasExtension);
    AtlasBuildContext.androidBuilderMap.put(project, (AtlasBuilder) (atlasBuilder));
}
Also used : ErrorReporter(com.android.builder.core.ErrorReporter) GradleProcessExecutor(com.android.build.gradle.internal.process.GradleProcessExecutor) AndroidBuilder(com.android.builder.core.AndroidBuilder) AtlasBuilder(com.android.builder.core.AtlasBuilder) GradleJavaProcessExecutor(com.android.build.gradle.internal.process.GradleJavaProcessExecutor)

Example 3 with AtlasBuilder

use of com.android.builder.core.AtlasBuilder in project atlas by alibaba.

the class ProcessAwbAndroidResources method doFullTaskAction.

@Override
protected void doFullTaskAction() throws IOException {
    File srcOut = getSourceOutputDir();
    aaptGeneration = AaptGeneration.fromProjectOptions(appVariantContext.getScope().getGlobalScope().getProjectOptions());
    if (srcOut != null) {
        // FileUtils.emptyFolder(srcOut);
        srcOut.delete();
        srcOut.mkdirs();
    }
    getTextSymbolOutputDir().mkdirs();
    getPackageOutputFile().getParentFile().mkdirs();
    @Nullable File resOutBaseNameFile = getPackageOutputFile();
    // If are in instant run mode and we have an instant run enabled manifest
    File instantRunManifest = getInstantRunManifestFile();
    File manifestFileToPackage = instantRunBuildContext.isInInstantRunMode() && instantRunManifest != null && instantRunManifest.exists() ? instantRunManifest : getManifestFile();
    // Add additional parameters required for the awb module to compile
    Set<File> libraries = new HashSet<>();
    for (AndroidLibrary androidLibrary : getLibraries()) {
        if (androidLibrary.getSymbolFile().exists()) {
            if (androidLibrary.getManifest().exists() && androidLibrary.getSymbolFile().exists()) {
                File libraryRtxt = androidLibrary.getSymbolFile();
                File awbPackageR = new File(libraryRtxt.getParentFile(), "package-aware-r.txt");
                SymbolIo.writeSymbolTableWithPackage(libraryRtxt.toPath(), androidLibrary.getManifest().toPath(), awbPackageR.toPath());
                libraries.add(awbPackageR);
            }
        }
    }
    // libraries.addAll(mainDexSymbolFileCollection.getFiles());
    addAaptOptions(aaptGeneration);
    AaptPackageConfig.Builder aaptPackageCommandBuilder = new AaptPackageConfig.Builder().setManifestFile(manifestFileToPackage).setOptions(DslAdaptersKt.convert(getAaptOptions())).setResourceDir(getResDir()).setLibrarySymbolTableFiles(libraries).setCustomPackageForR(getPackageForR()).setSourceOutputDir(srcOut).setSymbolOutputDir(getTextSymbolOutputDir()).setResourceOutputApk(resOutBaseNameFile).setProguardOutputFile(getProguardOutputFile()).setVariantType(getType()).setDebuggable(debuggable).setResourceConfigs(getResourceConfigs()).setSplits(getSplits()).setPreferredDensity(getPreferredDensity()).setPseudoLocalize(getPseudoLocalesEnabled()).setListResourceFiles(aaptGeneration == AaptGeneration.AAPT_V2);
    // .setPackageId(Integer.valueOf(getCustomPackageId()));
    @NonNull AndroidBuilder builder = getBuilder();
    Aapt aapt = makeAapt();
    try {
        if (builder instanceof AtlasBuilder) {
            ((AtlasBuilder) builder).processAwbResources(aapt, aaptPackageCommandBuilder, getMainSymbolFile(), awbBundle.getAndroidLibrary().getSymbolFile(), appVariantContext.getVariantData().getApplicationId());
        } else {
            builder.processResources(aapt, aaptPackageCommandBuilder);
        }
        if (resOutBaseNameFile != null) {
            if (instantRunBuildContext.isInInstantRunMode()) {
                instantRunBuildContext.addChangedFile(FileType.RESOURCES, resOutBaseNameFile);
                // get the new manifest file CRC
                JarFile jarFile = new JarFile(resOutBaseNameFile);
                String currentIterationCRC = null;
                try {
                    ZipEntry entry = jarFile.getEntry(SdkConstants.ANDROID_MANIFEST_XML);
                    if (entry != null) {
                        currentIterationCRC = String.valueOf(entry.getCrc());
                    }
                } finally {
                    jarFile.close();
                }
                // check the manifest file binary format.
                File crcFile = new File(instantRunSupportDir, "manifest.crc");
                if (crcFile.exists() && currentIterationCRC != null) {
                    // compare its content with the new binary file crc.
                    String previousIterationCRC = Files.readFirstLine(crcFile, Charsets.UTF_8);
                    if (!currentIterationCRC.equals(previousIterationCRC)) {
                        instantRunBuildContext.close();
                    // instantRunBuildContext.setVerifierResult(InstantRunVerifierStatus
                    // .BINARY_MANIFEST_FILE_CHANGE);
                    }
                }
                // write the new manifest file CRC.
                Files.createParentDirs(crcFile);
                Files.write(currentIterationCRC, crcFile, Charsets.UTF_8);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new GradleException("process res exception", e);
    }
}
Also used : AndroidBuilder(com.android.builder.core.AndroidBuilder) AtlasBuilder(com.android.builder.core.AtlasBuilder) ZipEntry(java.util.zip.ZipEntry) JarFile(java.util.jar.JarFile) AaptPackageConfig(com.android.builder.internal.aapt.AaptPackageConfig) IOException(java.io.IOException) GradleException(org.gradle.api.GradleException) AndroidLibrary(com.android.builder.model.AndroidLibrary) NonNull(com.android.annotations.NonNull) GradleException(org.gradle.api.GradleException) AndroidBuilder(com.android.builder.core.AndroidBuilder) AtlasBuilder(com.android.builder.core.AtlasBuilder) Aapt(com.android.builder.internal.aapt.Aapt) JarFile(java.util.jar.JarFile) File(java.io.File) Nullable(com.android.annotations.Nullable)

Aggregations

AtlasBuilder (com.android.builder.core.AtlasBuilder)3 NonNull (com.android.annotations.NonNull)2 Nullable (com.android.annotations.Nullable)2 AndroidBuilder (com.android.builder.core.AndroidBuilder)2 File (java.io.File)2 IOException (java.io.IOException)2 JarFile (java.util.jar.JarFile)2 ZipEntry (java.util.zip.ZipEntry)2 GradleJavaProcessExecutor (com.android.build.gradle.internal.process.GradleJavaProcessExecutor)1 GradleProcessExecutor (com.android.build.gradle.internal.process.GradleProcessExecutor)1 AaptPackageProcessBuilder (com.android.builder.core.AaptPackageProcessBuilder)1 ErrorReporter (com.android.builder.core.ErrorReporter)1 Aapt (com.android.builder.internal.aapt.Aapt)1 AaptPackageConfig (com.android.builder.internal.aapt.AaptPackageConfig)1 AndroidLibrary (com.android.builder.model.AndroidLibrary)1 LoggedProcessOutputHandler (com.android.ide.common.process.LoggedProcessOutputHandler)1 ProcessException (com.android.ide.common.process.ProcessException)1 ProcessOutputHandler (com.android.ide.common.process.ProcessOutputHandler)1 GradleException (org.gradle.api.GradleException)1 InputFile (org.gradle.api.tasks.InputFile)1