Search in sources :

Example 6 with AndroidCompilerMessageKind

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

the class AndroidDxWrapper method execute.

@SuppressWarnings({ "IOResourceOpenedButNotSafelyClosed" })
public static Map<AndroidCompilerMessageKind, List<String>> execute(@NotNull Module module, @NotNull IAndroidTarget target, @NotNull String outputDir, @NotNull String[] compileTargets, @NotNull String additionalVmParams, int maxHeapSize, boolean optimize) {
    BuildToolInfo buildToolInfo = target.getBuildToolInfo();
    if (buildToolInfo == null) {
        return Collections.singletonMap(AndroidCompilerMessageKind.ERROR, Collections.singletonList("No Build Tools in the Android SDK."));
    }
    String outFile = outputDir + File.separatorChar + AndroidCommonUtils.CLASSES_FILE_NAME;
    final Map<AndroidCompilerMessageKind, List<String>> messages = new HashMap<AndroidCompilerMessageKind, List<String>>(2);
    messages.put(AndroidCompilerMessageKind.ERROR, new ArrayList<String>());
    messages.put(AndroidCompilerMessageKind.INFORMATION, new ArrayList<String>());
    messages.put(AndroidCompilerMessageKind.WARNING, new ArrayList<String>());
    String dxJarPath = buildToolInfo.getPath(BuildToolInfo.PathId.DX_JAR);
    File dxJar = new File(dxJarPath);
    if (!dxJar.isFile()) {
        messages.get(AndroidCompilerMessageKind.ERROR).add(AndroidBundle.message("android.file.not.exist.error", dxJarPath));
        return messages;
    }
    JavaParameters parameters = new JavaParameters();
    Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
    // dex runs after simple java compilation, so JDK must be specified
    assert sdk != null;
    parameters.setJdk(sdk);
    parameters.setMainClass(AndroidDxRunner.class.getName());
    ParametersList programParamList = parameters.getProgramParametersList();
    programParamList.add(dxJarPath);
    programParamList.add(outFile);
    programParamList.add("--optimize", Boolean.toString(optimize));
    programParamList.addAll(compileTargets);
    programParamList.add("--exclude");
    ParametersList vmParamList = parameters.getVMParametersList();
    if (additionalVmParams.length() > 0) {
        vmParamList.addParametersString(additionalVmParams);
    }
    if (!AndroidCommonUtils.hasXmxParam(vmParamList.getParameters())) {
        vmParamList.add("-Xmx" + maxHeapSize + "M");
    }
    final PathsList classPath = parameters.getClassPath();
    classPath.add(PathUtil.getJarPathForClass(AndroidDxRunner.class));
    classPath.add(PathUtil.getJarPathForClass(FileUtilRt.class));
    // delete file to check if it will exist after dex compilation
    if (!new File(outFile).delete()) {
        LOG.info("Cannot delete file " + outFile);
    }
    Process process;
    try {
        parameters.setUseDynamicClasspath(true);
        GeneralCommandLine commandLine = parameters.toCommandLine();
        LOG.info(commandLine.getCommandLineString());
        process = commandLine.createProcess();
        AndroidCommonUtils.handleDexCompilationResult(process, commandLine.getCommandLineString(), outFile, messages, false);
    } catch (ExecutionException e) {
        messages.get(AndroidCompilerMessageKind.ERROR).add("ExecutionException: " + e.getMessage());
        LOG.info(e);
        return messages;
    }
    return messages;
}
Also used : BuildToolInfo(com.android.sdklib.BuildToolInfo) HashMap(com.intellij.util.containers.HashMap) AndroidCompilerMessageKind(org.jetbrains.android.util.AndroidCompilerMessageKind) PathsList(com.intellij.util.PathsList) PathsList(com.intellij.util.PathsList) ArrayList(java.util.ArrayList) List(java.util.List) FileUtilRt(com.intellij.openapi.util.io.FileUtilRt) Sdk(com.intellij.openapi.projectRoots.Sdk) ExecutionException(com.intellij.execution.ExecutionException) File(java.io.File)

Example 7 with AndroidCompilerMessageKind

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

the class AndroidApplicationArtifactProperties method onBuildFinished.

@Override
public void onBuildFinished(@NotNull Artifact artifact, @NotNull CompileContext context) {
    if (!(artifact.getArtifactType() instanceof AndroidApplicationArtifactType)) {
        return;
    }
    if (mySigningMode != AndroidArtifactSigningMode.RELEASE_SIGNED && mySigningMode != AndroidArtifactSigningMode.DEBUG_WITH_CUSTOM_CERTIFICATE) {
        return;
    }
    final AndroidFacet facet = AndroidArtifactUtil.getPackagedFacet(context.getProject(), artifact);
    if (facet == null) {
        return;
    }
    final String artifactName = artifact.getName();
    final String messagePrefix = "[Artifact '" + artifactName + "'] ";
    final Module module = facet.getModule();
    final AndroidPlatform platform = AndroidPlatform.getInstance(module);
    if (platform == null) {
        context.addMessage(CompilerMessageCategory.ERROR, messagePrefix + AndroidBundle.message("android.compilation.error.specify.platform", module.getName()), null, -1, -1);
        return;
    }
    final String sdkLocation = platform.getSdkData().getPath();
    final String artifactFilePath = artifact.getOutputFilePath();
    final String keyStorePath = myKeyStoreUrl != null ? VfsUtilCore.urlToPath(myKeyStoreUrl) : "";
    final String keyStorePassword = myKeyStorePassword != null && myKeyStorePassword.length() > 0 ? getPlainKeystorePassword() : null;
    final String keyPassword = myKeyPassword != null && myKeyPassword.length() > 0 ? getPlainKeyPassword() : null;
    try {
        final Map<AndroidCompilerMessageKind, List<String>> messages = AndroidCommonUtils.buildArtifact(artifactName, messagePrefix, sdkLocation, platform.getTarget(), artifactFilePath, keyStorePath, myKeyAlias, keyStorePassword, keyPassword);
        AndroidCompileUtil.addMessages(context, AndroidCompileUtil.toCompilerMessageCategoryKeys(messages), null);
    } catch (GeneralSecurityException e) {
        AndroidCompileUtil.reportException(context, messagePrefix, e);
    } catch (IOException e) {
        AndroidCompileUtil.reportException(context, messagePrefix, e);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) ArrayList(java.util.ArrayList) List(java.util.List) AndroidCompilerMessageKind(org.jetbrains.android.util.AndroidCompilerMessageKind) IOException(java.io.IOException) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 8 with AndroidCompilerMessageKind

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

the class AndroidApt method compile.

public static Map<AndroidCompilerMessageKind, List<String>> compile(@NotNull IAndroidTarget target, int platformToolsRevision, @NotNull String manifestFileOsPath, @NotNull String aPackage, @NotNull String outDirOsPath, @NotNull String[] resourceDirsOsPaths, @NotNull List<Pair<String, String>> libRTxtFilesAndPackages, boolean nonConstantFields, @Nullable String proguardCfgOutputFileOsPath, @Nullable String rTxtOutDirOsPath, boolean optimizeRFile) throws IOException {
    final Map<AndroidCompilerMessageKind, List<String>> messages = new HashMap<AndroidCompilerMessageKind, List<String>>();
    messages.put(AndroidCompilerMessageKind.ERROR, new ArrayList<String>());
    messages.put(AndroidCompilerMessageKind.INFORMATION, new ArrayList<String>());
    final File outOsDir = new File(outDirOsPath);
    if (!outOsDir.exists()) {
        if (!outOsDir.mkdirs()) {
            messages.get(AndroidCompilerMessageKind.ERROR).add("Unable to create directory " + outDirOsPath);
        }
    }
    final String packageFolderOsPath = FileUtil.toSystemDependentName(outDirOsPath + '/' + aPackage.replace('.', '/'));
    /* We actually need to delete the manifest.java as it may become empty and
    in this case aapt doesn't generate an empty one, but instead doesn't
    touch it */
    final File manifestJavaFile = new File(packageFolderOsPath + File.separatorChar + AndroidCommonUtils.MANIFEST_JAVA_FILE_NAME);
    if (manifestJavaFile.exists()) {
        if (!FileUtil.delete(manifestJavaFile)) {
            messages.get(AndroidCompilerMessageKind.ERROR).add("Unable to delete " + manifestJavaFile.getPath());
        }
    }
    final File rJavaFile = new File(packageFolderOsPath + File.separatorChar + AndroidCommonUtils.R_JAVA_FILENAME);
    if (rJavaFile.exists()) {
        if (!FileUtil.delete(rJavaFile)) {
            messages.get(AndroidCompilerMessageKind.ERROR).add("Unable to delete " + rJavaFile.getPath());
        }
    }
    final File[] extraRJavaFiles = new File[libRTxtFilesAndPackages.size()];
    for (int i = 0, n = libRTxtFilesAndPackages.size(); i < n; i++) {
        final String libPackage = libRTxtFilesAndPackages.get(i).getSecond();
        final String libPackageFolderOsPath = FileUtil.toSystemDependentName(outDirOsPath + '/' + libPackage.replace('.', '/'));
        extraRJavaFiles[i] = new File(libPackageFolderOsPath + File.separatorChar + AndroidCommonUtils.R_JAVA_FILENAME);
    }
    for (File extraRJavaFile : extraRJavaFiles) {
        if (extraRJavaFile.exists()) {
            if (!FileUtil.delete(extraRJavaFile)) {
                messages.get(AndroidCompilerMessageKind.ERROR).add("Unable to delete " + extraRJavaFile.getPath());
            }
        }
    }
    if (platformToolsRevision < 0 || platformToolsRevision > 7) {
        Map<AndroidCompilerMessageKind, List<String>> map = doCompile(target, manifestFileOsPath, outDirOsPath, resourceDirsOsPaths, libRTxtFilesAndPackages, null, nonConstantFields, proguardCfgOutputFileOsPath, rTxtOutDirOsPath, optimizeRFile);
        if (map.get(AndroidCompilerMessageKind.ERROR).isEmpty()) {
            makeFieldsNotFinal(extraRJavaFiles);
        }
        AndroidExecutionUtil.addMessages(messages, map);
        return messages;
    } else {
        messages.get(AndroidCompilerMessageKind.ERROR).add("'Platform Tools' package is out of date. Please update it through Android SDK manager");
        return messages;
    }
}
Also used : HashMap(com.intellij.util.containers.HashMap) AndroidCompilerMessageKind(org.jetbrains.android.util.AndroidCompilerMessageKind) File(java.io.File)

Example 9 with AndroidCompilerMessageKind

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

the class AndroidResourceCachingBuilder method runPngCaching.

private static boolean runPngCaching(AndroidResourceCachingBuildTarget target, CompileContext context, BuildOutputConsumer outputConsumer) throws IOException {
    final JpsModule module = target.getModule();
    final JpsAndroidModuleExtension extension = AndroidJpsUtil.getExtension(module);
    assert extension != null;
    context.processMessage(new CompilerMessage(BUILDER_NAME, BuildMessage.Kind.INFO, AndroidJpsBundle.message("android.jps.progress.res.caching", module.getName())));
    final AndroidPlatform platform = AndroidJpsUtil.getAndroidPlatform(module, context, BUILDER_NAME);
    if (platform == null) {
        return false;
    }
    final File resCacheDir = target.getOutputDir(context);
    // todo: probably it may be done automatically
    if (context.getScope().isBuildForced(target) && resCacheDir.exists()) {
        if (!FileUtil.delete(resCacheDir)) {
            context.processMessage(new CompilerMessage(BUILDER_NAME, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.cannot.create.directory", resCacheDir.getPath())));
            return false;
        }
    }
    if (!resCacheDir.exists()) {
        if (!resCacheDir.mkdirs()) {
            context.processMessage(new CompilerMessage(BUILDER_NAME, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.cannot.create.directory", resCacheDir.getPath())));
            return false;
        }
    }
    final IAndroidTarget androidTarget = platform.getTarget();
    final List<BuildRootDescriptor> roots = context.getProjectDescriptor().getBuildRootIndex().getTargetRoots(target, context);
    if (roots.size() == 0) {
        return true;
    }
    final List<String> inputDirs = new ArrayList<String>();
    for (BuildRootDescriptor root : roots) {
        final File f = root.getRootFile();
        if (f.exists()) {
            inputDirs.add(f.getPath());
        }
    }
    final Map<AndroidCompilerMessageKind, List<String>> messages = AndroidApt.crunch(androidTarget, inputDirs, resCacheDir.getPath());
    AndroidJpsUtil.addMessages(context, messages, BUILDER_NAME, module.getName());
    final boolean success = messages.get(AndroidCompilerMessageKind.ERROR).isEmpty();
    if (success) {
        final Map<String, File> outputFiles = new HashMap<String, File>();
        FileUtil.processFilesRecursively(resCacheDir, new Processor<File>() {

            @Override
            public boolean process(File file) {
                if (file.isFile()) {
                    final String relativePath = FileUtil.getRelativePath(resCacheDir, file);
                    if (relativePath != null) {
                        outputFiles.put(relativePath, file);
                    }
                }
                return true;
            }
        });
        for (Map.Entry<String, File> entry : outputFiles.entrySet()) {
            final String relativePath = entry.getKey();
            final File outputFile = entry.getValue();
            for (String inputDir : inputDirs) {
                final File srcFile = new File(inputDir, relativePath);
                outputConsumer.registerOutputFile(outputFile, Collections.singletonList(srcFile.getPath()));
            }
        }
    }
    return success;
}
Also used : BuildRootDescriptor(org.jetbrains.jps.builders.BuildRootDescriptor) CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) HashMap(com.intellij.util.containers.HashMap) ArrayList(java.util.ArrayList) IAndroidTarget(com.android.sdklib.IAndroidTarget) AndroidCompilerMessageKind(org.jetbrains.android.util.AndroidCompilerMessageKind) JpsModule(org.jetbrains.jps.model.module.JpsModule) JpsAndroidModuleExtension(org.jetbrains.jps.android.model.JpsAndroidModuleExtension) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) HashMap(com.intellij.util.containers.HashMap) Map(java.util.Map)

Example 10 with AndroidCompilerMessageKind

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

the class AndroidResourcePackagingBuilder method doPackageResources.

private static boolean doPackageResources(@NotNull final CompileContext context, @NotNull File manifestFile, @NotNull IAndroidTarget target, @NotNull String[] resourceDirPaths, @NotNull String[] assetsDirPaths, @NotNull String outputPath, boolean releasePackage, @NotNull String moduleName, @NotNull BuildOutputConsumer outputConsumer, @Nullable String customManifestPackage, @Nullable String additionalParameters) {
    try {
        final IgnoredFileIndex ignoredFileIndex = context.getProjectDescriptor().getIgnoredFileIndex();
        final Map<AndroidCompilerMessageKind, List<String>> messages = AndroidApt.packageResources(target, -1, manifestFile.getPath(), resourceDirPaths, assetsDirPaths, outputPath, null, !releasePackage, 0, customManifestPackage, additionalParameters, new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                return !ignoredFileIndex.isIgnored(PathUtilRt.getFileName(pathname.getPath()));
            }
        });
        AndroidJpsUtil.addMessages(context, messages, BUILDER_NAME, moduleName);
        final boolean success = messages.get(AndroidCompilerMessageKind.ERROR).size() == 0;
        if (success) {
            final List<String> srcFiles = new ArrayList<String>();
            srcFiles.add(manifestFile.getPath());
            fillRecursively(resourceDirPaths, srcFiles);
            fillRecursively(assetsDirPaths, srcFiles);
            outputConsumer.registerOutputFile(new File(outputPath), srcFiles);
        }
        return success;
    } catch (final IOException e) {
        AndroidJpsUtil.reportExceptionError(context, null, e, BUILDER_NAME);
        return false;
    }
}
Also used : IgnoredFileIndex(org.jetbrains.jps.indices.IgnoredFileIndex) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) AndroidCompilerMessageKind(org.jetbrains.android.util.AndroidCompilerMessageKind) IOException(java.io.IOException) FileFilter(java.io.FileFilter) File(java.io.File)

Aggregations

AndroidCompilerMessageKind (org.jetbrains.android.util.AndroidCompilerMessageKind)12 File (java.io.File)10 HashMap (com.intellij.util.containers.HashMap)7 CompilerMessage (org.jetbrains.jps.incremental.messages.CompilerMessage)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 BuildToolInfo (com.android.sdklib.BuildToolInfo)3 HashSet (com.intellij.util.containers.HashSet)3 IOException (java.io.IOException)3 JpsAndroidModuleExtension (org.jetbrains.jps.android.model.JpsAndroidModuleExtension)3 FileUtilRt (com.intellij.openapi.util.io.FileUtilRt)2 AndroidBuildTestingManager (org.jetbrains.android.util.AndroidBuildTestingManager)2 JpsAndroidDexCompilerConfiguration (org.jetbrains.jps.android.model.JpsAndroidDexCompilerConfiguration)2 BuildRootDescriptor (org.jetbrains.jps.builders.BuildRootDescriptor)2 ProgressMessage (org.jetbrains.jps.incremental.messages.ProgressMessage)2 JpsModule (org.jetbrains.jps.model.module.JpsModule)2 DebugKeyProvider (com.android.jarutils.DebugKeyProvider)1 SignedJarBuilder (com.android.jarutils.SignedJarBuilder)1 AndroidLocation (com.android.prefs.AndroidLocation)1 IAndroidTarget (com.android.sdklib.IAndroidTarget)1