Search in sources :

Example 6 with AndroidPlatform

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

the class ConfigureTemplateParametersStep method onProceeding.

/**
   * When finished with this step, calculate and install a bunch of values that will be used in our
   * template's <a href="http://freemarker.incubator.apache.org/docs/dgui_quickstart_basics.html">data model.</a>
   */
@Override
protected void onProceeding() {
    // canGoForward guarantees this optional value is present
    AndroidSourceSet sourceSet = getModel().getSourceSet().get();
    AndroidProjectPaths paths = sourceSet.getPaths();
    File moduleRoot = paths.getModuleRoot();
    if (moduleRoot == null) {
        getLog().error(String.format("%s failure: can't create files because module root is not found. Please report this error.", getTitle()));
        return;
    }
    // Some parameter values should be saved for later runs through this wizard, so do that first.
    for (RowEntry rowEntry : myParameterRows.values()) {
        rowEntry.accept();
    }
    // Prepare the template data-model, starting from scratch and filling in all values we know
    Map<String, Object> templateValues = getModel().getTemplateValues();
    templateValues.clear();
    for (Parameter parameter : myParameterRows.keySet()) {
        ObservableValue<?> property = myParameterRows.get(parameter).getProperty();
        if (property != null) {
            templateValues.put(parameter.id, property.get());
        }
    }
    templateValues.put(ATTR_PACKAGE_NAME, myPackageName.get());
    templateValues.put(ATTR_SOURCE_PROVIDER_NAME, sourceSet.getName());
    // Android Modules are called Gradle Projects
    templateValues.put(ATTR_IS_NEW_PROJECT, isNewModule());
    if (isNewModule()) {
        templateValues.put(ATTR_IS_LAUNCHER, true);
    }
    try {
        File sha1File = myFacet == null ? getOrCreateDefaultDebugKeystore() : getDebugKeystore(myFacet);
        templateValues.put(ATTR_DEBUG_KEYSTORE_SHA1, KeystoreUtils.sha1(sha1File));
    } catch (Exception e) {
        getLog().info("Could not compute SHA1 hash of debug keystore.", e);
        templateValues.put(ATTR_DEBUG_KEYSTORE_SHA1, "");
    }
    if (myFacet == null) {
        // If we don't have an AndroidFacet, we must have the Android Sdk info
        AndroidVersionsInfo.VersionItem buildVersion = getModel().androidSdkInfo().getValue();
        templateValues.put(ATTR_MIN_API_LEVEL, buildVersion.getApiLevel());
        templateValues.put(ATTR_MIN_API, buildVersion.getApiLevelStr());
        templateValues.put(ATTR_BUILD_API, buildVersion.getBuildApiLevel());
        templateValues.put(ATTR_BUILD_API_STRING, buildVersion.getBuildApiLevelStr());
        templateValues.put(ATTR_TARGET_API, buildVersion.getTargetApiLevel());
        templateValues.put(ATTR_TARGET_API_STRING, buildVersion.getTargetApiLevelStr());
    } else {
        AndroidPlatform platform = AndroidPlatform.getInstance(myFacet.getModule());
        if (platform != null) {
            templateValues.put(ATTR_BUILD_API, platform.getTarget().getVersion().getFeatureLevel());
            templateValues.put(ATTR_BUILD_API_STRING, getBuildApiString(platform.getTarget().getVersion()));
        }
        AndroidModuleInfo moduleInfo = AndroidModuleInfo.get(myFacet);
        AndroidVersion minSdkVersion = moduleInfo.getMinSdkVersion();
        String minSdkName = minSdkVersion.getApiString();
        templateValues.put(ATTR_MIN_API, minSdkName);
        templateValues.put(ATTR_TARGET_API, moduleInfo.getTargetSdkVersion().getApiLevel());
        templateValues.put(ATTR_MIN_API_LEVEL, minSdkVersion.getFeatureLevel());
        templateValues.put(ATTR_IS_LIBRARY_MODULE, myFacet.isLibraryProject());
        // Register application-wide settings
        String applicationPackage = AndroidPackageUtils.getPackageForApplication(myFacet);
        if (!myPackageName.get().equals(applicationPackage)) {
            templateValues.put(ATTR_APPLICATION_PACKAGE, AndroidPackageUtils.getPackageForApplication(myFacet));
        }
    }
    // Register the resource directories associated with the active source provider
    templateValues.put(ATTR_PROJECT_OUT, FileUtil.toSystemIndependentName(moduleRoot.getAbsolutePath()));
    String packageAsDir = myPackageName.get().replace('.', File.separatorChar);
    File srcDir = paths.getSrcDirectory();
    if (srcDir != null) {
        srcDir = new File(srcDir, packageAsDir);
        templateValues.put(ATTR_SRC_DIR, getRelativePath(moduleRoot, srcDir));
        templateValues.put(ATTR_SRC_OUT, FileUtil.toSystemIndependentName(srcDir.getAbsolutePath()));
    }
    File testDir = paths.getTestDirectory();
    if (testDir != null) {
        testDir = new File(testDir, packageAsDir);
        templateValues.put(ATTR_TEST_DIR, getRelativePath(moduleRoot, testDir));
        templateValues.put(ATTR_TEST_OUT, FileUtil.toSystemIndependentName(testDir.getAbsolutePath()));
    }
    File resDir = paths.getResDirectory();
    if (resDir != null) {
        templateValues.put(ATTR_RES_DIR, getRelativePath(moduleRoot, resDir));
        templateValues.put(ATTR_RES_OUT, FileUtil.toSystemIndependentName(resDir.getPath()));
    }
    File manifestDir = paths.getManifestDirectory();
    if (manifestDir != null) {
        templateValues.put(ATTR_MANIFEST_DIR, getRelativePath(moduleRoot, manifestDir));
        templateValues.put(ATTR_MANIFEST_OUT, FileUtil.toSystemIndependentName(manifestDir.getPath()));
    }
    File aidlDir = paths.getAidlDirectory();
    if (aidlDir != null) {
        templateValues.put(ATTR_AIDL_DIR, getRelativePath(moduleRoot, aidlDir));
        templateValues.put(ATTR_AIDL_OUT, FileUtil.toSystemIndependentName(aidlDir.getPath()));
    }
    templateValues.put(PROJECT_LOCATION_ID, moduleRoot.getParent());
    // We're really interested in the directory name on disk, not the module name. These will be different if you give a module the same
    // name as its containing project.
    String moduleName = moduleRoot.getName();
    templateValues.put(ATTR_MODULE_NAME, moduleName);
}
Also used : AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) AndroidVersion(com.android.sdklib.AndroidVersion) AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) AndroidVersionsInfo(com.android.tools.idea.npw.platform.AndroidVersionsInfo) File(java.io.File) AndroidSourceSet(com.android.tools.idea.npw.project.AndroidSourceSet) AndroidProjectPaths(com.android.tools.idea.npw.project.AndroidProjectPaths)

Example 7 with AndroidPlatform

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

the class AndroidJunitPatcher method patchJavaParameters.

@Override
public void patchJavaParameters(@Nullable Module module, @NotNull JavaParameters javaParameters) {
    // Only patch if the project is a Gradle project.
    if (module == null || !isBuildWithGradle(module.getProject())) {
        return;
    }
    AndroidModuleModel androidModel = AndroidModuleModel.get(module);
    if (androidModel == null) {
        return;
    }
    // Modify the class path only if we're dealing with the unit test artifact.
    JavaArtifact testArtifact = androidModel.getUnitTestArtifactInSelectedVariant();
    if (testArtifact == null) {
        return;
    }
    PathsList classPath = javaParameters.getClassPath();
    TestArtifactSearchScopes testScopes = TestArtifactSearchScopes.get(module);
    if (testScopes == null) {
        return;
    }
    // Filter the library / module dependencies that are in android test
    FileRootSearchScope excludeScope = testScopes.getUnitTestExcludeScope();
    // complexity. TODO change the {@code PathList} API.
    for (String path : classPath.getPathList()) {
        if (excludeScope.accept(new File(path))) {
            classPath.remove(path);
        }
    }
    AndroidPlatform platform = AndroidPlatform.getInstance(module);
    if (platform == null) {
        return;
    }
    String originalClassPath = classPath.getPathsString();
    try {
        handlePlatformJar(classPath, platform, testArtifact);
        handleJavaResources(module, androidModel, classPath);
    } catch (RuntimeException e) {
        throw new RuntimeException(String.format("Error patching the JUnit class path. Original class path:%n%s", originalClassPath), e);
    }
}
Also used : JavaArtifact(com.android.builder.model.JavaArtifact) PathsList(com.intellij.util.PathsList) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) File(java.io.File)

Example 8 with AndroidPlatform

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

the class ResourceResolverCache method getFrameworkResources.

/**
   * Returns a {@link LocalResourceRepository} for the framework resources based on the current configuration selection.
   *
   * @return the framework resources or {@code null} if not found.
   */
@Nullable
public ResourceRepository getFrameworkResources(@NotNull FolderConfiguration configuration, @NotNull IAndroidTarget target) {
    int apiLevel = target.getVersion().getFeatureLevel();
    LocaleQualifier locale = configuration.getLocaleQualifier();
    boolean needLocales = locale != null && !locale.hasFakeValue() || myManager.getLocale() != Locale.ANY;
    AndroidTargetData targetData = myFrameworkResources.get(apiLevel);
    if (targetData == null) {
        AndroidPlatform platform = AndroidPlatform.getInstance(myManager.getModule());
        if (platform == null) {
            return null;
        }
        // uses soft ref
        targetData = platform.getSdkData().getTargetData(target);
        myFrameworkResources.put(apiLevel, targetData);
    }
    try {
        return targetData.getFrameworkResources(needLocales);
    } catch (IOException e) {
        LOG.error(e);
    }
    return null;
}
Also used : AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) IOException(java.io.IOException) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) AndroidTargetData(org.jetbrains.android.sdk.AndroidTargetData) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with AndroidPlatform

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

the class RenderService method supportsCapability.

public static boolean supportsCapability(@NotNull final Module module, @NotNull IAndroidTarget target, @MagicConstant(flagsFromClass = Features.class) int capability) {
    Project project = module.getProject();
    AndroidPlatform platform = AndroidPlatform.getInstance(module);
    if (platform != null) {
        try {
            LayoutLibrary library = platform.getSdkData().getTargetData(target).getLayoutLibrary(project);
            if (library != null) {
                return library.supports(capability);
            }
        } catch (RenderingException | IOException e) {
        // Ignore: if service can't be found, that capability isn't available
        }
    }
    return false;
}
Also used : Project(com.intellij.openapi.project.Project) LayoutLibrary(com.android.ide.common.rendering.LayoutLibrary) RenderingException(com.android.tools.idea.layoutlib.RenderingException) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) IOException(java.io.IOException)

Example 10 with AndroidPlatform

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

the class RenderService method getLayoutLibrary.

@Nullable
public static LayoutLibrary getLayoutLibrary(@Nullable final Module module, @Nullable IAndroidTarget target) {
    if (module == null || target == null) {
        return null;
    }
    Project project = module.getProject();
    AndroidPlatform platform = AndroidPlatform.getInstance(module);
    if (platform != null) {
        try {
            return platform.getSdkData().getTargetData(target).getLayoutLibrary(project);
        } catch (RenderingException | IOException e) {
        // Ignore.
        }
    }
    return null;
}
Also used : Project(com.intellij.openapi.project.Project) RenderingException(com.android.tools.idea.layoutlib.RenderingException) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

AndroidPlatform (org.jetbrains.android.sdk.AndroidPlatform)46 Module (com.intellij.openapi.module.Module)16 IAndroidTarget (com.android.sdklib.IAndroidTarget)13 Nullable (org.jetbrains.annotations.Nullable)11 Project (com.intellij.openapi.project.Project)10 Sdk (com.intellij.openapi.projectRoots.Sdk)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)10 File (java.io.File)9 IOException (java.io.IOException)9 AndroidVersion (com.android.sdklib.AndroidVersion)8 NotNull (org.jetbrains.annotations.NotNull)6 AndroidTargetData (org.jetbrains.android.sdk.AndroidTargetData)5 LayoutLibrary (com.android.ide.common.rendering.LayoutLibrary)3 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)3 RenderingException (com.android.tools.idea.layoutlib.RenderingException)3 AndroidModuleInfo (com.android.tools.idea.model.AndroidModuleInfo)3 AndroidSdkData (org.jetbrains.android.sdk.AndroidSdkData)3 SourceProvider (com.android.builder.model.SourceProvider)2 IDevice (com.android.ddmlib.IDevice)2