Search in sources :

Example 1 with AndroidBuildTestingManager

use of org.jetbrains.android.util.AndroidBuildTestingManager in project android by JetBrains.

the class AndroidDexBuilder method runDex.

public static boolean runDex(@NotNull AndroidPlatform platform, @NotNull String outFilePath, @NotNull String[] compileTargets, @NotNull CompileContext context, @NotNull JpsProject project, @NotNull BuildOutputConsumer outputConsumer, @NotNull String builderName, @NotNull String srcTargetName, @Nullable JpsModule module) throws IOException {
    BuildToolInfo buildToolInfo = platform.getTarget().getBuildToolInfo();
    if (buildToolInfo == null) {
        return false;
    }
    final String dxJarPath = FileUtil.toSystemDependentName(buildToolInfo.getPath(BuildToolInfo.PathId.DX_JAR));
    final AndroidBuildTestingManager testingManager = AndroidBuildTestingManager.getTestingManager();
    final File dxJar = new File(dxJarPath);
    if (testingManager == null && !dxJar.isFile()) {
        context.processMessage(new CompilerMessage(builderName, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.cannot.find.file", dxJarPath)));
        return false;
    }
    boolean multiDex = false;
    if (module != null) {
        JpsAndroidModuleExtension extension = AndroidJpsUtil.getExtension(module);
        if (extension != null && extension.isMultiDexEnabled()) {
            outFilePath = new File(outFilePath).getParent();
            multiDex = true;
        }
    }
    final List<String> programParamList = new ArrayList<String>();
    programParamList.add(dxJarPath);
    programParamList.add(outFilePath);
    final JpsAndroidDexCompilerConfiguration configuration = JpsAndroidExtensionService.getInstance().getDexCompilerConfiguration(project);
    final List<String> vmOptions;
    if (configuration != null) {
        vmOptions = new ArrayList<String>();
        vmOptions.addAll(ParametersListUtil.parse(configuration.getVmOptions()));
        if (!AndroidCommonUtils.hasXmxParam(vmOptions)) {
            vmOptions.add("-Xmx" + configuration.getMaxHeapSize() + "M");
        }
        programParamList.addAll(Arrays.asList("--optimize", Boolean.toString(configuration.isOptimize())));
        if (configuration.isForceJumbo()) {
            programParamList.addAll(Arrays.asList("--forceJumbo", Boolean.TRUE.toString()));
        }
        if (configuration.isCoreLibrary()) {
            programParamList.add("--coreLibrary");
        }
    } else {
        vmOptions = Collections.singletonList("-Xmx1024M");
    }
    if (multiDex) {
        JpsAndroidModuleExtension extension = AndroidJpsUtil.getExtension(module);
        if (extension != null) {
            programParamList.add("--multi-dex");
            if (!StringUtil.isEmpty(extension.getMainDexList())) {
                programParamList.add("--main-dex-list");
                programParamList.add(extension.getMainDexList());
            }
            if (extension.isMinimalMainDex()) {
                programParamList.add("--minimal-main-dex");
            }
        }
    }
    programParamList.addAll(Arrays.asList(compileTargets));
    programParamList.add("--exclude");
    final List<String> classPath = new ArrayList<String>();
    classPath.add(ClasspathBootstrap.getResourcePath(AndroidDxRunner.class));
    classPath.add(ClasspathBootstrap.getResourcePath(FileUtilRt.class));
    final File outFile = new File(outFilePath);
    if (outFile.exists() && !outFile.isDirectory() && !outFile.delete()) {
        context.processMessage(new CompilerMessage(builderName, BuildMessage.Kind.WARNING, AndroidJpsBundle.message("android.jps.cannot.delete.file", outFilePath)));
    }
    final String javaExecutable = getJavaExecutable(platform, context, builderName);
    if (javaExecutable == null) {
        return false;
    }
    final List<String> commandLine = ExternalProcessUtil.buildJavaCommandLine(javaExecutable, AndroidDxRunner.class.getName(), Collections.<String>emptyList(), classPath, vmOptions, programParamList);
    LOG.info(AndroidCommonUtils.command2string(commandLine));
    final String[] commands = ArrayUtil.toStringArray(commandLine);
    final Process process;
    if (testingManager != null) {
        process = testingManager.getCommandExecutor().createProcess(commands, Collections.<String, String>emptyMap());
    } else {
        process = Runtime.getRuntime().exec(commands);
    }
    final HashMap<AndroidCompilerMessageKind, List<String>> messages = new HashMap<AndroidCompilerMessageKind, List<String>>(3);
    messages.put(AndroidCompilerMessageKind.ERROR, new ArrayList<String>());
    messages.put(AndroidCompilerMessageKind.WARNING, new ArrayList<String>());
    messages.put(AndroidCompilerMessageKind.INFORMATION, new ArrayList<String>());
    AndroidCommonUtils.handleDexCompilationResult(process, StringUtil.join(commandLine, " "), outFilePath, messages, multiDex);
    AndroidJpsUtil.addMessages(context, messages, builderName, srcTargetName);
    final boolean success = messages.get(AndroidCompilerMessageKind.ERROR).size() == 0;
    if (success) {
        final List<String> srcFiles = new ArrayList<String>();
        for (String compileTargetPath : compileTargets) {
            final File compileTarget = new File(compileTargetPath);
            if (compileTarget.isFile()) {
                srcFiles.add(compileTargetPath);
            } else if (compileTarget.isDirectory()) {
                AndroidJpsUtil.processClassFilesAndJarsRecursively(compileTarget, new Processor<File>() {

                    @Override
                    public boolean process(File file) {
                        if (file.isFile()) {
                            srcFiles.add(file.getPath());
                        }
                        return true;
                    }
                });
            }
        }
        outputConsumer.registerOutputFile(outFile, srcFiles);
    }
    return success;
}
Also used : AndroidBuildTestingManager(org.jetbrains.android.util.AndroidBuildTestingManager) CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) Processor(com.intellij.util.Processor) BuildToolInfo(com.android.sdklib.BuildToolInfo) HashMap(com.intellij.util.containers.HashMap) AndroidCompilerMessageKind(org.jetbrains.android.util.AndroidCompilerMessageKind) AndroidDxRunner(org.jetbrains.android.compiler.tools.AndroidDxRunner) JpsAndroidDexCompilerConfiguration(org.jetbrains.jps.android.model.JpsAndroidDexCompilerConfiguration) JpsAndroidModuleExtension(org.jetbrains.jps.android.model.JpsAndroidModuleExtension) FileUtilRt(com.intellij.openapi.util.io.FileUtilRt) File(java.io.File)

Example 2 with AndroidBuildTestingManager

use of org.jetbrains.android.util.AndroidBuildTestingManager in project android by JetBrains.

the class AndroidDexBuilder method runProguardIfNecessary.

private static Pair<Boolean, AndroidProGuardStateStorage.MyState> runProguardIfNecessary(@NotNull JpsAndroidModuleExtension extension, @NotNull AndroidDexBuildTarget target, @NotNull AndroidPlatform platform, @NotNull CompileContext context, @NotNull String outputJarPath, @NotNull String[] proguardCfgPaths, boolean hasDirtyFiles, @Nullable AndroidProGuardStateStorage.MyState oldState) throws IOException {
    final JpsModule module = extension.getModule();
    final File[] proguardCfgFiles = new File[proguardCfgPaths.length];
    for (int i = 0; i < proguardCfgFiles.length; i++) {
        proguardCfgFiles[i] = new File(proguardCfgPaths[i]);
        if (!proguardCfgFiles[i].exists()) {
            context.processMessage(new CompilerMessage(PRO_GUARD_BUILDER_NAME, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.cannot.find.file", proguardCfgPaths[i])));
            return null;
        }
    }
    final File mainContentRoot = AndroidJpsUtil.getMainContentRoot(extension);
    if (mainContentRoot == null) {
        context.processMessage(new CompilerMessage(PRO_GUARD_BUILDER_NAME, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.errors.main.content.root.not.found", module.getName())));
        return null;
    }
    final String javaExecutable = getJavaExecutable(platform, context, PRO_GUARD_BUILDER_NAME);
    if (javaExecutable == null) {
        return null;
    }
    final File proguardLogsDir = extension.getProguardLogsDir();
    final File logsDir;
    if (proguardLogsDir != null) {
        logsDir = proguardLogsDir;
    } else {
        logsDir = new File(mainContentRoot.getPath() + '/' + AndroidCommonUtils.DIRECTORY_FOR_LOGS_NAME);
    }
    final AndroidProGuardStateStorage.MyState newState = new AndroidProGuardStateStorage.MyState(proguardCfgFiles);
    if (!hasDirtyFiles && newState.equals(oldState)) {
        return Pair.create(false, null);
    }
    final List<String> classesDirs = new ArrayList<String>();
    final List<String> libClassesDirs = new ArrayList<String>();
    final List<String> externalJars = new ArrayList<String>();
    final List<String> providedJars = new ArrayList<String>();
    final List<BuildRootDescriptor> roots = context.getProjectDescriptor().getBuildRootIndex().getTargetRoots(target, context);
    for (BuildRootDescriptor root : roots) {
        final File rootFile = root.getRootFile();
        if (!rootFile.exists()) {
            continue;
        }
        if (root instanceof AndroidDexBuildTarget.MyClassesDirBuildRootDescriptor) {
            final AndroidDexBuildTarget.ClassesDirType type = ((AndroidDexBuildTarget.MyClassesDirBuildRootDescriptor) root).getClassesDirType();
            if (type == AndroidDexBuildTarget.ClassesDirType.JAVA || type == AndroidDexBuildTarget.ClassesDirType.ANDROID_APP) {
                AndroidJpsUtil.addSubdirectories(rootFile, classesDirs);
            } else {
                AndroidJpsUtil.addSubdirectories(rootFile, libClassesDirs);
            }
        } else if (root instanceof AndroidDexBuildTarget.MyJarBuildRootDescriptor) {
            final AndroidDexBuildTarget.MyJarBuildRootDescriptor jarRoot = (AndroidDexBuildTarget.MyJarBuildRootDescriptor) root;
            if (!jarRoot.isLibPackage() && !jarRoot.isPreDexed()) {
                externalJars.add(rootFile.getPath());
            }
        } else if (root instanceof AndroidDexBuildTarget.MyProvidedJarBuildRootDescriptor) {
            providedJars.add(rootFile.getPath());
        }
    }
    final String[] classFilesDirOsPaths = ArrayUtil.toStringArray(classesDirs);
    final String[] libClassFilesDirOsPaths = ArrayUtil.toStringArray(libClassesDirs);
    final String[] externalJarOsPaths = ArrayUtil.toStringArray(externalJars);
    final String[] providedJarOsPaths = ArrayUtil.toStringArray(providedJars);
    final String inputJarOsPath = AndroidCommonUtils.buildTempInputJar(classFilesDirOsPaths, libClassFilesDirOsPaths);
    final AndroidBuildTestingManager testingManager = AndroidBuildTestingManager.getTestingManager();
    if (testingManager != null) {
        testingManager.getCommandExecutor().checkJarContent("proguard_input_jar", inputJarOsPath);
    }
    if (!logsDir.exists()) {
        if (!logsDir.mkdirs()) {
            context.processMessage(new CompilerMessage(PRO_GUARD_BUILDER_NAME, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.cannot.create.directory", FileUtil.toSystemDependentName(logsDir.getPath()))));
            return null;
        }
    }
    final JpsAndroidDexCompilerConfiguration configuration = JpsAndroidExtensionService.getInstance().getDexCompilerConfiguration(module.getProject());
    String proguardVmOptions = configuration != null ? configuration.getProguardVmOptions() : null;
    if (proguardVmOptions == null) {
        proguardVmOptions = "";
    }
    context.processMessage(new ProgressMessage(AndroidJpsBundle.message("android.jps.progress.proguard", module.getName())));
    final Map<AndroidCompilerMessageKind, List<String>> messages = AndroidCommonUtils.launchProguard(platform.getTarget(), platform.getSdkToolsRevision(), platform.getSdk().getHomePath(), javaExecutable, proguardVmOptions, proguardCfgPaths, inputJarOsPath, externalJarOsPaths, providedJarOsPaths, outputJarPath, logsDir.getPath());
    AndroidJpsUtil.addMessages(context, messages, PRO_GUARD_BUILDER_NAME, module.getName());
    return messages.get(AndroidCompilerMessageKind.ERROR).isEmpty() ? Pair.create(true, newState) : null;
}
Also used : ProgressMessage(org.jetbrains.jps.incremental.messages.ProgressMessage) CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) AndroidCompilerMessageKind(org.jetbrains.android.util.AndroidCompilerMessageKind) JpsAndroidDexCompilerConfiguration(org.jetbrains.jps.android.model.JpsAndroidDexCompilerConfiguration) AndroidDexBuildTarget(org.jetbrains.jps.android.builder.AndroidDexBuildTarget) BuildRootDescriptor(org.jetbrains.jps.builders.BuildRootDescriptor) AndroidBuildTestingManager(org.jetbrains.android.util.AndroidBuildTestingManager) JpsModule(org.jetbrains.jps.model.module.JpsModule) File(java.io.File)

Example 3 with AndroidBuildTestingManager

use of org.jetbrains.android.util.AndroidBuildTestingManager in project android by JetBrains.

the class AndroidLibraryPackagingBuilder method doBuild.

private static boolean doBuild(CompileContext context, JpsModule module, BuildOutputConsumer outputConsumer) throws IOException {
    final JpsAndroidModuleExtension extension = AndroidJpsUtil.getExtension(module);
    if (extension == null || !extension.isLibrary()) {
        return true;
    }
    File outputDir = AndroidJpsUtil.getDirectoryForIntermediateArtifacts(context, module);
    outputDir = AndroidJpsUtil.createDirIfNotExist(outputDir, context, BUILDER_NAME);
    if (outputDir == null) {
        return false;
    }
    final File classesDir = ProjectPaths.getModuleOutputDir(module, false);
    if (classesDir == null || !classesDir.isDirectory()) {
        return true;
    }
    final Set<String> subdirs = new HashSet<String>();
    AndroidJpsUtil.addSubdirectories(classesDir, subdirs);
    if (subdirs.size() > 0) {
        context.processMessage(new ProgressMessage(AndroidJpsBundle.message("android.jps.progress.library.packaging", module.getName())));
        final File outputJarFile = new File(outputDir, AndroidCommonUtils.CLASSES_JAR_FILE_NAME);
        final List<String> srcFiles;
        try {
            srcFiles = AndroidCommonUtils.packClassFilesIntoJar(ArrayUtil.EMPTY_STRING_ARRAY, ArrayUtil.toStringArray(subdirs), outputJarFile);
        } catch (IOException e) {
            AndroidJpsUtil.reportExceptionError(context, null, e, BUILDER_NAME);
            return false;
        }
        final AndroidBuildTestingManager testingManager = AndroidBuildTestingManager.getTestingManager();
        if (testingManager != null && outputJarFile.isFile()) {
            testingManager.getCommandExecutor().checkJarContent("library_package_jar", outputJarFile.getPath());
        }
        if (srcFiles.size() > 0) {
            outputConsumer.registerOutputFile(outputJarFile, srcFiles);
        }
    }
    return true;
}
Also used : ProgressMessage(org.jetbrains.jps.incremental.messages.ProgressMessage) AndroidBuildTestingManager(org.jetbrains.android.util.AndroidBuildTestingManager) JpsAndroidModuleExtension(org.jetbrains.jps.android.model.JpsAndroidModuleExtension) IOException(java.io.IOException) File(java.io.File) HashSet(com.intellij.util.containers.HashSet)

Example 4 with AndroidBuildTestingManager

use of org.jetbrains.android.util.AndroidBuildTestingManager in project android by JetBrains.

the class AndroidAarDepsBuilder method doBuild.

private static boolean doBuild(final CompileContext context, AndroidAarDepsBuildTarget target, BuildOutputConsumer outputConsumer) {
    final JpsModule module = target.getModule();
    final JpsAndroidModuleExtension extension = AndroidJpsUtil.getExtension(module);
    if (extension == null || extension.isLibrary()) {
        return true;
    }
    File outputDir = AndroidJpsUtil.getDirectoryForIntermediateArtifacts(context, module);
    outputDir = AndroidJpsUtil.createDirIfNotExist(outputDir, context, BUILDER_NAME);
    if (outputDir == null) {
        return false;
    }
    final List<String> srcJarFiles = new ArrayList<String>();
    for (BuildRootDescriptor descriptor : context.getProjectDescriptor().getBuildRootIndex().getTargetRoots(target, context)) {
        final File file = descriptor.getRootFile();
        if (file.exists()) {
            srcJarFiles.add(file.getPath());
        }
    }
    if (srcJarFiles.size() == 0) {
        return true;
    }
    context.processMessage(new ProgressMessage(AndroidJpsBundle.message("android.jps.progress.aar.dependencies.packaging", module.getName())));
    File tempDir = null;
    try {
        tempDir = FileUtil.createTempDirectory("extracted_aar_deps", "tmp");
        for (int i = srcJarFiles.size() - 1; i >= 0; i--) {
            ZipUtil.extract(new File(srcJarFiles.get(i)), tempDir, null, true);
        }
        final File outputJarFile = new File(outputDir, AndroidCommonUtils.AAR_DEPS_JAR_FILE_NAME);
        if (!packDirectoryIntoJar(tempDir, outputJarFile, context)) {
            return false;
        }
        final AndroidBuildTestingManager testingManager = AndroidBuildTestingManager.getTestingManager();
        if (testingManager != null && outputJarFile.isFile()) {
            testingManager.getCommandExecutor().checkJarContent("aar_dependencies_package_jar", outputJarFile.getPath());
        }
        outputConsumer.registerOutputFile(outputJarFile, srcJarFiles);
        return true;
    } catch (IOException e) {
        AndroidJpsUtil.reportExceptionError(context, null, e, BUILDER_NAME);
        return false;
    } finally {
        if (tempDir != null) {
            FileUtil.delete(tempDir);
        }
    }
}
Also used : JpsModule(org.jetbrains.jps.model.module.JpsModule) BuildRootDescriptor(org.jetbrains.jps.builders.BuildRootDescriptor) ProgressMessage(org.jetbrains.jps.incremental.messages.ProgressMessage) AndroidBuildTestingManager(org.jetbrains.android.util.AndroidBuildTestingManager) JpsAndroidModuleExtension(org.jetbrains.jps.android.model.JpsAndroidModuleExtension) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File)

Example 5 with AndroidBuildTestingManager

use of org.jetbrains.android.util.AndroidBuildTestingManager in project android by JetBrains.

the class AndroidManifestMergingBuilder method doMergeManifests.

private static boolean doMergeManifests(final CompileContext context, File manifestFile, List<File> libManifests, File outputFile) throws IOException {
    final AndroidBuildTestingManager testingManager = AndroidBuildTestingManager.getTestingManager();
    if (testingManager != null) {
        final StringBuilder messageBuilder = new StringBuilder("manifest_merging\n");
        messageBuilder.append(manifestFile.getPath()).append('\n');
        Collections.sort(libManifests);
        for (File libManifest : libManifests) {
            messageBuilder.append(libManifest.getPath()).append('\n');
        }
        messageBuilder.append(outputFile.getPath());
        testingManager.getCommandExecutor().log(messageBuilder.toString());
    }
    ImmutableList.Builder<Pair<String, File>> libraryFiles = ImmutableList.builder();
    for (File f : libManifests) {
        libraryFiles.add(Pair.of(f.getName(), f));
    }
    final ManifestMerger2.Invoker manifestMergerInvoker = ManifestMerger2.newMerger(manifestFile, NullLogger.getLogger(), ManifestMerger2.MergeType.APPLICATION).addBundleManifests(libraryFiles.build());
    MergingReport mergingReport;
    try {
        mergingReport = manifestMergerInvoker.merge();
    } catch (ManifestMerger2.MergeFailureException e) {
        context.processMessage(new CompilerMessage(BUILDER_NAME, BuildMessage.Kind.ERROR, e.getMessage()));
        return false;
    }
    MergingReport.Result result = mergingReport.getResult();
    for (MergingReport.Record record : mergingReport.getLoggingRecords()) {
        SourceFilePosition position = record.getSourceLocation();
        File sourceFile = position.getFile().getSourceFile();
        String sourceFilePath = sourceFile != null ? sourceFile.getAbsolutePath() : null;
        SourcePosition pos = position.getPosition();
        MergingReport.Record.Severity severity = record.getSeverity();
        if (severity != MergingReport.Record.Severity.INFO) {
            context.processMessage(new CompilerMessage(BUILDER_NAME, toBuildMessageKind(record.getSeverity()), record.getMessage(), sourceFilePath, pos.getStartOffset(), pos.getEndOffset(), pos.getEndOffset(), pos.getEndLine(), pos.getEndColumn()));
        }
    }
    if (!result.isError()) {
        String xmlDocument = mergingReport.getMergedDocument(MergingReport.MergedManifestKind.MERGED);
        Files.write(xmlDocument, outputFile, Charsets.UTF_8);
    }
    return result.isSuccess();
}
Also used : MergingReport(com.android.manifmerger.MergingReport) AndroidBuildTestingManager(org.jetbrains.android.util.AndroidBuildTestingManager) CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) ImmutableList(com.google.common.collect.ImmutableList) ManifestMerger2(com.android.manifmerger.ManifestMerger2) SourceFilePosition(com.android.ide.common.blame.SourceFilePosition) SourcePosition(com.android.ide.common.blame.SourcePosition) File(java.io.File) Pair(com.android.utils.Pair)

Aggregations

File (java.io.File)5 AndroidBuildTestingManager (org.jetbrains.android.util.AndroidBuildTestingManager)5 JpsAndroidModuleExtension (org.jetbrains.jps.android.model.JpsAndroidModuleExtension)3 CompilerMessage (org.jetbrains.jps.incremental.messages.CompilerMessage)3 ProgressMessage (org.jetbrains.jps.incremental.messages.ProgressMessage)3 IOException (java.io.IOException)2 AndroidCompilerMessageKind (org.jetbrains.android.util.AndroidCompilerMessageKind)2 JpsAndroidDexCompilerConfiguration (org.jetbrains.jps.android.model.JpsAndroidDexCompilerConfiguration)2 BuildRootDescriptor (org.jetbrains.jps.builders.BuildRootDescriptor)2 JpsModule (org.jetbrains.jps.model.module.JpsModule)2 SourceFilePosition (com.android.ide.common.blame.SourceFilePosition)1 SourcePosition (com.android.ide.common.blame.SourcePosition)1 ManifestMerger2 (com.android.manifmerger.ManifestMerger2)1 MergingReport (com.android.manifmerger.MergingReport)1 BuildToolInfo (com.android.sdklib.BuildToolInfo)1 Pair (com.android.utils.Pair)1 ImmutableList (com.google.common.collect.ImmutableList)1 FileUtilRt (com.intellij.openapi.util.io.FileUtilRt)1 Processor (com.intellij.util.Processor)1 HashMap (com.intellij.util.containers.HashMap)1