Search in sources :

Example 11 with AndroidSdkAdditionalData

use of org.jetbrains.android.sdk.AndroidSdkAdditionalData 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 12 with AndroidSdkAdditionalData

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

the class AndroidSdkInitializer method setUpSdks.

private static void setUpSdks() {
    Sdk sdk = findFirstCompatibleAndroidSdk();
    if (sdk != null) {
        String sdkHomePath = sdk.getHomePath();
        assert sdkHomePath != null;
        IdeSdks.getInstance().createAndroidSdkPerAndroidTarget(new File(toSystemDependentName(sdkHomePath)));
        return;
    }
    // Called in a 'invokeLater' block, otherwise file chooser will hang forever.
    ApplicationManager.getApplication().invokeLater(() -> {
        File androidSdkPath = findOrGetAndroidSdkPath();
        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 newSdk = createNewAndroidPlatform(androidSdkPath.getPath(), promptSdkSelection);
        if (newSdk != null) {
            // Rename the SDK to fit our default naming convention.
            String sdkNamePrefix = AndroidSdks.SDK_NAME_PREFIX;
            if (newSdk.getName().startsWith(sdkNamePrefix)) {
                SdkModificator sdkModificator = newSdk.getSdkModificator();
                sdkModificator.setName(sdkNamePrefix + newSdk.getName().substring(sdkNamePrefix.length()));
                sdkModificator.commitChanges();
                // Rename the JDK that goes along with this SDK.
                AndroidSdkAdditionalData additionalData = AndroidSdks.getInstance().getAndroidSdkAdditionalData(newSdk);
                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.getInstance().createAndroidSdkPerAndroidTarget(androidSdkPath);
            }
        }
    });
}
Also used : AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData) FirstRunWizardMode(com.android.tools.idea.welcome.config.FirstRunWizardMode) Sdk(com.intellij.openapi.projectRoots.Sdk) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) File(java.io.File)

Example 13 with AndroidSdkAdditionalData

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

the class AndroidFacetImporterBase method createNewAndroidSdkForMaven.

@Nullable
private static Sdk createNewAndroidSdkForMaven(File sdkPath, IAndroidTarget target) {
    AndroidSdks androidSdks = AndroidSdks.getInstance();
    Sdk sdk = null;
    Sdk jdk = Jdks.getInstance().chooseOrCreateJavaSdk();
    if (jdk != null) {
        String sdkName = "Maven " + androidSdks.chooseNameForNewLibrary(target);
        sdk = androidSdks.create(target, sdkPath, sdkName, jdk, false);
    }
    if (sdk == null) {
        return null;
    }
    SdkModificator modificator = sdk.getSdkModificator();
    for (OrderRoot root : androidSdks.getLibraryRootsForTarget(target, sdkPath, false)) {
        modificator.addRoot(root.getFile(), root.getType());
    }
    AndroidSdkAdditionalData data = androidSdks.getAndroidSdkAdditionalData(sdk);
    if (data != null) {
        final Sdk javaSdk = data.getJavaSdk();
        if (javaSdk != null) {
            for (VirtualFile file : javaSdk.getRootProvider().getFiles(OrderRootType.CLASSES)) {
                modificator.addRoot(file, OrderRootType.CLASSES);
            }
        } else {
            LOG.error("AndroidSdkUtils.createNewAndroidPlatform should return Android SDK with a valid JDK reference, or return null");
        }
    }
    modificator.commitChanges();
    return sdk;
}
Also used : AndroidSdks(com.android.tools.idea.sdk.AndroidSdks) AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData) OrderRoot(com.intellij.openapi.roots.libraries.ui.OrderRoot) Sdk(com.intellij.openapi.projectRoots.Sdk) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with AndroidSdkAdditionalData

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

the class SdksCleanupStep method cleanUpSdk.

public void cleanUpSdk(@NotNull Module module, @NotNull Collection<Sdk> invalidAndroidSdks) {
    AndroidFacet androidFacet = AndroidFacet.getInstance(module);
    if (androidFacet == null || androidFacet.getAndroidModel() == null) {
        return;
    }
    Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
    if (sdk != null && !invalidAndroidSdks.contains(sdk) && (isMissingAndroidLibrary(sdk) || shouldRemoveAnnotationsJar(sdk))) {
        // First try to recreate SDK; workaround for issue 78072
        AndroidSdkAdditionalData additionalData = myAndroidSdks.getAndroidSdkAdditionalData(sdk);
        AndroidSdkData sdkData = AndroidSdkData.getSdkData(sdk);
        if (additionalData != null && sdkData != null) {
            IAndroidTarget target = additionalData.getBuildTarget(sdkData);
            if (target == null) {
                AndroidSdkHandler sdkHandler = sdkData.getSdkHandler();
                StudioLoggerProgressIndicator logger = new StudioLoggerProgressIndicator(getClass());
                sdkHandler.getSdkManager(logger).loadSynchronously(0, logger, null, null);
                target = sdkHandler.getAndroidTargetManager(logger).getTargetFromHashString(additionalData.getBuildTargetHashString(), logger);
            }
            if (target != null) {
                SdkModificator sdkModificator = sdk.getSdkModificator();
                sdkModificator.removeAllRoots();
                for (OrderRoot orderRoot : myAndroidSdks.getLibraryRootsForTarget(target, sdk, true)) {
                    sdkModificator.addRoot(orderRoot.getFile(), orderRoot.getType());
                }
                attachJdkAnnotations(sdkModificator);
                sdkModificator.commitChanges();
            }
        }
        // above workaround deals with)
        if (isMissingAndroidLibrary(sdk)) {
            invalidAndroidSdks.add(sdk);
        }
    }
}
Also used : StudioLoggerProgressIndicator(com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator) AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData) AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) AndroidSdkHandler(com.android.sdklib.repository.AndroidSdkHandler) OrderRoot(com.intellij.openapi.roots.libraries.ui.OrderRoot) Sdk(com.intellij.openapi.projectRoots.Sdk) IAndroidTarget(com.android.sdklib.IAndroidTarget) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 15 with AndroidSdkAdditionalData

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

the class RenderErrorContributor method isBuiltByJdk7OrHigher.

static boolean isBuiltByJdk7OrHigher(@NotNull Module module) {
    Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
    if (sdk == null) {
        return false;
    }
    AndroidSdks androidSdks = AndroidSdks.getInstance();
    if (androidSdks.isAndroidSdk(sdk)) {
        AndroidSdkAdditionalData data = androidSdks.getAndroidSdkAdditionalData(sdk);
        if (data != null) {
            Sdk jdk = data.getJavaSdk();
            if (jdk != null) {
                sdk = jdk;
            }
        }
    }
    return sdk.getSdkType() instanceof JavaSdk && JavaSdk.getInstance().isOfVersionOrHigher(sdk, JavaSdkVersion.JDK_1_7);
}
Also used : AndroidSdks(com.android.tools.idea.sdk.AndroidSdks) AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) Sdk(com.intellij.openapi.projectRoots.Sdk)

Aggregations

AndroidSdkAdditionalData (org.jetbrains.android.sdk.AndroidSdkAdditionalData)22 Sdk (com.intellij.openapi.projectRoots.Sdk)16 IAndroidTarget (com.android.sdklib.IAndroidTarget)8 File (java.io.File)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)5 AndroidSdkData (org.jetbrains.android.sdk.AndroidSdkData)5 AndroidTargetHash.getTargetHashString (com.android.sdklib.AndroidTargetHash.getTargetHashString)4 Nullable (org.jetbrains.annotations.Nullable)4 SdkPaths.validateAndroidSdk (com.android.tools.idea.sdk.SdkPaths.validateAndroidSdk)3 SdkAdditionalData (com.intellij.openapi.projectRoots.SdkAdditionalData)3 OrderRoot (com.intellij.openapi.roots.libraries.ui.OrderRoot)3 AndroidPlatform (org.jetbrains.android.sdk.AndroidPlatform)3 AndroidSdks (com.android.tools.idea.sdk.AndroidSdks)2 FirstRunWizardMode (com.android.tools.idea.welcome.config.FirstRunWizardMode)2 JavaSdk (com.intellij.openapi.projectRoots.JavaSdk)2 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)2 AndroidVersion (com.android.sdklib.AndroidVersion)1 AndroidSdkHandler (com.android.sdklib.repository.AndroidSdkHandler)1 GradleBuildFile (com.android.tools.idea.gradle.parser.GradleBuildFile)1