Search in sources :

Example 1 with JpsAndroidDexCompilerConfiguration

use of org.jetbrains.jps.android.model.JpsAndroidDexCompilerConfiguration 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 JpsAndroidDexCompilerConfiguration

use of org.jetbrains.jps.android.model.JpsAndroidDexCompilerConfiguration 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 JpsAndroidDexCompilerConfiguration

use of org.jetbrains.jps.android.model.JpsAndroidDexCompilerConfiguration in project android by JetBrains.

the class AndroidDexBuildTarget method writeConfiguration.

@Override
public void writeConfiguration(ProjectDescriptor pd, PrintWriter out) {
    super.writeConfiguration(pd, out);
    final JpsAndroidDexCompilerConfiguration c = JpsAndroidExtensionService.getInstance().getDexCompilerConfiguration(getModule().getProject());
    if (c != null) {
        out.println(c.getVmOptions());
        out.println(c.getMaxHeapSize());
        out.println(c.isOptimize());
        out.println(c.isForceJumbo());
        out.println(c.isCoreLibrary());
        out.println(c.getProguardVmOptions());
    }
}
Also used : JpsAndroidDexCompilerConfiguration(org.jetbrains.jps.android.model.JpsAndroidDexCompilerConfiguration)

Aggregations

JpsAndroidDexCompilerConfiguration (org.jetbrains.jps.android.model.JpsAndroidDexCompilerConfiguration)3 File (java.io.File)2 AndroidBuildTestingManager (org.jetbrains.android.util.AndroidBuildTestingManager)2 AndroidCompilerMessageKind (org.jetbrains.android.util.AndroidCompilerMessageKind)2 CompilerMessage (org.jetbrains.jps.incremental.messages.CompilerMessage)2 BuildToolInfo (com.android.sdklib.BuildToolInfo)1 FileUtilRt (com.intellij.openapi.util.io.FileUtilRt)1 Processor (com.intellij.util.Processor)1 HashMap (com.intellij.util.containers.HashMap)1 AndroidDxRunner (org.jetbrains.android.compiler.tools.AndroidDxRunner)1 AndroidDexBuildTarget (org.jetbrains.jps.android.builder.AndroidDexBuildTarget)1 JpsAndroidModuleExtension (org.jetbrains.jps.android.model.JpsAndroidModuleExtension)1 BuildRootDescriptor (org.jetbrains.jps.builders.BuildRootDescriptor)1 ProgressMessage (org.jetbrains.jps.incremental.messages.ProgressMessage)1 JpsModule (org.jetbrains.jps.model.module.JpsModule)1