Search in sources :

Example 1 with ILogger

use of com.android.utils.ILogger in project android by JetBrains.

the class FrameworkResourceLoader method load.

@Nullable
public static FrameworkResources load(@NotNull IAndroidTarget myTarget, boolean withLocale) throws IOException {
    final ILogger logger = new LogWrapper(LOG);
    final File resFolder = myTarget.getFile(IAndroidTarget.RESOURCES);
    if (!resFolder.isDirectory()) {
        LOG.error(AndroidBundle.message("android.directory.cannot.be.found.error", resFolder.getPath()));
        return null;
    }
    return loadPlatformResources(resFolder, logger, withLocale);
}
Also used : ILogger(com.android.utils.ILogger) LogWrapper(com.android.tools.idea.rendering.LogWrapper) IAbstractFile(com.android.io.IAbstractFile) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ILogger

use of com.android.utils.ILogger in project android by JetBrains.

the class KeystoreUtils method getOrCreateDefaultDebugKeystore.

public static File getOrCreateDefaultDebugKeystore() throws Exception {
    try {
        File debugLocation = new File(KeystoreHelper.defaultDebugKeystoreLocation());
        if (!debugLocation.exists()) {
            File keystoreDirectory = new File(AndroidLocation.getFolder());
            if (!keystoreDirectory.canWrite()) {
                throw new AndroidLocationException("Could not create debug keystore because \"" + keystoreDirectory + "\" is not writable");
            }
            ILogger logger = new StdLogger(StdLogger.Level.ERROR);
            // Default values taken from http://developer.android.com/tools/publishing/app-signing.html
            // Keystore name: "debug.keystore"
            // Keystore password: "android"
            // Keystore alias: "androiddebugkey"
            // Key password: "android"
            KeystoreHelper.createDebugStore(null, debugLocation, "android", "android", "AndroidDebugKey", logger);
        }
        if (!debugLocation.exists()) {
            throw new AndroidLocationException("Could not create debug keystore");
        }
        return debugLocation;
    } catch (AndroidLocationException exception) {
        throw new Exception("Failed to get debug keystore path", exception);
    }
}
Also used : StdLogger(com.android.utils.StdLogger) ILogger(com.android.utils.ILogger) AndroidLocationException(com.android.prefs.AndroidLocation.AndroidLocationException) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) File(java.io.File) GradleSettingsFile(com.android.tools.idea.gradle.parser.GradleSettingsFile) KeyStoreException(java.security.KeyStoreException) AndroidLocationException(com.android.prefs.AndroidLocation.AndroidLocationException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 3 with ILogger

use of com.android.utils.ILogger in project android by JetBrains.

the class LayoutLibraryLoader method load.

@Nullable
public static LayoutLibrary load(@NotNull IAndroidTarget target, @NotNull Map<String, Map<String, Integer>> enumMap) throws RenderingException, IOException {
    final String fontFolderPath = FileUtil.toSystemIndependentName((target.getPath(IAndroidTarget.FONTS)));
    final VirtualFile fontFolder = LocalFileSystem.getInstance().findFileByPath(fontFolderPath);
    if (fontFolder == null || !fontFolder.isDirectory()) {
        throw new RenderingException(LayoutlibBundle.message("android.directory.cannot.be.found.error", FileUtil.toSystemDependentName(fontFolderPath)));
    }
    final String platformFolderPath = target.isPlatform() ? target.getLocation() : target.getParent().getLocation();
    final File platformFolder = new File(platformFolderPath);
    if (!platformFolder.isDirectory()) {
        throw new RenderingException(LayoutlibBundle.message("android.directory.cannot.be.found.error", FileUtil.toSystemDependentName(platformFolderPath)));
    }
    final File buildProp = new File(platformFolder, SdkConstants.FN_BUILD_PROP);
    if (!buildProp.isFile()) {
        throw new RenderingException(LayoutlibBundle.message("android.file.not.exist.error", FileUtil.toSystemDependentName(buildProp.getPath())));
    }
    if (!SystemInfo.isJavaVersionAtLeast("1.8") && target.getVersion().getFeatureLevel() >= 24) {
        // From N, we require to be running in Java 8
        throw new UnsupportedJavaRuntimeException(LayoutlibBundle.message("android.layout.preview.unsupported.jdk", SdkVersionInfo.getCodeName(target.getVersion().getFeatureLevel())));
    }
    LayoutLibrary library;
    final ILogger logger = new LogWrapper(LOG);
    if (USE_SDK_LAYOUTLIB) {
        final String resFolderPath = FileUtil.toSystemIndependentName((target.getPath(IAndroidTarget.RESOURCES)));
        final VirtualFile resFolder = LocalFileSystem.getInstance().findFileByPath(resFolderPath);
        if (resFolder == null || !resFolder.isDirectory()) {
            throw new RenderingException(LayoutlibBundle.message("android.directory.cannot.be.found.error", FileUtil.toSystemDependentName(resFolderPath)));
        }
        final String layoutLibJarPath = FileUtil.toSystemIndependentName((target.getPath(IAndroidTarget.LAYOUT_LIB)));
        final VirtualFile layoutLibJar = LocalFileSystem.getInstance().findFileByPath(layoutLibJarPath);
        if (layoutLibJar == null || layoutLibJar.isDirectory()) {
            throw new RenderingException(LayoutlibBundle.message("android.file.not.exist.error", FileUtil.toSystemDependentName(layoutLibJarPath)));
        }
        library = LayoutLibrary.load(layoutLibJar.getPath(), logger, ApplicationNamesInfo.getInstance().getFullProductName());
    } else {
        // We instantiate the local Bridge implementation and pass it to the LayoutLibrary instance
        library = LayoutLibrary.load(new Bridge(), LayoutLibraryLoader.class.getClassLoader());
    }
    if (library.getStatus() != LoadStatus.LOADED) {
        throw new RenderingException(library.getLoadMessage());
    }
    final Map<String, String> buildPropMap = ProjectProperties.parsePropertyFile(new BufferingFileWrapper(buildProp), logger);
    final LayoutLog layoutLog = new LayoutLogWrapper(LOG);
    if (library.init(buildPropMap, new File(fontFolder.getPath()), enumMap, layoutLog)) {
        return library;
    } else {
        return null;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) BufferingFileWrapper(org.jetbrains.android.util.BufferingFileWrapper) LayoutLibrary(com.android.ide.common.rendering.LayoutLibrary) ILogger(com.android.utils.ILogger) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) Bridge(com.android.layoutlib.bridge.Bridge) LayoutLog(com.android.ide.common.rendering.api.LayoutLog) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with ILogger

use of com.android.utils.ILogger in project android by JetBrains.

the class ManifestInfo method getMergedManifest.

@NotNull
static MergingReport getMergedManifest(@NotNull AndroidFacet facet, @NotNull VirtualFile primaryManifestFile, @NotNull List<VirtualFile> flavorAndBuildTypeManifests, @NotNull List<VirtualFile> libManifests) throws ManifestMerger2.MergeFailureException {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    final File mainManifestFile = VfsUtilCore.virtualToIoFile(primaryManifestFile);
    ILogger logger = NullLogger.getLogger();
    ManifestMerger2.MergeType mergeType = facet.isAppProject() ? ManifestMerger2.MergeType.APPLICATION : ManifestMerger2.MergeType.LIBRARY;
    AndroidModel androidModel = facet.getAndroidModel();
    AndroidModuleModel gradleModel = AndroidModuleModel.get(facet);
    ManifestMerger2.Invoker manifestMergerInvoker = ManifestMerger2.newMerger(mainManifestFile, logger, mergeType);
    manifestMergerInvoker.withFeatures(ManifestMerger2.Invoker.Feature.SKIP_BLAME, ManifestMerger2.Invoker.Feature.SKIP_XML_STRING);
    manifestMergerInvoker.addFlavorAndBuildTypeManifests(VfsUtilCore.virtualToIoFiles(flavorAndBuildTypeManifests).toArray(new File[0]));
    List<Pair<String, File>> libraryManifests = new ArrayList<>();
    for (VirtualFile file : libManifests) {
        libraryManifests.add(Pair.of(file.getName(), VfsUtilCore.virtualToIoFile(file)));
    }
    manifestMergerInvoker.addBundleManifests(libraryManifests);
    if (androidModel != null) {
        AndroidVersion minSdkVersion = androidModel.getMinSdkVersion();
        if (minSdkVersion != null) {
            manifestMergerInvoker.setOverride(ManifestSystemProperty.MIN_SDK_VERSION, minSdkVersion.getApiString());
        }
        AndroidVersion targetSdkVersion = androidModel.getTargetSdkVersion();
        if (targetSdkVersion != null) {
            manifestMergerInvoker.setOverride(ManifestSystemProperty.TARGET_SDK_VERSION, targetSdkVersion.getApiString());
        }
        Integer versionCode = androidModel.getVersionCode();
        if (versionCode != null && versionCode > 0) {
            manifestMergerInvoker.setOverride(ManifestSystemProperty.VERSION_CODE, String.valueOf(versionCode));
        }
        String packageOverride = androidModel.getApplicationId();
        if (!Strings.isNullOrEmpty(packageOverride)) {
            manifestMergerInvoker.setOverride(ManifestSystemProperty.PACKAGE, packageOverride);
        }
    }
    if (gradleModel != null) {
        BuildTypeContainer buildTypeContainer = gradleModel.findBuildType(gradleModel.getSelectedVariant().getBuildType());
        assert buildTypeContainer != null;
        BuildType buildType = buildTypeContainer.getBuildType();
        ProductFlavor mergedProductFlavor = gradleModel.getSelectedVariant().getMergedFlavor();
        // copy-paste from {@link VariantConfiguration#getManifestPlaceholders()}
        Map<String, Object> placeHolders = new HashMap<>(mergedProductFlavor.getManifestPlaceholders());
        placeHolders.putAll(buildType.getManifestPlaceholders());
        manifestMergerInvoker.setPlaceHolderValues(placeHolders);
        // @deprecated maxSdkVersion has been ignored since Android 2.1 (API level 7)
        Integer maxSdkVersion = mergedProductFlavor.getMaxSdkVersion();
        if (maxSdkVersion != null) {
            manifestMergerInvoker.setOverride(ManifestSystemProperty.MAX_SDK_VERSION, maxSdkVersion.toString());
        }
        // TODO we should have version Name for non-gradle projects
        // copy-paste from {@link VariantConfiguration#getVersionName()}
        String versionName = mergedProductFlavor.getVersionName();
        String flavorVersionNameSuffix = null;
        if (gradleModel.getFeatures().isProductFlavorVersionSuffixSupported()) {
            flavorVersionNameSuffix = getVersionNameSuffix(mergedProductFlavor);
        }
        String versionNameSuffix = Joiner.on("").skipNulls().join(flavorVersionNameSuffix, getVersionNameSuffix(buildType));
        if (!Strings.isNullOrEmpty(versionName) || !Strings.isNullOrEmpty(versionNameSuffix)) {
            if (Strings.isNullOrEmpty(versionName)) {
                Manifest manifest = facet.getManifest();
                if (manifest != null) {
                    versionName = manifest.getXmlTag().getAttributeValue(SdkConstants.ATTR_VERSION_NAME, ANDROID_URI);
                }
            }
            if (!Strings.isNullOrEmpty(versionNameSuffix)) {
                versionName = Strings.nullToEmpty(versionName) + versionNameSuffix;
            }
            manifestMergerInvoker.setOverride(ManifestSystemProperty.VERSION_NAME, versionName);
        }
    }
    if (mergeType == ManifestMerger2.MergeType.APPLICATION) {
        manifestMergerInvoker.withFeatures(ManifestMerger2.Invoker.Feature.REMOVE_TOOLS_DECLARATIONS);
    }
    final Module module = facet.getModule();
    final Project project = module.getProject();
    manifestMergerInvoker.withFileStreamProvider(new ManifestMerger2.FileStreamProvider() {

        @Override
        protected InputStream getInputStream(@NotNull File file) throws FileNotFoundException {
            VirtualFile vFile;
            if (file == mainManifestFile) {
                // Some tests use VirtualFile files (e.g. temp:///src/AndroidManifest.xml) for the main manifest
                vFile = primaryManifestFile;
            } else {
                vFile = VfsUtil.findFileByIoFile(file, false);
            }
            assert vFile != null : file;
            // findModuleForFile does not work for other build systems (e.g. bazel)
            if (!libManifests.isEmpty()) {
                Module moduleContainingManifest = getAndroidModuleForManifest(vFile);
                if (moduleContainingManifest != null && !module.equals(moduleContainingManifest)) {
                    MergedManifest manifest = MergedManifest.get(moduleContainingManifest);
                    Document document = manifest.getDocument();
                    if (document != null) {
                        // normally the case, but can fail on merge fail
                        // This is not very efficient. Consider enhancing the manifest merger API
                        // such that I can pass back a fully merged DOM document instead of
                        // an XML string since it will need to turn around and parse it anyway.
                        String text = XmlUtils.toXml(document);
                        return new ByteArrayInputStream(text.getBytes(Charsets.UTF_8));
                    }
                }
            }
            try {
                PsiFile psiFile = PsiManager.getInstance(project).findFile(vFile);
                if (psiFile != null) {
                    String text = psiFile.getText();
                    return new ByteArrayInputStream(text.getBytes(Charsets.UTF_8));
                }
            } catch (ProcessCanceledException ignore) {
            // During startup we may receive a progress canceled exception here,
            // but we don't *need* to read from PSI; we can read directly from
            // disk. PSI is useful when the file has been modified, but that's not
            // the case in the typical scenario where we hit process canceled.
            }
            return super.getInputStream(file);
        }

        @Nullable
        private Module getAndroidModuleForManifest(@NotNull VirtualFile vFile) {
            // project and look at their source providers
            for (Module m : ModuleManager.getInstance(project).getModules()) {
                AndroidFacet androidFacet = AndroidFacet.getInstance(m);
                if (androidFacet == null) {
                    continue;
                }
                List<VirtualFile> manifestFiles = IdeaSourceProvider.getManifestFiles(androidFacet);
                for (VirtualFile manifestFile : manifestFiles) {
                    if (vFile.equals(manifestFile)) {
                        return m;
                    }
                }
            }
            return null;
        }
    });
    return manifestMergerInvoker.merge();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileNotFoundException(java.io.FileNotFoundException) Document(org.w3c.dom.Document) ILogger(com.android.utils.ILogger) PsiFile(com.intellij.psi.PsiFile) ImmutableList(com.google.common.collect.ImmutableList) Pair(com.android.utils.Pair) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AndroidVersion(com.android.sdklib.AndroidVersion) Manifest(org.jetbrains.android.dom.manifest.Manifest) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) ByteArrayInputStream(java.io.ByteArrayInputStream) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with ILogger

use of com.android.utils.ILogger in project android by JetBrains.

the class FileResourceRepository method createResourceMerger.

private static ResourceMerger createResourceMerger(File file, String libraryName) {
    ILogger logger = new LogWrapper(LOG);
    ResourceMerger merger = new ResourceMerger(0);
    ResourceSet resourceSet = new ResourceSet(file.getName(), libraryName, false);
    resourceSet.addSource(file);
    resourceSet.setTrackSourcePositions(false);
    try {
        resourceSet.loadFromFiles(logger);
    } catch (DuplicateDataException e) {
        // This should not happen; resourceSet validation is disabled.
        assert false;
    } catch (MergingException e) {
        LOG.warn(e);
    }
    merger.addDataSet(resourceSet);
    return merger;
}
Also used : ILogger(com.android.utils.ILogger) LogWrapper(com.android.tools.idea.rendering.LogWrapper)

Aggregations

ILogger (com.android.utils.ILogger)5 File (java.io.File)4 Nullable (org.jetbrains.annotations.Nullable)3 LogWrapper (com.android.tools.idea.rendering.LogWrapper)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 LayoutLibrary (com.android.ide.common.rendering.LayoutLibrary)1 LayoutLog (com.android.ide.common.rendering.api.LayoutLog)1 IAbstractFile (com.android.io.IAbstractFile)1 Bridge (com.android.layoutlib.bridge.Bridge)1 AndroidLocationException (com.android.prefs.AndroidLocation.AndroidLocationException)1 AndroidVersion (com.android.sdklib.AndroidVersion)1 GradleBuildFile (com.android.tools.idea.gradle.parser.GradleBuildFile)1 GradleSettingsFile (com.android.tools.idea.gradle.parser.GradleSettingsFile)1 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)1 Pair (com.android.utils.Pair)1 StdLogger (com.android.utils.StdLogger)1 ImmutableList (com.google.common.collect.ImmutableList)1 Module (com.intellij.openapi.module.Module)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 Project (com.intellij.openapi.project.Project)1