Search in sources :

Example 6 with NdkModuleModel

use of com.android.tools.idea.gradle.project.model.NdkModuleModel in project android by JetBrains.

the class LinkExternalCppProjectAction method isValidAndroidGradleModuleSelected.

private static boolean isValidAndroidGradleModuleSelected(@NotNull DataContext dataContext) {
    Module module = getSelectedModule(dataContext);
    if (module == null) {
        return false;
    }
    AndroidModuleModel androidModel = AndroidModuleModel.get(module);
    if (androidModel == null || !androidModel.getFeatures().isExternalBuildSupported()) {
        return false;
    }
    AndroidPluginGeneration pluginGeneration = AndroidPluginGeneration.find(module);
    if (pluginGeneration == COMPONENT) {
        // Updating experimental plugin DSL is not yet supported.
        return false;
    }
    NdkModuleModel ndkModuleModel = NdkModuleModel.get(module);
    if (ndkModuleModel != null) {
        // Some external native project is already linked to this module.
        return false;
    }
    if (GradleBuildModel.get(module) == null) {
        // This should never for an fully synced module, but checking for just in case.
        return false;
    }
    return true;
}
Also used : AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) Module(com.intellij.openapi.module.Module) NdkModuleModel(com.android.tools.idea.gradle.project.model.NdkModuleModel) AndroidPluginGeneration(com.android.tools.idea.gradle.plugin.AndroidPluginGeneration)

Example 7 with NdkModuleModel

use of com.android.tools.idea.gradle.project.model.NdkModuleModel in project android by JetBrains.

the class GradleFiles method areGradleFilesModified.

/**
   * Indicates whether a project sync with Gradle is needed if the following files:
   * <ul>
   *   <li>gradle.properties</li>
   *   <li>build.gradle</li>
   *   <li>settings.gradle</li>
   *   <li>external build files (e.g. cmake files)</li>
   * </ul>
   * were modified after the given time.
   *
   * @param referenceTimeInMillis the given time, in milliseconds.
   * @return {@code true} if any of the Gradle files changed, {@code false} otherwise.
   * @throws IllegalArgumentException if the given time is less than or equal to zero.
   */
public boolean areGradleFilesModified(long referenceTimeInMillis) {
    if (referenceTimeInMillis <= 0) {
        throw new IllegalArgumentException("Reference time (in milliseconds) should be greater than zero");
    }
    setExternalBuildFilesModified(false);
    if (areFilesInProjectRootFolderModified(referenceTimeInMillis, FN_GRADLE_PROPERTIES, FN_SETTINGS_GRADLE)) {
        return true;
    }
    for (Module module : ModuleManager.getInstance(myProject).getModules()) {
        VirtualFile buildFile = getGradleBuildFile(module);
        if (buildFile != null) {
            if (myDocumentManager.isFileModified(buildFile)) {
                return true;
            }
            File buildFilePath = virtualToIoFile(buildFile);
            if (buildFilePath.lastModified() > referenceTimeInMillis) {
                return true;
            }
        }
        NdkModuleModel ndkModuleModel = NdkModuleModel.get(module);
        if (ndkModuleModel != null) {
            for (File externalBuildFile : ndkModuleModel.getAndroidProject().getBuildFiles()) {
                if (externalBuildFile.lastModified() > referenceTimeInMillis) {
                    setExternalBuildFilesModified(true);
                    return true;
                }
                // TODO find a better way to find a VirtualFile without refreshing the file systerm. It is expensive.
                VirtualFile virtualFile = findFileByIoFile(externalBuildFile, true);
                if (virtualFile != null && myDocumentManager.isFileModified(virtualFile)) {
                    setExternalBuildFilesModified(true);
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Module(com.intellij.openapi.module.Module) NdkModuleModel(com.android.tools.idea.gradle.project.model.NdkModuleModel) VfsUtil.findFileByIoFile(com.intellij.openapi.vfs.VfsUtil.findFileByIoFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) GradleUtil.getGradleBuildFile(com.android.tools.idea.gradle.util.GradleUtil.getGradleBuildFile) PsiFile(com.intellij.psi.PsiFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)

Example 8 with NdkModuleModel

use of com.android.tools.idea.gradle.project.model.NdkModuleModel in project android by JetBrains.

the class ProjectStructureUsageTracker method trackProjectStructure.

@VisibleForTesting
void trackProjectStructure(@NotNull Module[] modules) {
    AndroidModuleModel appModel = null;
    AndroidModuleModel libModel = null;
    int appCount = 0;
    int libCount = 0;
    List<GradleLibrary> gradleLibraries = new ArrayList<>();
    for (Module module : modules) {
        AndroidModuleModel androidModel = AndroidModuleModel.get(module);
        if (androidModel != null) {
            if (androidModel.getProjectType() == PROJECT_TYPE_LIBRARY) {
                libModel = androidModel;
                libCount++;
                continue;
            }
            appModel = androidModel;
            appCount++;
            GradleLibrary gradleLibrary = trackExternalDependenciesInAndroidApp(androidModel);
            if (gradleLibrary != null) {
                gradleLibraries.add(gradleLibrary);
            }
        }
    }
    // Ideally we would like to get data from an "app" module, but if the project does not have one (which would be unusual, we can use
    // an Android library one.)
    AndroidModuleModel model = appModel != null ? appModel : libModel;
    if (model != null) {
        List<GradleAndroidModule> gradleAndroidModules = new ArrayList<>();
        List<GradleNativeAndroidModule> gradleNativeAndroidModules = new ArrayList<>();
        String appId = AndroidStudioUsageTracker.anonymizeUtf8(model.getApplicationId());
        AndroidProject androidProject = model.getAndroidProject();
        GradleVersion gradleVersion = GradleVersions.getInstance().getGradleVersion(myProject);
        if (gradleVersion == null) {
            gradleVersion = new GradleVersion(0, 0, 0);
        }
        GradleModule gradleModule = GradleModule.newBuilder().setTotalModuleCount(modules.length).setAppModuleCount(appCount).setLibModuleCount(libCount).build();
        for (Module module : modules) {
            AndroidModuleModel androidModel = AndroidModuleModel.get(module);
            if (androidModel != null) {
                gradleAndroidModules.add(GradleAndroidModule.newBuilder().setModuleName(AndroidStudioUsageTracker.anonymizeUtf8(module.getName())).setSigningConfigCount(androidModel.getAndroidProject().getSigningConfigs().size()).setIsLibrary(androidModel.getProjectType() == PROJECT_TYPE_LIBRARY).setBuildTypeCount(androidModel.getBuildTypeNames().size()).setFlavorCount(androidModel.getProductFlavorNames().size()).setFlavorDimension(getFlavorDimensions(androidModel).size()).build());
            }
            boolean shouldReportNative = false;
            NdkModuleModel ndkModuleModel = NdkModuleModel.get(module);
            NativeBuildSystemType buildSystemType = NativeBuildSystemType.UNKNOWN_NATIVE_BUILD_SYSTEM_TYPE;
            String moduleName = "";
            if (ndkModuleModel != null) {
                shouldReportNative = true;
                if (ndkModuleModel.modelVersionIsAtLeast("2.2.0")) {
                    for (String buildSystem : ndkModuleModel.getAndroidProject().getBuildSystems()) {
                        buildSystemType = stringToBuildSystemType(buildSystem);
                    }
                } else {
                    buildSystemType = NativeBuildSystemType.GRADLE_EXPERIMENTAL;
                }
                moduleName = AndroidStudioUsageTracker.anonymizeUtf8(ndkModuleModel.getModuleName());
            } else if (androidModel != null && areNativeLibrariesPresent(androidModel.getAndroidProject())) {
                shouldReportNative = true;
                if (AndroidPluginGeneration.find(module) == COMPONENT) {
                    buildSystemType = NativeBuildSystemType.GRADLE_EXPERIMENTAL;
                } else {
                    buildSystemType = NativeBuildSystemType.NDK_COMPILE;
                }
            }
            if (shouldReportNative) {
                gradleNativeAndroidModules.add(GradleNativeAndroidModule.newBuilder().setModuleName(moduleName).setBuildSystemType(buildSystemType).build());
            }
        }
        UsageTracker.getInstance().log(AndroidStudioEvent.newBuilder().setCategory(EventCategory.GRADLE).setKind(AndroidStudioEvent.EventKind.GRADLE_BUILD_DETAILS).setGradleBuildDetails(GradleBuildDetails.newBuilder().setAppId(appId).setAndroidPluginVersion(androidProject.getModelVersion()).setGradleVersion(gradleVersion.toString()).setUserEnabledIr(InstantRunSettings.isInstantRunEnabled()).setModelSupportsIr(InstantRunGradleUtils.modelSupportsInstantRun(model)).setVariantSupportsIr(InstantRunGradleUtils.variantSupportsInstantRun(model)).addAllLibraries(gradleLibraries).addModules(gradleModule).addAllAndroidModules(gradleAndroidModules).addAllNativeAndroidModules(gradleNativeAndroidModules)));
    }
}
Also used : NativeBuildSystemType(com.google.wireless.android.sdk.stats.GradleNativeAndroidModule.NativeBuildSystemType) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) Module(com.intellij.openapi.module.Module) GradleVersion(com.android.ide.common.repository.GradleVersion) NdkModuleModel(com.android.tools.idea.gradle.project.model.NdkModuleModel) VisibleForTesting(com.android.annotations.VisibleForTesting)

Example 9 with NdkModuleModel

use of com.android.tools.idea.gradle.project.model.NdkModuleModel in project android by JetBrains.

the class BuildVariantView method updateContents.

public void updateContents() {
    GradleSyncState syncState = GradleSyncState.getInstance(myProject);
    if (syncState.isSyncInProgress() && !syncState.isSyncSkipped()) {
        projectImportStarted();
        return;
    }
    final List<Object[]> rows = Lists.newArrayList();
    final List<BuildVariantItem[]> variantNamesPerRow = Lists.newArrayList();
    for (Module module : getGradleModulesWithAndroidProjects()) {
        AndroidFacet androidFacet = AndroidFacet.getInstance(module);
        NdkFacet ndkFacet = NdkFacet.getInstance(module);
        // getGradleModules() returns only relevant modules.
        assert androidFacet != null || ndkFacet != null;
        String variantName = null;
        if (androidFacet != null) {
            JpsAndroidModuleProperties facetProperties = androidFacet.getProperties();
            variantName = facetProperties.SELECTED_BUILD_VARIANT;
        }
        BuildVariantItem[] variantNames = getVariantItems(module);
        if (variantNames != null) {
            if (androidFacet != null) {
                AndroidModuleModel androidModel = AndroidModuleModel.get(module);
                // AndroidModel may be null when applying a quick fix (e.g. "Fix Gradle version")
                if (androidModel != null) {
                    variantName = androidModel.getSelectedVariant().getName();
                }
            } else {
                // As only the modules backed by either AndroidGradleModel or NativeAndroidGradleModel are shown in the Build Variants View,
                // when a module is not backed by AndroidGradleModel, it surely contains a valid NativeAndroidGradleModel.
                NdkModuleModel ndkModuleModel = NdkModuleModel.get(module);
                if (ndkModuleModel != null) {
                    variantName = ndkModuleModel.getSelectedVariant().getName();
                }
            }
            variantNamesPerRow.add(variantNames);
        }
        if (variantName != null) {
            Object[] row = { module, variantName };
            rows.add(row);
        }
    }
    Runnable setModelTask = () -> getVariantsTable().setModel(rows, variantNamesPerRow);
    Application application = ApplicationManager.getApplication();
    if (application.isDispatchThread()) {
        setModelTask.run();
    } else {
        application.invokeLater(setModelTask);
    }
}
Also used : NdkFacet(com.android.tools.idea.gradle.project.facet.ndk.NdkFacet) JpsAndroidModuleProperties(org.jetbrains.jps.android.model.impl.JpsAndroidModuleProperties) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) GradleSyncState(com.android.tools.idea.gradle.project.sync.GradleSyncState) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) Module(com.intellij.openapi.module.Module) NdkModuleModel(com.android.tools.idea.gradle.project.model.NdkModuleModel) Application(com.intellij.openapi.application.Application)

Example 10 with NdkModuleModel

use of com.android.tools.idea.gradle.project.model.NdkModuleModel in project android by JetBrains.

the class AndroidGradleProjectResolver method populateModuleContentRoots.

@Override
public void populateModuleContentRoots(@NotNull IdeaModule gradleModule, @NotNull DataNode<ModuleData> ideModule) {
    ImportedModule importedModule = new ImportedModule(gradleModule);
    ideModule.createChild(IMPORTED_MODULE, importedModule);
    GradleProject gradleProject = gradleModule.getGradleProject();
    GradleScript buildScript = null;
    try {
        buildScript = gradleProject.getBuildScript();
    } catch (UnsupportedOperationException ignore) {
    }
    if (buildScript == null || !isAndroidGradleProject()) {
        nextResolver.populateModuleContentRoots(gradleModule, ideModule);
        return;
    }
    // do not derive module root dir based on *.iml file location
    File moduleRootDirPath = new File(toSystemDependentName(ideModule.getData().getLinkedExternalProjectPath()));
    AndroidProject androidProject = resolverCtx.getExtraProject(gradleModule, AndroidProject.class);
    boolean androidProjectWithoutVariants = false;
    String moduleName = gradleModule.getName();
    if (androidProject != null) {
        Variant selectedVariant = myVariantSelector.findVariantToSelect(androidProject);
        if (selectedVariant == null) {
            // If an Android project does not have variants, it would be impossible to build. This is a possible but invalid use case.
            // For now we are going to treat this case as a Java library module, because everywhere in the IDE (e.g. run configurations,
            // editors, test support, variants tool window, project building, etc.) we have the assumption that there is at least one variant
            // per Android project, and changing that in the code base is too risky, for very little benefit.
            // See https://code.google.com/p/android/issues/detail?id=170722
            androidProjectWithoutVariants = true;
        } else {
            String variantName = selectedVariant.getName();
            AndroidModuleModel model = new AndroidModuleModel(moduleName, moduleRootDirPath, androidProject, variantName);
            ideModule.createChild(ANDROID_MODEL, model);
        }
    }
    NativeAndroidProject nativeAndroidProject = resolverCtx.getExtraProject(gradleModule, NativeAndroidProject.class);
    if (nativeAndroidProject != null) {
        NdkModuleModel ndkModuleModel = new NdkModuleModel(moduleName, moduleRootDirPath, nativeAndroidProject);
        ideModule.createChild(NDK_MODEL, ndkModuleModel);
    }
    File gradleSettingsFile = new File(moduleRootDirPath, FN_SETTINGS_GRADLE);
    if (gradleSettingsFile.isFile() && androidProject == null && nativeAndroidProject == null) {
        // This is just a root folder for a group of Gradle projects. We don't set an IdeaGradleProject so the JPS builder won't try to
        // compile it using Gradle. We still need to create the module to display files inside it.
        createJavaProject(gradleModule, ideModule, false);
        return;
    }
    BuildScriptClasspathModel buildScriptModel = resolverCtx.getExtraProject(BuildScriptClasspathModel.class);
    String gradleVersion = buildScriptModel != null ? buildScriptModel.getGradleVersion() : null;
    File buildFilePath = buildScript.getSourceFile();
    GradleModuleModel gradleModuleModel = new GradleModuleModel(moduleName, gradleProject, buildFilePath, gradleVersion);
    ideModule.createChild(GRADLE_MODULE_MODEL, gradleModuleModel);
    if (nativeAndroidProject == null && (androidProject == null || androidProjectWithoutVariants)) {
        // This is a Java lib module.
        createJavaProject(gradleModule, ideModule, androidProjectWithoutVariants);
    }
}
Also used : BuildScriptClasspathModel(org.jetbrains.plugins.gradle.model.BuildScriptClasspathModel) ImportedModule(com.android.tools.idea.gradle.ImportedModule) AndroidProject(com.android.builder.model.AndroidProject) NativeAndroidProject(com.android.builder.model.NativeAndroidProject) Variant(com.android.builder.model.Variant) GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) NativeAndroidProject(com.android.builder.model.NativeAndroidProject) GradleScript(org.gradle.tooling.model.gradle.GradleScript) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) GradleProject(org.gradle.tooling.model.GradleProject) NdkModuleModel(com.android.tools.idea.gradle.project.model.NdkModuleModel) File(java.io.File)

Aggregations

NdkModuleModel (com.android.tools.idea.gradle.project.model.NdkModuleModel)18 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)10 Module (com.intellij.openapi.module.Module)9 NativeAndroidProject (com.android.builder.model.NativeAndroidProject)4 NdkFacet (com.android.tools.idea.gradle.project.facet.ndk.NdkFacet)4 GradleModuleModel (com.android.tools.idea.gradle.project.model.GradleModuleModel)4 AndroidProject (com.android.builder.model.AndroidProject)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 File (java.io.File)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 Nullable (org.jetbrains.annotations.Nullable)3 VisibleForTesting (com.android.annotations.VisibleForTesting)2 GradleFacet (com.android.tools.idea.gradle.project.facet.gradle.GradleFacet)2 GradleSyncState (com.android.tools.idea.gradle.project.sync.GradleSyncState)2 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)2 DataNode (com.intellij.openapi.externalSystem.model.DataNode)2 VfsUtil.findFileByIoFile (com.intellij.openapi.vfs.VfsUtil.findFileByIoFile)2 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)2 NotNull (org.jetbrains.annotations.NotNull)2 NativeArtifact (com.android.builder.model.NativeArtifact)1