Search in sources :

Example 1 with AndroidSdks

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

the class AndroidSdkUtils method findAppropriateAndroidPlatform.

@Nullable
public static Sdk findAppropriateAndroidPlatform(@NotNull IAndroidTarget target, @NotNull AndroidSdkData sdkData, boolean forMaven) {
    for (Sdk sdk : ProjectJdkTable.getInstance().getAllJdks()) {
        String homePath = sdk.getHomePath();
        AndroidSdks androidSdks = AndroidSdks.getInstance();
        if (homePath != null && androidSdks.isAndroidSdk(sdk)) {
            AndroidSdkData currentSdkData = getSdkData(homePath);
            if (sdkData.equals(currentSdkData)) {
                AndroidSdkAdditionalData data = androidSdks.getAndroidSdkAdditionalData(sdk);
                if (data != null) {
                    IAndroidTarget currentTarget = data.getBuildTarget(currentSdkData);
                    if (currentTarget != null && target.hashString().equals(currentTarget.hashString()) && checkSdkRoots(sdk, target, forMaven)) {
                        return sdk;
                    }
                }
            }
        }
    }
    return null;
}
Also used : AndroidSdks(com.android.tools.idea.sdk.AndroidSdks) ModuleRootModificationUtil.setModuleSdk(com.intellij.openapi.roots.ModuleRootModificationUtil.setModuleSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) IAndroidTarget(com.android.sdklib.IAndroidTarget) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with AndroidSdks

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

the class AndroidSdkUtils method checkSdkRoots.

public static boolean checkSdkRoots(@NotNull Sdk sdk, @NotNull IAndroidTarget target, boolean forMaven) {
    String homePath = sdk.getHomePath();
    if (homePath == null) {
        return false;
    }
    AndroidSdks androidSdks = AndroidSdks.getInstance();
    AndroidSdkAdditionalData sdkAdditionalData = androidSdks.getAndroidSdkAdditionalData(sdk);
    Sdk javaSdk = sdkAdditionalData != null ? sdkAdditionalData.getJavaSdk() : null;
    if (javaSdk == null) {
        return false;
    }
    Set<VirtualFile> filesInSdk = Sets.newHashSet(sdk.getRootProvider().getFiles(CLASSES));
    List<VirtualFile> platformAndAddOnJars = androidSdks.getPlatformAndAddOnJars(target);
    for (VirtualFile file : platformAndAddOnJars) {
        if (filesInSdk.contains(file) == forMaven) {
            return false;
        }
    }
    boolean containsJarFromJdk = false;
    for (VirtualFile file : javaSdk.getRootProvider().getFiles(CLASSES)) {
        if (file.getFileType() instanceof ArchiveFileType && filesInSdk.contains(file)) {
            containsJarFromJdk = true;
        }
    }
    return containsJarFromJdk == forMaven;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AndroidSdks(com.android.tools.idea.sdk.AndroidSdks) ArchiveFileType(com.intellij.ide.highlighter.ArchiveFileType) ModuleRootModificationUtil.setModuleSdk(com.intellij.openapi.roots.ModuleRootModificationUtil.setModuleSdk) Sdk(com.intellij.openapi.projectRoots.Sdk)

Example 3 with AndroidSdks

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

the class AndroidTestBase method ensureSdkManagerAvailable.

protected void ensureSdkManagerAvailable() {
    AndroidSdks androidSdks = AndroidSdks.getInstance();
    AndroidSdkData sdkData = androidSdks.tryToChooseAndroidSdk();
    if (sdkData == null) {
        sdkData = createTestSdkManager();
        if (sdkData != null) {
            androidSdks.setSdkData(sdkData);
        }
    }
    assertNotNull(sdkData);
}
Also used : AndroidSdks(com.android.tools.idea.sdk.AndroidSdks)

Example 4 with AndroidSdks

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

the class VfsInstallListener method doRefresh.

private static void doRefresh(@NonNull PackageOperation op, @NonNull ProgressIndicator progress) {
    VirtualFile file = VfsUtil.findFileByIoFile(op.getLocation(progress), false);
    if (file != null) {
        file.refresh(false, true);
    }
    // Note that this must be done asynchronously, and must be done separately from the refresh above:
    // The above refresh may note that jar files are added or deleted, which then kicks off an asynchronous
    // refresh of jar-rooted filesystems. We queue a dummy refresh to run afterward, with our desired logic
    // as the finishRunnable.
    RefreshQueue.getInstance().refresh(true, false, () -> {
        AndroidSdks androidSdks = AndroidSdks.getInstance();
        for (Sdk sdk : androidSdks.getAllAndroidSdks()) {
            androidSdks.refreshLibrariesIn(sdk);
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AndroidSdks(com.android.tools.idea.sdk.AndroidSdks) Sdk(com.intellij.openapi.projectRoots.Sdk)

Example 5 with AndroidSdks

use of com.android.tools.idea.sdk.AndroidSdks 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)

Aggregations

AndroidSdks (com.android.tools.idea.sdk.AndroidSdks)6 Sdk (com.intellij.openapi.projectRoots.Sdk)5 ModuleRootModificationUtil.setModuleSdk (com.intellij.openapi.roots.ModuleRootModificationUtil.setModuleSdk)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 AndroidSdkAdditionalData (org.jetbrains.android.sdk.AndroidSdkAdditionalData)2 Nullable (org.jetbrains.annotations.Nullable)2 IAndroidTarget (com.android.sdklib.IAndroidTarget)1 ArchiveFileType (com.intellij.ide.highlighter.ArchiveFileType)1 JavaSdk (com.intellij.openapi.projectRoots.JavaSdk)1 SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)1 OrderRoot (com.intellij.openapi.roots.libraries.ui.OrderRoot)1