Search in sources :

Example 31 with AndroidSdkData

use of org.jetbrains.android.sdk.AndroidSdkData in project android by JetBrains.

the class TemplateManager method getExtraTemplateRootFolders.

/**
   * @return A list of root folders containing extra templates
   */
@NotNull
public static List<File> getExtraTemplateRootFolders() {
    List<File> folders = new ArrayList<>();
    // Check in various locations in the SDK
    AndroidSdkData sdkData = AndroidSdks.getInstance().tryToChooseAndroidSdk();
    if (sdkData != null) {
        File location = sdkData.getLocation();
        if (USE_SDK_TEMPLATES) {
            // Look in SDK/tools/templates
            File toolsTemplatesFolder = new File(location, FileUtil.join(FD_TOOLS, FD_TEMPLATES));
            if (toolsTemplatesFolder.isDirectory()) {
                File[] templateRoots = toolsTemplatesFolder.listFiles(FileUtilRt.ALL_DIRECTORIES);
                if (templateRoots != null) {
                    Collections.addAll(folders, templateRoots);
                }
            }
        }
        // Look in SDK/extras/*
        File extras = new File(location, FD_EXTRAS);
        if (extras.isDirectory()) {
            for (File vendor : listFiles(extras)) {
                if (!vendor.isDirectory()) {
                    continue;
                }
                for (File pkg : listFiles(vendor)) {
                    if (pkg.isDirectory()) {
                        File folder = new File(pkg, FD_TEMPLATES);
                        if (folder.isDirectory()) {
                            folders.add(folder);
                        }
                    }
                }
            }
            // Legacy
            File folder = new File(extras, FD_TEMPLATES);
            if (folder.isDirectory()) {
                folders.add(folder);
            }
        }
        // Look in SDK/add-ons
        File addOns = new File(location, FD_ADDONS);
        if (addOns.isDirectory()) {
            for (File addOn : listFiles(addOns)) {
                if (!addOn.isDirectory()) {
                    continue;
                }
                File folder = new File(addOn, FD_TEMPLATES);
                if (folder.isDirectory()) {
                    folders.add(folder);
                }
            }
        }
    }
    // Look for source tree files
    String homePath = toSystemIndependentName(PathManager.getHomePath());
    // Release build?
    VirtualFile root = LocalFileSystem.getInstance().findFileByPath(toSystemIndependentName(homePath + BUNDLED_TEMPLATE_PATH));
    if (root == null) {
        // Development build?
        for (String path : DEVELOPMENT_TEMPLATE_PATHS) {
            root = LocalFileSystem.getInstance().findFileByPath(toSystemIndependentName(homePath + path));
            if (root != null) {
                break;
            }
        }
    }
    if (root == null) {
        // error message tailored for release build file layout
        LOG.error("Templates not found in: " + homePath + BUNDLED_TEMPLATE_PATH + " or " + homePath + Arrays.toString(DEVELOPMENT_TEMPLATE_PATHS));
    } else {
        File templateDir = new File(root.getCanonicalPath()).getAbsoluteFile();
        if (templateDir.isDirectory()) {
            folders.add(templateDir);
        }
    }
    return folders;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 32 with AndroidSdkData

use of org.jetbrains.android.sdk.AndroidSdkData in project android by JetBrains.

the class AndroidRunSdkToolAction method doAction.

public void doAction(@NotNull Project project) {
    if (IdeInfo.getInstance().isAndroidStudio()) {
        File androidHome = IdeSdks.getInstance().getAndroidSdkPath();
        if (androidHome != null) {
            doRunTool(project, androidHome.getPath());
            return;
        }
    }
    // Gradle project.
    try {
        LocalProperties localProperties = new LocalProperties(project);
        File androidSdkPath = localProperties.getAndroidSdkPath();
        if (androidSdkPath != null) {
            doRunTool(project, androidSdkPath.getPath());
            return;
        }
    } catch (IOException ignored) {
        LOG.info(String.format("Unable to read local.properties file from project '%1$s'", project.getName()), ignored);
    }
    List<AndroidFacet> facets = ProjectFacetManager.getInstance(project).getFacets(AndroidFacet.ID);
    assert facets.size() > 0;
    Set<String> sdkSet = new HashSet<>();
    for (AndroidFacet facet : facets) {
        AndroidSdkData sdkData = facet.getConfiguration().getAndroidSdk();
        if (sdkData != null) {
            sdkSet.add(sdkData.getLocation().getPath());
        }
    }
    if (sdkSet.size() == 0) {
        Messages.showErrorDialog(project, AndroidBundle.message("specify.platform.error"), CommonBundle.getErrorTitle());
        return;
    }
    String sdkPath = sdkSet.iterator().next();
    if (sdkSet.size() > 1) {
        String[] sdks = ArrayUtil.toStringArray(sdkSet);
        int index = Messages.showChooseDialog(project, AndroidBundle.message("android.choose.sdk.label"), AndroidBundle.message("android.choose.sdk.title"), Messages.getQuestionIcon(), sdks, sdkPath);
        if (index < 0) {
            return;
        }
        sdkPath = sdks[index];
    }
    doRunTool(project, sdkPath);
}
Also used : AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) IOException(java.io.IOException) File(java.io.File) LocalProperties(com.android.tools.idea.gradle.util.LocalProperties) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) HashSet(com.intellij.util.containers.HashSet)

Example 33 with AndroidSdkData

use of org.jetbrains.android.sdk.AndroidSdkData in project android by JetBrains.

the class SdkBuildToolsTooLowErrorHandler method getQuickFixHyperlinks.

@NotNull
public List<NotificationHyperlink> getQuickFixHyperlinks(@NotNull String minimumVersion, @NotNull Project project, @Nullable Module module) {
    List<NotificationHyperlink> hyperlinks = new ArrayList<>();
    boolean buildToolInstalled = false;
    AndroidSdkHandler sdkHandler = null;
    AndroidSdkData androidSdkData = AndroidSdks.getInstance().tryToChooseAndroidSdk();
    if (androidSdkData != null) {
        sdkHandler = androidSdkData.getSdkHandler();
    }
    if (sdkHandler != null) {
        ProgressIndicator progress = new StudioLoggerProgressIndicator(SdkBuildToolsTooLowErrorHandler.class);
        RepositoryPackages packages = sdkHandler.getSdkManager(progress).getPackages();
        LocalPackage buildTool = packages.getLocalPackages().get(getBuildToolsPath(parseRevision(minimumVersion)));
        buildToolInstalled = buildTool != null;
    }
    if (module != null) {
        VirtualFile buildFile = getGradleBuildFile(module);
        AndroidPluginInfo androidPluginInfo = AndroidPluginInfo.find(project);
        if (!buildToolInstalled) {
            if (androidPluginInfo != null && androidPluginInfo.isExperimental()) {
                hyperlinks.add(new InstallBuildToolsHyperlink(minimumVersion, null));
            } else {
                hyperlinks.add(new InstallBuildToolsHyperlink(minimumVersion, buildFile));
            }
        } else if (buildFile != null && androidPluginInfo != null && !androidPluginInfo.isExperimental()) {
            hyperlinks.add(new FixBuildToolsVersionHyperlink(buildFile, minimumVersion));
        }
        if (buildFile != null) {
            hyperlinks.add(new OpenFileHyperlink(buildFile.getPath()));
        }
    }
    return hyperlinks;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalPackage(com.android.repository.api.LocalPackage) FixBuildToolsVersionHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.FixBuildToolsVersionHyperlink) ArrayList(java.util.ArrayList) AndroidSdkHandler(com.android.sdklib.repository.AndroidSdkHandler) InstallBuildToolsHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.InstallBuildToolsHyperlink) StudioLoggerProgressIndicator(com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator) AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) RepositoryPackages(com.android.repository.impl.meta.RepositoryPackages) AndroidPluginInfo(com.android.tools.idea.gradle.plugin.AndroidPluginInfo) StudioLoggerProgressIndicator(com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator) ProgressIndicator(com.android.repository.api.ProgressIndicator) OpenFileHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.OpenFileHyperlink) NotificationHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink) NotNull(org.jetbrains.annotations.NotNull)

Example 34 with AndroidSdkData

use of org.jetbrains.android.sdk.AndroidSdkData in project android by JetBrains.

the class MissingPlatformErrorHandler method findAndAddQuickFixes.

private void findAndAddQuickFixes(@NotNull NotificationData notification, @NotNull Project project, @NotNull String text) {
    String missingPlatform = getMissingPlatform(text);
    if (missingPlatform == null) {
        return;
    }
    String loadError = null;
    List<NotificationHyperlink> hyperlinks = new ArrayList<>();
    AndroidSdkHandler sdkHandler = null;
    AndroidSdkData androidSdkData = AndroidSdks.getInstance().tryToChooseAndroidSdk();
    if (androidSdkData != null) {
        sdkHandler = androidSdkData.getSdkHandler();
    }
    if (sdkHandler != null) {
        AndroidVersion version = AndroidTargetHash.getPlatformVersion(missingPlatform);
        if (version != null) {
            // Is the platform installed?
            ProgressIndicator logger = new StudioLoggerProgressIndicator(getClass());
            loadError = sdkHandler.getAndroidTargetManager(logger).getErrorForPackage(DetailsTypes.getPlatformPath(version));
            hyperlinks.add(new InstallPlatformHyperlink(version));
        }
    }
    if (hyperlinks.isEmpty()) {
        // We are unable to install platform automatically.
        List<AndroidFacet> facets = ProjectFacetManager.getInstance(project).getFacets(AndroidFacet.ID);
        if (!facets.isEmpty()) {
            // We can only open SDK manager if the project has an Android facet. Android facet has a reference to the Android SDK manager.
            hyperlinks.add(new OpenAndroidSdkManagerHyperlink());
        }
    }
    if (isNotEmpty(loadError)) {
        text += "\nPossible cause: " + loadError;
    }
    SyncMessages.getInstance(project).updateNotification(notification, text, hyperlinks);
}
Also used : OpenAndroidSdkManagerHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.OpenAndroidSdkManagerHyperlink) ArrayList(java.util.ArrayList) AndroidSdkHandler(com.android.sdklib.repository.AndroidSdkHandler) AndroidVersion(com.android.sdklib.AndroidVersion) InstallPlatformHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.InstallPlatformHyperlink) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) StudioLoggerProgressIndicator(com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator) AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) StudioLoggerProgressIndicator(com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator) ProgressIndicator(com.android.repository.api.ProgressIndicator) NotificationHyperlink(com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink)

Example 35 with AndroidSdkData

use of org.jetbrains.android.sdk.AndroidSdkData in project android by JetBrains.

the class TemplateTest method checkTemplate.

private void checkTemplate(File templateFile, boolean createWithProject, @Nullable Map<String, Object> overrides, @Nullable Map<String, Object> projectOverrides) throws Exception {
    if (KNOWN_BROKEN.contains(templateFile.getName())) {
        return;
    }
    AndroidSdkData sdkData = AndroidSdks.getInstance().tryToChooseAndroidSdk();
    assertNotNull(sdkData);
    NewProjectWizardState values = createNewProjectState(createWithProject, sdkData);
    String projectNameBase = "TestProject" + templateFile.getName();
    TemplateWizardState state = values.getActivityTemplateState();
    state.setTemplateLocation(templateFile);
    // Iterate over all (valid) combinations of build target, minSdk and targetSdk
    // TODO: Assert that the SDK manager has a minimum set of SDKs installed needed to be certain
    // the test is comprehensive
    // For now make sure there's at least one
    boolean ranTest = false;
    IAndroidTarget[] targets = sdkData.getTargets();
    for (int i = targets.length - 1; i >= 0; i--) {
        IAndroidTarget target = targets[i];
        if (!target.isPlatform()) {
            continue;
        }
        if (!isInterestingApiLevel(target.getVersion().getApiLevel(), MANUAL_BUILD_API)) {
            continue;
        }
        TemplateMetadata activityMetadata = state.getTemplateMetadata();
        TemplateMetadata projectMetadata = values.getTemplateMetadata();
        assertNotNull(activityMetadata);
        assertNotNull(projectMetadata);
        int lowestSupportedApi = Math.max(projectMetadata.getMinSdk(), activityMetadata.getMinSdk());
        for (int minSdk = lowestSupportedApi; minSdk <= SdkVersionInfo.HIGHEST_KNOWN_API; minSdk++) {
            // Don't bother checking *every* single minSdk, just pick some interesting ones
            if (!isInterestingApiLevel(minSdk, MANUAL_MIN_API)) {
                continue;
            }
            for (int targetSdk = minSdk; targetSdk <= SdkVersionInfo.HIGHEST_KNOWN_API; targetSdk++) {
                if (!isInterestingApiLevel(targetSdk, MANUAL_TARGET_API)) {
                    continue;
                }
                String status = validateTemplate(projectMetadata, minSdk, target.getVersion().getApiLevel());
                if (status != null) {
                    continue;
                }
                // Also make sure activity is enabled for these versions
                status = validateTemplate(activityMetadata, minSdk, target.getVersion().getApiLevel());
                if (status != null) {
                    continue;
                }
                // Iterate over all new new project templates
                // should I try all options of theme with all platforms?
                // or just try all platforms, with one setting for each?
                // doesn't seem like I need to multiply
                // just pick the best setting that applies instead for each platform
                Collection<Parameter> parameters = projectMetadata.getParameters();
                projectParameters: for (Parameter parameter : parameters) {
                    if (overrides != null) {
                        String base = projectNameBase + "_min_" + minSdk + "_target_" + targetSdk + "_build_" + target.getVersion().getApiLevel() + "_overrides";
                        checkApiTarget(minSdk, targetSdk, target, values, base, state, overrides, projectOverrides);
                        ranTest = true;
                        break projectParameters;
                    }
                    List<Element> options = parameter.getOptions();
                    if (parameter.type == Parameter.Type.ENUM) {
                        assertNotNull(parameter.id);
                        for (Element element : options) {
                            Option option = Option.get(element);
                            String optionId = option.id;
                            int optionMinSdk = option.minSdk;
                            int optionMinBuildApi = option.minBuild;
                            if (optionMinSdk <= minSdk && optionMinBuildApi <= target.getVersion().getApiLevel()) {
                                values.put(parameter.id, optionId);
                                if (parameter.id.equals("baseTheme")) {
                                    String base = projectNameBase + "_min_" + minSdk + "_target_" + targetSdk + "_build_" + target.getVersion().getApiLevel() + "_theme_" + optionId;
                                    checkApiTarget(minSdk, targetSdk, target, values, base, state, null, null);
                                    ranTest = true;
                                    if (!TEST_VARIABLE_COMBINATIONS) {
                                        break projectParameters;
                                    }
                                }
                            }
                        }
                    }
                }
                if (TEST_JUST_ONE_TARGET_SDK_VERSION) {
                    break;
                }
            }
            if (TEST_JUST_ONE_MIN_SDK) {
                break;
            }
        }
        if (TEST_JUST_ONE_BUILD_TARGET) {
            break;
        }
    }
    assertTrue("Didn't run any tests! Make sure you have the right platforms installed.", ranTest);
}
Also used : Element(org.w3c.dom.Element) TemplateWizardState(com.android.tools.idea.wizard.template.TemplateWizardState) IAndroidTarget(com.android.sdklib.IAndroidTarget) NewProjectWizardState(com.android.tools.idea.npw.NewProjectWizardState) AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) TemplateMetadata(com.android.tools.idea.templates.TemplateMetadata)

Aggregations

AndroidSdkData (org.jetbrains.android.sdk.AndroidSdkData)39 IAndroidTarget (com.android.sdklib.IAndroidTarget)15 File (java.io.File)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 Sdk (com.intellij.openapi.projectRoots.Sdk)9 NotNull (org.jetbrains.annotations.NotNull)7 AndroidSdkHandler (com.android.sdklib.repository.AndroidSdkHandler)6 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)6 Nullable (org.jetbrains.annotations.Nullable)6 StudioLoggerProgressIndicator (com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator)5 AndroidSdkAdditionalData (org.jetbrains.android.sdk.AndroidSdkAdditionalData)5 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 AndroidVersion (com.android.sdklib.AndroidVersion)3 NewProjectWizardState (com.android.tools.idea.npw.NewProjectWizardState)3 SdkPaths.validateAndroidSdk (com.android.tools.idea.sdk.SdkPaths.validateAndroidSdk)3 TemplateWizardState (com.android.tools.idea.wizard.template.TemplateWizardState)3 AndroidPlatform (org.jetbrains.android.sdk.AndroidPlatform)3 ProgressIndicator (com.android.repository.api.ProgressIndicator)2