Search in sources :

Example 6 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class AndroidSourceGeneratingBuilder method runAidlCompiler.

private static boolean runAidlCompiler(@NotNull final CompileContext context, @NotNull Map<File, ModuleBuildTarget> files, @NotNull Map<JpsModule, MyModuleData> moduleDataMap) {
    if (files.size() > 0) {
        context.processMessage(new ProgressMessage(AndroidJpsBundle.message("android.jps.progress.aidl")));
    }
    boolean success = true;
    for (Map.Entry<File, ModuleBuildTarget> entry : files.entrySet()) {
        final File file = entry.getKey();
        final ModuleBuildTarget buildTarget = entry.getValue();
        final String filePath = file.getPath();
        final MyModuleData moduleData = moduleDataMap.get(buildTarget.getModule());
        if (!LOG.assertTrue(moduleData != null)) {
            context.processMessage(new CompilerMessage(ANDROID_IDL_COMPILER, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.internal.error")));
            success = false;
            continue;
        }
        final File generatedSourcesDir = AndroidJpsUtil.getGeneratedSourcesStorage(buildTarget.getModule(), context.getProjectDescriptor().dataManager);
        final File aidlOutputDirectory = new File(generatedSourcesDir, AndroidJpsUtil.AIDL_GENERATED_SOURCE_ROOT_NAME);
        if (!aidlOutputDirectory.exists() && !aidlOutputDirectory.mkdirs()) {
            context.processMessage(new CompilerMessage(ANDROID_IDL_COMPILER, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.cannot.create.directory", aidlOutputDirectory.getPath())));
            success = false;
            continue;
        }
        final IAndroidTarget target = moduleData.getPlatform().getTarget();
        try {
            final File[] sourceRoots = AndroidJpsUtil.getSourceRootsForModuleAndDependencies(buildTarget.getModule());
            final String[] sourceRootPaths = AndroidJpsUtil.toPaths(sourceRoots);
            final String packageName = computePackageForFile(context, file);
            if (packageName == null) {
                context.processMessage(new CompilerMessage(ANDROID_IDL_COMPILER, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.errors.cannot.compute.package", filePath)));
                success = false;
                continue;
            }
            final File outputFile = new File(aidlOutputDirectory, packageName.replace('.', File.separatorChar) + File.separator + FileUtil.getNameWithoutExtension(file) + ".java");
            final String outputFilePath = outputFile.getPath();
            final Map<AndroidCompilerMessageKind, List<String>> messages = AndroidIdl.execute(target, filePath, outputFilePath, sourceRootPaths);
            addMessages(context, messages, filePath, ANDROID_IDL_COMPILER);
            if (messages.get(AndroidCompilerMessageKind.ERROR).size() > 0) {
                success = false;
            } else if (outputFile.exists()) {
                final SourceToOutputMapping sourceToOutputMap = context.getProjectDescriptor().dataManager.getSourceToOutputMap(buildTarget);
                sourceToOutputMap.setOutput(filePath, outputFilePath);
                FSOperations.markDirty(context, CompilationRound.CURRENT, outputFile);
            }
        } catch (final IOException e) {
            AndroidJpsUtil.reportExceptionError(context, filePath, e, ANDROID_IDL_COMPILER);
            success = false;
        }
    }
    return success;
}
Also used : ProgressMessage(org.jetbrains.jps.incremental.messages.ProgressMessage) SourceToOutputMapping(org.jetbrains.jps.builders.storage.SourceToOutputMapping) CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) IAndroidTarget(com.android.sdklib.IAndroidTarget) HashMap(com.intellij.util.containers.HashMap) TObjectLongHashMap(gnu.trove.TObjectLongHashMap)

Example 7 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class AndroidSourceGeneratingBuilder method runAaptCompiler.

private static MyExitStatus runAaptCompiler(@NotNull final CompileContext context, @NotNull Map<JpsModule, MyModuleData> moduleDataMap) throws IOException {
    boolean success = true;
    boolean didSomething = false;
    for (Map.Entry<JpsModule, MyModuleData> entry : moduleDataMap.entrySet()) {
        final JpsModule module = entry.getKey();
        final ModuleBuildTarget moduleTarget = new ModuleBuildTarget(module, JavaModuleBuildTargetType.PRODUCTION);
        final AndroidAptStateStorage storage = context.getProjectDescriptor().dataManager.getStorage(moduleTarget, AndroidAptStateStorage.PROVIDER);
        final MyModuleData moduleData = entry.getValue();
        final JpsAndroidModuleExtension extension = moduleData.getAndroidExtension();
        final File generatedSourcesDir = AndroidJpsUtil.getGeneratedSourcesStorage(module, context.getProjectDescriptor().dataManager);
        final File aptOutputDirectory = new File(generatedSourcesDir, AndroidJpsUtil.AAPT_GENERATED_SOURCE_ROOT_NAME);
        final IAndroidTarget target = moduleData.getPlatform().getTarget();
        try {
            final String[] resPaths = AndroidJpsUtil.collectResourceDirsForCompilation(extension, false, context, true);
            if (resPaths.length == 0) {
                // there is no resources in the module
                if (!clearDirectoryIfNotEmpty(aptOutputDirectory, context, ANDROID_APT_COMPILER)) {
                    success = false;
                }
                continue;
            }
            final String packageName = moduleData.getPackage();
            final File manifestFile;
            if (extension.isLibrary() || !extension.isManifestMergingEnabled()) {
                manifestFile = moduleData.getManifestFileForCompiler();
            } else {
                manifestFile = new File(AndroidJpsUtil.getPreprocessedManifestDirectory(module, context.getProjectDescriptor().dataManager.getDataPaths()), SdkConstants.FN_ANDROID_MANIFEST_XML);
            }
            if (isLibraryWithBadCircularDependency(extension)) {
                if (!clearDirectoryIfNotEmpty(aptOutputDirectory, context, ANDROID_APT_COMPILER)) {
                    success = false;
                }
                continue;
            }
            final Map<JpsModule, String> packageMap = getDepLibPackages(module);
            packageMap.put(module, packageName);
            final JpsModule circularDepLibWithSamePackage = findCircularDependencyOnLibraryWithSamePackage(extension, packageMap);
            if (circularDepLibWithSamePackage != null && !extension.isLibrary()) {
                final String message = "Generated fields in " + packageName + ".R class in module '" + module.getName() + "' won't be final, because of circular dependency on module '" + circularDepLibWithSamePackage.getName() + "'";
                context.processMessage(new CompilerMessage(ANDROID_APT_COMPILER, BuildMessage.Kind.WARNING, message));
            }
            final boolean generateNonFinalFields = extension.isLibrary() || circularDepLibWithSamePackage != null;
            AndroidAptValidityState oldState;
            try {
                oldState = storage.getState(module.getName());
            } catch (IOException e) {
                LOG.info(e);
                oldState = null;
            }
            final Map<String, ResourceFileData> resources = new HashMap<String, ResourceFileData>();
            final TObjectLongHashMap<String> valueResFilesTimestamps = new TObjectLongHashMap<String>();
            collectResources(resPaths, resources, valueResFilesTimestamps, oldState);
            final List<ResourceEntry> manifestElements = collectManifestElements(manifestFile);
            final List<Pair<String, String>> libRTextFilesAndPackages = new ArrayList<Pair<String, String>>(packageMap.size());
            for (Map.Entry<JpsModule, String> entry1 : packageMap.entrySet()) {
                final String libPackage = entry1.getValue();
                if (!packageName.equals(libPackage)) {
                    final String libRTxtFilePath = new File(new File(AndroidJpsUtil.getDirectoryForIntermediateArtifacts(context, entry1.getKey()), R_TXT_OUTPUT_DIR_NAME), SdkConstants.FN_RESOURCE_TEXT).getPath();
                    libRTextFilesAndPackages.add(Pair.create(libRTxtFilePath, libPackage));
                }
            }
            AndroidJpsUtil.collectRTextFilesFromAarDeps(module, libRTextFilesAndPackages);
            final File outputDirForArtifacts = AndroidJpsUtil.getDirectoryForIntermediateArtifacts(context, module);
            final String proguardOutputCfgFilePath;
            if (AndroidJpsUtil.getProGuardConfigIfShouldRun(context, extension) != null) {
                if (AndroidJpsUtil.createDirIfNotExist(outputDirForArtifacts, context, BUILDER_NAME) == null) {
                    success = false;
                    continue;
                }
                proguardOutputCfgFilePath = new File(outputDirForArtifacts, AndroidCommonUtils.PROGUARD_CFG_OUTPUT_FILE_NAME).getPath();
            } else {
                proguardOutputCfgFilePath = null;
            }
            String rTxtOutDirOsPath = null;
            if (extension.isLibrary() || libRTextFilesAndPackages.size() > 0) {
                final File rTxtOutDir = new File(outputDirForArtifacts, R_TXT_OUTPUT_DIR_NAME);
                if (AndroidJpsUtil.createDirIfNotExist(rTxtOutDir, context, BUILDER_NAME) == null) {
                    success = false;
                    continue;
                }
                rTxtOutDirOsPath = rTxtOutDir.getPath();
            }
            final AndroidAptValidityState newState = new AndroidAptValidityState(resources, valueResFilesTimestamps, manifestElements, libRTextFilesAndPackages, packageName, proguardOutputCfgFilePath, rTxtOutDirOsPath, extension.isLibrary());
            if (newState.equalsTo(oldState)) {
                // we need to update state, because it also contains myValueResFilesTimestamps not taking into account by equalsTo()
                storage.update(module.getName(), newState);
                continue;
            }
            didSomething = true;
            context.processMessage(new ProgressMessage(AndroidJpsBundle.message("android.jps.progress.aapt", module.getName())));
            File tmpOutputDir = null;
            try {
                tmpOutputDir = FileUtil.createTempDirectory("android_apt_output", "tmp");
                final Map<AndroidCompilerMessageKind, List<String>> messages = AndroidApt.compile(target, -1, manifestFile.getPath(), packageName, tmpOutputDir.getPath(), resPaths, libRTextFilesAndPackages, generateNonFinalFields, proguardOutputCfgFilePath, rTxtOutDirOsPath, !extension.isLibrary());
                AndroidJpsUtil.addMessages(context, messages, ANDROID_APT_COMPILER, module.getName());
                if (messages.get(AndroidCompilerMessageKind.ERROR).size() > 0) {
                    success = false;
                    storage.update(module.getName(), null);
                } else {
                    if (!AndroidCommonUtils.directoriesContainSameContent(tmpOutputDir, aptOutputDirectory, JAVA_FILE_FILTER)) {
                        if (!deleteAndMarkRecursively(aptOutputDirectory, context, ANDROID_APT_COMPILER)) {
                            success = false;
                            continue;
                        }
                        final File parent = aptOutputDirectory.getParentFile();
                        if (parent != null && !parent.exists() && !parent.mkdirs()) {
                            context.processMessage(new CompilerMessage(ANDROID_APT_COMPILER, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.cannot.create.directory", parent.getPath())));
                            success = false;
                            continue;
                        }
                        // we use copyDir instead of moveDirWithContent here, because tmp directory may be located on other disk and
                        // moveDirWithContent doesn't work for such case
                        FileUtil.copyDir(tmpOutputDir, aptOutputDirectory);
                        markDirtyRecursively(aptOutputDirectory, context, ANDROID_APT_COMPILER, true);
                    }
                    storage.update(module.getName(), newState);
                }
            } finally {
                if (tmpOutputDir != null) {
                    FileUtil.delete(tmpOutputDir);
                }
            }
        } catch (IOException e) {
            AndroidJpsUtil.reportExceptionError(context, null, e, ANDROID_APT_COMPILER);
            success = false;
        }
    }
    if (!success) {
        return MyExitStatus.FAIL;
    } else if (didSomething) {
        return MyExitStatus.OK;
    }
    return MyExitStatus.NOTHING_CHANGED;
}
Also used : ProgressMessage(org.jetbrains.jps.incremental.messages.ProgressMessage) CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) HashMap(com.intellij.util.containers.HashMap) TObjectLongHashMap(gnu.trove.TObjectLongHashMap) Pair(com.intellij.openapi.util.Pair) IAndroidTarget(com.android.sdklib.IAndroidTarget) TObjectLongHashMap(gnu.trove.TObjectLongHashMap) JpsModule(org.jetbrains.jps.model.module.JpsModule) JpsAndroidModuleExtension(org.jetbrains.jps.android.model.JpsAndroidModuleExtension) HashMap(com.intellij.util.containers.HashMap) TObjectLongHashMap(gnu.trove.TObjectLongHashMap)

Example 8 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class AndroidJpsUtil method getAndroidPlatform.

@Nullable
public static AndroidPlatform getAndroidPlatform(@NotNull JpsModule module, @Nullable CompileContext context, String builderName) {
    final JpsSdk<JpsSimpleElement<JpsAndroidSdkProperties>> sdk = module.getSdk(JpsAndroidSdkType.INSTANCE);
    if (sdk == null) {
        if (context != null) {
            context.processMessage(new CompilerMessage(builderName, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.errors.sdk.not.specified", module.getName())));
        }
        return null;
    }
    final Pair<IAndroidTarget, AndroidSdkHandler> pair = getAndroidTarget(sdk, context, builderName);
    if (pair == null) {
        if (context != null) {
            context.processMessage(new CompilerMessage(builderName, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.errors.sdk.invalid", module.getName())));
        }
        return null;
    }
    return new AndroidPlatform(sdk, pair.getFirst(), pair.getSecond());
}
Also used : CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) AndroidSdkHandler(com.android.sdklib.repository.AndroidSdkHandler) JpsSimpleElement(org.jetbrains.jps.model.JpsSimpleElement) IAndroidTarget(com.android.sdklib.IAndroidTarget) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class ProjectResourceIdResolver method getIdMap.

private TIntObjectHashMap<String> getIdMap() {
    AndroidFacet facet = null;
    for (Module m : ModuleManager.getInstance(myProject).getModules()) {
        facet = AndroidFacet.getInstance(m);
        if (facet != null) {
            break;
        }
    }
    AndroidSdkData sdkData = facet == null ? null : facet.getSdkData();
    if (sdkData == null) {
        return null;
    }
    IAndroidTarget[] targets = sdkData.getTargets();
    if (targets.length == 0) {
        return null;
    }
    return sdkData.getTargetData(targets[targets.length - 1]).getPublicIdMap();
}
Also used : AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) IAndroidTarget(com.android.sdklib.IAndroidTarget) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 10 with IAndroidTarget

use of com.android.sdklib.IAndroidTarget in project android by JetBrains.

the class AndroidSdks method getPlatformAndAddOnJars.

@NotNull
public List<VirtualFile> getPlatformAndAddOnJars(@NotNull IAndroidTarget target) {
    List<VirtualFile> result = new ArrayList<>();
    VirtualFile platformFolder = getPlatformFolder(target);
    if (platformFolder != null) {
        VirtualFile androidJar = platformFolder.findChild(FN_FRAMEWORK_LIBRARY);
        if (androidJar != null) {
            File androidJarPath = virtualToIoFile(androidJar);
            VirtualFile androidJarRoot = findFileInJarFileSystem(androidJarPath);
            if (androidJarRoot != null) {
                result.add(androidJarRoot);
            }
            List<IAndroidTarget.OptionalLibrary> libraries = target.getAdditionalLibraries();
            for (IAndroidTarget.OptionalLibrary library : libraries) {
                VirtualFile root = getRoot(library);
                if (root != null) {
                    result.add(root);
                }
            }
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IAndroidTarget(com.android.sdklib.IAndroidTarget) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) VfsUtil.findFileByIoFile(com.intellij.openapi.vfs.VfsUtil.findFileByIoFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

IAndroidTarget (com.android.sdklib.IAndroidTarget)105 Nullable (org.jetbrains.annotations.Nullable)24 VirtualFile (com.intellij.openapi.vfs.VirtualFile)19 Module (com.intellij.openapi.module.Module)17 NotNull (org.jetbrains.annotations.NotNull)16 AndroidSdkData (org.jetbrains.android.sdk.AndroidSdkData)15 Sdk (com.intellij.openapi.projectRoots.Sdk)14 File (java.io.File)13 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)13 AndroidPlatform (org.jetbrains.android.sdk.AndroidPlatform)13 AndroidVersion (com.android.sdklib.AndroidVersion)11 Device (com.android.sdklib.devices.Device)11 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)9 State (com.android.sdklib.devices.State)8 AndroidSdkAdditionalData (org.jetbrains.android.sdk.AndroidSdkAdditionalData)8 AndroidTargetData (org.jetbrains.android.sdk.AndroidTargetData)8 Configuration (com.android.tools.idea.configurations.Configuration)7 CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)7 CompilerMessage (org.jetbrains.jps.incremental.messages.CompilerMessage)7 ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)6