Search in sources :

Example 1 with IdeSdks

use of com.android.tools.idea.sdk.IdeSdks in project android by JetBrains.

the class GradleSyncTest method sdkSwitch.

// Verifies that the IDE switches SDKs if the IDE and project SDKs are not the same.
@Test
public void sdkSwitch() throws IOException {
    File secondSdkPath = getFilePathPropertyOrSkipTest("second.android.sdk.path", "the path of a secondary Android SDK", true);
    getGuiTestSuiteState().setSkipSdkMerge(true);
    IdeSdks ideSdks = IdeSdks.getInstance();
    File originalSdkPath = ideSdks.getAndroidSdkPath();
    guiTest.importSimpleApplication();
    IdeFrameFixture ideFrame = guiTest.ideFrame();
    // Change the SDK in the project. We expect the IDE to have the same SDK as the project.
    LocalProperties localProperties = new LocalProperties(ideFrame.getProject());
    localProperties.setAndroidSdkPath(secondSdkPath);
    localProperties.save();
    ideFrame.requestProjectSync();
    MessagesFixture messages = ideFrame.findMessageDialog(ANDROID_SDK_MANAGER_DIALOG_TITLE);
    messages.click("Use Project's SDK");
    ideFrame.waitForGradleProjectSyncToFinish();
    assertThat(ideSdks.getAndroidSdkPath()).isEqualTo(secondSdkPath);
    // Set the project's SDK to be the original one. Now we will choose the IDE's SDK.
    localProperties = new LocalProperties(ideFrame.getProject());
    localProperties.setAndroidSdkPath(originalSdkPath);
    localProperties.save();
    ideFrame.requestProjectSync();
    messages = ideFrame.findMessageDialog(ANDROID_SDK_MANAGER_DIALOG_TITLE);
    messages.click("Use Android Studio's SDK");
    ideFrame.waitForGradleProjectSyncToFinish();
    localProperties = new LocalProperties(ideFrame.getProject());
    assertThat(localProperties.getAndroidSdkPath()).isEqualTo(secondSdkPath);
}
Also used : IdeSdks(com.android.tools.idea.sdk.IdeSdks) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtil.findFileByIoFile(com.intellij.openapi.vfs.VfsUtil.findFileByIoFile) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) File(java.io.File) GradleUtil.getGradleBuildFile(com.android.tools.idea.gradle.util.GradleUtil.getGradleBuildFile) LocalProperties(com.android.tools.idea.gradle.util.LocalProperties) Test(org.junit.Test)

Example 2 with IdeSdks

use of com.android.tools.idea.sdk.IdeSdks in project android by JetBrains.

the class GradleSpecificInitializer method setupSdks.

private static void setupSdks() {
    IdeSdks ideSdks = IdeSdks.getInstance();
    File androidHome = ideSdks.getAndroidSdkPath();
    if (androidHome != null) {
        String androidHomePath = androidHome.getAbsolutePath();
        ValidationResult result = validateLocation(androidHomePath, "Android SDK location", false, WritableCheckMode.DO_NOT_CHECK);
        if (result.isError()) {
            notifyInvalidSdk();
        }
        // Do not prompt user to select SDK path (we have one already.) Instead, check SDK compatibility when a project is opened.
        return;
    }
    // If running in a GUI test we don't want the "Select SDK" dialog to show up when running GUI tests.
    if (AndroidPlugin.isGuiTestingMode()) {
        // This is good enough. Later on in the GUI test we'll validate the given SDK path.
        return;
    }
    Sdk sdk = findFirstCompatibleAndroidSdk();
    if (sdk != null) {
        String sdkHomePath = sdk.getHomePath();
        assert sdkHomePath != null;
        ideSdks.createAndroidSdkPerAndroidTarget(new File(toSystemDependentName(sdkHomePath)));
        return;
    }
    // Called in a 'invokeLater' block, otherwise file chooser will hang forever.
    ApplicationManager.getApplication().invokeLater(() -> {
        File androidSdkPath = getAndroidSdkPath();
        if (androidSdkPath == null) {
            return;
        }
        FirstRunWizardMode wizardMode = AndroidStudioWelcomeScreenProvider.getWizardMode();
        // Only show "Select SDK" dialog if the "First Run" wizard is not displayed.
        boolean promptSdkSelection = wizardMode == null;
        Sdk sdk1 = createNewAndroidPlatform(androidSdkPath.getPath(), promptSdkSelection);
        if (sdk1 != null) {
            // Rename the SDK to fit our default naming convention.
            String sdkNamePrefix = AndroidSdks.SDK_NAME_PREFIX;
            if (sdk1.getName().startsWith(sdkNamePrefix)) {
                SdkModificator sdkModificator = sdk1.getSdkModificator();
                sdkModificator.setName(sdkNamePrefix + sdk1.getName().substring(sdkNamePrefix.length()));
                sdkModificator.commitChanges();
                // Rename the JDK that goes along with this SDK.
                AndroidSdkAdditionalData additionalData = AndroidSdks.getInstance().getAndroidSdkAdditionalData(sdk1);
                if (additionalData != null) {
                    Sdk jdk = additionalData.getJavaSdk();
                    if (jdk != null) {
                        sdkModificator = jdk.getSdkModificator();
                        sdkModificator.setName(DEFAULT_JDK_NAME);
                        sdkModificator.commitChanges();
                    }
                }
                // Fill out any missing build APIs for this new SDK.
                ideSdks.createAndroidSdkPerAndroidTarget(androidSdkPath);
            }
        }
    });
}
Also used : IdeSdks(com.android.tools.idea.sdk.IdeSdks) AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData) FirstRunWizardMode(com.android.tools.idea.welcome.config.FirstRunWizardMode) Sdk(com.intellij.openapi.projectRoots.Sdk) ValidationResult(com.android.tools.idea.npw.WizardUtils.ValidationResult) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 3 with IdeSdks

use of com.android.tools.idea.sdk.IdeSdks in project android by JetBrains.

the class AndroidSdkDataTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    File sdkDir = TestUtils.getSdk();
    sdkData = AndroidSdkData.getSdkData(sdkDir);
    ApplicationManager.getApplication().runWriteAction(() -> {
        IdeSdks ideSdks = IdeSdks.getInstance();
        ideSdks.setAndroidSdkPath(sdkDir, null);
        ProjectRootManager.getInstance(getProject()).setProjectSdk(ideSdks.getEligibleAndroidSdks().get(0));
    });
}
Also used : IdeSdks(com.android.tools.idea.sdk.IdeSdks) File(java.io.File)

Example 4 with IdeSdks

use of com.android.tools.idea.sdk.IdeSdks in project android by JetBrains.

the class AndroidGradleTestCase method setUpSdks.

private void setUpSdks() {
    // We seem to have two different locations where the SDK needs to be specified.
    // One is whatever is already defined in the JDK Table, and the other is the global one as defined by IdeSdks.
    // Gradle import will fail if the global one isn't set.
    File androidSdkPath = getSdk();
    IdeSdks ideSdks = IdeSdks.getInstance();
    runWriteCommandAction(getProject(), () -> {
        if (IdeInfo.getInstance().isAndroidStudio()) {
            ideSdks.setUseEmbeddedJdk();
            LOG.info("Set JDK to " + ideSdks.getJdkPath());
        }
        ideSdks.setAndroidSdkPath(androidSdkPath, getProject());
        LOG.info("Set IDE Sdk Path to " + androidSdkPath);
    });
    Sdk currentJdk = ideSdks.getJdk();
    assertNotNull(currentJdk);
    assertTrue("JDK 8 is required. Found: " + currentJdk.getHomePath(), Jdks.getInstance().isApplicableJdk(currentJdk, JDK_1_8));
}
Also used : IdeSdks(com.android.tools.idea.sdk.IdeSdks) Sdk(com.intellij.openapi.projectRoots.Sdk) TestUtils.getSdk(com.android.testutils.TestUtils.getSdk) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File) TestUtils.getWorkspaceFile(com.android.testutils.TestUtils.getWorkspaceFile)

Example 5 with IdeSdks

use of com.android.tools.idea.sdk.IdeSdks in project android by JetBrains.

the class AndroidSdkInitializer method run.

@Override
public void run() {
    if (!isAndroidSdkManagerEnabled()) {
        return;
    }
    // If running in a GUI test we don't want the "Select SDK" dialog to show up when running GUI tests.
    if (isGuiTestingMode()) {
        // This is good enough. Later on in the GUI test we'll validate the given SDK path.
        return;
    }
    IdeSdks ideSdks = IdeSdks.getInstance();
    File androidSdkPath = ideSdks.getAndroidSdkPath();
    if (androidSdkPath == null) {
        try {
            // Setup JDK and Android SDK if necessary
            setUpSdks();
            androidSdkPath = ideSdks.getAndroidSdkPath();
        } catch (Exception e) {
            LOG.error("Unexpected error while setting up SDKs: ", e);
        }
    }
    if (androidSdkPath != null) {
        AndroidSdkHandler handler = AndroidSdkHandler.getInstance(androidSdkPath);
        new PatchInstallingRestarter(handler, FileOpUtils.create()).restartAndInstallIfNecessary();
        // We need to start the system info monitoring even in case when user never
        // runs a single emulator instance: e.g., incompatible hypervisor might be
        // the reason why emulator is never run, and that's exactly the data
        // SystemInfoStatsMonitor collects
        new SystemInfoStatsMonitor().start();
    }
}
Also used : IdeSdks(com.android.tools.idea.sdk.IdeSdks) PatchInstallingRestarter(com.android.tools.idea.sdk.install.patch.PatchInstallingRestarter) SystemInfoStatsMonitor(com.android.tools.idea.sdk.SystemInfoStatsMonitor) AndroidSdkHandler(com.android.sdklib.repository.AndroidSdkHandler) File(java.io.File) IOException(java.io.IOException)

Aggregations

IdeSdks (com.android.tools.idea.sdk.IdeSdks)7 File (java.io.File)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 Sdk (com.intellij.openapi.projectRoots.Sdk)2 AndroidSdkHandler (com.android.sdklib.repository.AndroidSdkHandler)1 TestUtils.getSdk (com.android.testutils.TestUtils.getSdk)1 TestUtils.getWorkspaceFile (com.android.testutils.TestUtils.getWorkspaceFile)1 GradleBuildFile (com.android.tools.idea.gradle.parser.GradleBuildFile)1 GradleUtil.getGradleBuildFile (com.android.tools.idea.gradle.util.GradleUtil.getGradleBuildFile)1 LocalProperties (com.android.tools.idea.gradle.util.LocalProperties)1 ValidationResult (com.android.tools.idea.npw.WizardUtils.ValidationResult)1 SystemInfoStatsMonitor (com.android.tools.idea.sdk.SystemInfoStatsMonitor)1 PatchInstallingRestarter (com.android.tools.idea.sdk.install.patch.PatchInstallingRestarter)1 FirstRunWizardMode (com.android.tools.idea.welcome.config.FirstRunWizardMode)1 SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)1 VfsUtil.findFileByIoFile (com.intellij.openapi.vfs.VfsUtil.findFileByIoFile)1 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)1 PsiFile (com.intellij.psi.PsiFile)1 IOException (java.io.IOException)1 GuiTask (org.fest.swing.edt.GuiTask)1