Search in sources :

Example 1 with AndroidSdkAdditionalData

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

the class DependenciesModuleSetupStep method addExtraSdkLibrariesAsDependencies.

/**
   * Sets the 'useLibrary' libraries or SDK add-ons as library dependencies.
   * <p>
   * These libraries are set at the project level, which makes it impossible to add them to a IDE SDK definition because the IDE SDK is
   * global to the whole IDE. To work around this limitation, we set these libraries as module dependencies instead.
   * </p>
   */
private void addExtraSdkLibrariesAsDependencies(@NotNull Module module, @NotNull IdeModifiableModelsProvider modelsProvider, @NotNull AndroidProject androidProject) {
    ModifiableRootModel moduleModel = modelsProvider.getModifiableRootModel(module);
    Sdk sdk = moduleModel.getSdk();
    // If we got here, SDK will *NOT* be null.
    assert sdk != null;
    String suffix = null;
    AndroidSdkData sdkData = AndroidSdkData.getSdkData(sdk);
    if (sdkData != null) {
        SdkAdditionalData data = sdk.getSdkAdditionalData();
        if (data instanceof AndroidSdkAdditionalData) {
            AndroidSdkAdditionalData androidSdkData = (AndroidSdkAdditionalData) data;
            suffix = androidSdkData.getBuildTargetHashString();
        }
    }
    if (suffix == null) {
        // In practice, we won't get here. A proper Android SDK has been already configured by now, and the suffix won't be null.
        suffix = androidProject.getCompileTarget();
    }
    Set<String> currentIdeSdkFilePaths = Sets.newHashSetWithExpectedSize(5);
    for (VirtualFile sdkFile : sdk.getRootProvider().getFiles(CLASSES)) {
        // We need to convert the VirtualFile to java.io.File, because the path of the VirtualPath is using 'jar' protocol and it won't match
        // the path returned by AndroidProject#getBootClasspath().
        File sdkFilePath = virtualToIoFile(sdkFile);
        currentIdeSdkFilePaths.add(sdkFilePath.getPath());
    }
    Collection<String> bootClasspath = androidProject.getBootClasspath();
    for (String library : bootClasspath) {
        if (isNotEmpty(library) && !currentIdeSdkFilePaths.contains(library)) {
            // Library is not in the SDK IDE definition. Add it as library and make the module depend on it.
            File binaryPath = new File(library);
            String name = binaryPath.isFile() ? getNameWithoutExtension(binaryPath) : sanitizeFileName(library);
            // Include compile target as part of the name, to ensure the library name is unique to this Android platform.
            // e.g. maps-android-23, effects-android-23 (it follows the library naming convention: library-version
            name = name + "-" + suffix;
            myDependenciesSetup.setUpLibraryDependency(module, modelsProvider, name, COMPILE, binaryPath);
        }
    }
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) VirtualFile(com.intellij.openapi.vfs.VirtualFile) AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData) AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) Sdk(com.intellij.openapi.projectRoots.Sdk) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File) SdkAdditionalData(com.intellij.openapi.projectRoots.SdkAdditionalData) AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData)

Example 2 with AndroidSdkAdditionalData

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

the class AndroidSdks method setUpSdk.

public void setUpSdk(@NotNull Sdk androidSdk, @NotNull SdkModificator androidSdkModificator, @NotNull IAndroidTarget target, @NotNull String sdkName, @NotNull Collection<Sdk> allSdks, @Nullable Sdk jdk, boolean addRoots) {
    AndroidSdkAdditionalData data = new AndroidSdkAdditionalData(androidSdk, jdk);
    data.setBuildTarget(target);
    String name = createUniqueSdkName(sdkName, allSdks);
    androidSdkModificator.setName(name);
    if (jdk != null) {
        androidSdkModificator.setVersionString(jdk.getVersionString());
    }
    androidSdkModificator.setSdkAdditionalData(data);
    if (addRoots) {
        List<OrderRoot> newRoots = getLibraryRootsForTarget(target, androidSdk, true);
        androidSdkModificator.removeAllRoots();
        for (OrderRoot orderRoot : newRoots) {
            androidSdkModificator.addRoot(orderRoot.getFile(), orderRoot.getType());
        }
        // TODO move this method to Jdks.
        attachJdkAnnotations(androidSdkModificator);
    }
    // Add sources at the end, otherwise if 'addRoots' is true, removing all existing roots will delete sources as well.
    // https://code.google.com/p/android/issues/detail?id=233221
    findAndSetPlatformSources(target, androidSdkModificator);
}
Also used : AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData) OrderRoot(com.intellij.openapi.roots.libraries.ui.OrderRoot)

Example 3 with AndroidSdkAdditionalData

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

the class AndroidSdks method findSuitableAndroidSdk.

@Nullable
public Sdk findSuitableAndroidSdk(@NotNull String targetHash) {
    Set<String> foundSdkHomePaths = new HashSet<>();
    List<Sdk> notCompatibleSdks = new ArrayList<>();
    for (Sdk sdk : getAllAndroidSdks()) {
        AndroidSdkAdditionalData originalData = getAndroidSdkAdditionalData(sdk);
        if (originalData == null) {
            continue;
        }
        String sdkHomePath = sdk.getHomePath();
        if (!foundSdkHomePaths.contains(sdkHomePath) && targetHash.equals(originalData.getBuildTargetHashString())) {
            if (VersionCheck.isCompatibleVersion(sdkHomePath)) {
                return sdk;
            }
            notCompatibleSdks.add(sdk);
            if (sdkHomePath != null) {
                foundSdkHomePaths.add(sdkHomePath);
            }
        }
    }
    return notCompatibleSdks.isEmpty() ? null : notCompatibleSdks.get(0);
}
Also used : AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData) Sdk(com.intellij.openapi.projectRoots.Sdk) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with AndroidSdkAdditionalData

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

the class GradleSyncTest method sdkCreationForAddons.

// https://code.google.com/p/android/issues/detail?id=185313
@Test
public void sdkCreationForAddons() throws IOException {
    guiTest.importSimpleApplication();
    IdeFrameFixture ideFrame = guiTest.ideFrame();
    Project project = ideFrame.getProject();
    Module appModule = ideFrame.getModule("app");
    GradleBuildFile buildFile = GradleBuildFile.get(appModule);
    execute(new GuiTask() {

        @Override
        protected void executeInEDT() throws Throwable {
            runWriteCommandAction(project, () -> buildFile.setValue(BuildFileKey.COMPILE_SDK_VERSION, "Google Inc.:Google APIs:24"));
        }
    });
    ideFrame.requestProjectSync().waitForGradleProjectSyncToFinish();
    Sdk sdk = ModuleRootManager.getInstance(appModule).getSdk();
    AndroidSdkData sdkData = AndroidSdkData.getSdkData(sdk);
    SdkAdditionalData data = sdk.getSdkAdditionalData();
    assertThat(data).isInstanceOf(AndroidSdkAdditionalData.class);
    AndroidSdkAdditionalData androidSdkData = (AndroidSdkAdditionalData) data;
    IAndroidTarget buildTarget = androidSdkData.getBuildTarget(sdkData);
    // By checking that there are no additional libraries in the SDK, we are verifying that an additional SDK was not created for add-ons.
    assertThat(buildTarget.getAdditionalLibraries()).hasSize(0);
}
Also used : GuiTask(org.fest.swing.edt.GuiTask) Project(com.intellij.openapi.project.Project) AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData) AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) GradleUtil.getGradleBuildFile(com.android.tools.idea.gradle.util.GradleUtil.getGradleBuildFile) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) IAndroidTarget(com.android.sdklib.IAndroidTarget) Module(com.intellij.openapi.module.Module) AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData) SdkAdditionalData(com.intellij.openapi.projectRoots.SdkAdditionalData) Test(org.junit.Test)

Example 5 with AndroidSdkAdditionalData

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

the class AndroidJunitPatcher method patchJavaParameters.

@Override
public void patchJavaParameters(@Nullable Module module, JavaParameters javaParameters) {
    if (module == null) {
        return;
    }
    AndroidModuleModel androidModel = AndroidModuleModel.get(module);
    if (androidModel == null) {
        return;
    }
    // Modify the class path only if we're dealing with the unit test artifact.
    PathsList classPath = javaParameters.getClassPath();
    Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
    if (sdk == null || !(sdk.getSdkType() instanceof AndroidSdkType)) {
        return;
    }
    SdkAdditionalData data = sdk.getSdkAdditionalData();
    if (!(data instanceof AndroidSdkAdditionalData)) {
        return;
    }
    AndroidPlatform platform = ((AndroidSdkAdditionalData) data).getAndroidPlatform();
    if (platform == null) {
        return;
    }
    classPath.remove(platform.getTarget().getPath(IAndroidTarget.ANDROID_JAR));
    // Move the mockable android jar to the end.
    String mockableJarPath = null;
    for (String path : classPath.getPathList()) {
        if (new File(FileUtil.toSystemDependentName(path)).getName().startsWith("mockable-android")) {
            // PathsList stores strings - use the one that's actually stored there.
            mockableJarPath = path;
            break;
        }
    }
    if (mockableJarPath != null) {
        classPath.remove(mockableJarPath);
        classPath.addTail(mockableJarPath);
    }
}
Also used : AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData) PathsList(com.intellij.util.PathsList) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) AndroidSdkType(org.jetbrains.android.sdk.AndroidSdkType) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) Sdk(com.intellij.openapi.projectRoots.Sdk) File(java.io.File) SdkAdditionalData(com.intellij.openapi.projectRoots.SdkAdditionalData) AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData)

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