Search in sources :

Example 1 with AppResourceRepository

use of com.android.tools.idea.res.AppResourceRepository in project android by JetBrains.

the class ViewLoader method loadAndParseRClass.

@VisibleForTesting
void loadAndParseRClass(@NotNull String className) throws ClassNotFoundException, InconvertibleClassError {
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("loadAndParseRClass(%s)", anonymizeClassName(className)));
    }
    Class<?> aClass = myLoadedClasses.get(className);
    AppResourceRepository appResources = AppResourceRepository.getAppResources(myModule, true);
    if (aClass == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("  The R class is not loaded.");
        }
        final ModuleClassLoader moduleClassLoader = getModuleClassLoader();
        final boolean isClassLoaded = moduleClassLoader.isClassLoaded(className);
        aClass = moduleClassLoader.loadClass(className);
        if (!isClassLoaded && aClass != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("  Class found in module %s, first time load.", anonymize(myModule)));
            }
            // This is the first time we've found the resources. The dynamic R classes generated for aar libraries are now stale and must be
            // regenerated. Clear the ModuleClassLoader and reload the R class.
            myLoadedClasses.clear();
            ModuleClassLoader.clearCache(myModule);
            myModuleClassLoader = null;
            aClass = getModuleClassLoader().loadClass(className);
            if (appResources != null) {
                appResources.resetDynamicIds(true);
            }
        } else {
            if (LOG.isDebugEnabled()) {
                if (isClassLoaded) {
                    LOG.debug(String.format("  Class already loaded in module %s.", anonymize(myModule)));
                }
            }
        }
        if (aClass != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("  Class loaded");
            }
            assert myLogger != null;
            myLoadedClasses.put(className, aClass);
            myLogger.setHasLoadedClasses(true);
        }
    }
    if (aClass != null) {
        final Map<ResourceType, TObjectIntHashMap<String>> res2id = new EnumMap<>(ResourceType.class);
        final TIntObjectHashMap<Pair<ResourceType, String>> id2res = new TIntObjectHashMap<>();
        final Map<IntArrayWrapper, String> styleableId2res = new HashMap<>();
        if (parseClass(aClass, id2res, styleableId2res, res2id)) {
            if (appResources != null) {
                appResources.setCompiledResources(id2res, styleableId2res, res2id);
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("END loadAndParseRClass(%s)", anonymizeClassName(className)));
    }
}
Also used : TObjectIntHashMap(gnu.trove.TObjectIntHashMap) HashMap(java.util.HashMap) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceType(com.android.resources.ResourceType) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) IntArrayWrapper(com.android.ide.common.resources.IntArrayWrapper) EnumMap(java.util.EnumMap) Pair(com.android.util.Pair) VisibleForTesting(com.android.annotations.VisibleForTesting)

Example 2 with AppResourceRepository

use of com.android.tools.idea.res.AppResourceRepository in project android by JetBrains.

the class ModuleClassLoader method getExternalJars.

@Override
protected List<URL> getExternalJars() {
    final Module module = myModuleReference.get();
    if (module == null) {
        return Collections.emptyList();
    }
    final List<URL> result = new ArrayList<>();
    if (ThemeEditorProvider.THEME_EDITOR_ENABLE) {
        URL customWidgetsUrl = ThemeEditorUtils.getCustomWidgetsJarUrl();
        if (customWidgetsUrl != null) {
            result.add(customWidgetsUrl);
        }
    }
    List<VirtualFile> externalLibraries;
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet != null && facet.requiresAndroidModel() && facet.getAndroidModel() != null) {
        AndroidModel androidModel = facet.getAndroidModel();
        externalLibraries = androidModel.getClassJarProvider().getModuleExternalLibraries(module);
    } else {
        externalLibraries = AndroidRootUtil.getExternalLibraries(module);
    }
    for (VirtualFile libFile : externalLibraries) {
        if (EXT_JAR.equals(libFile.getExtension())) {
            final File file = new File(libFile.getPath());
            if (file.exists()) {
                try {
                    result.add(SdkUtils.fileToUrl(file));
                    File aarDir = file.getParentFile();
                    if (aarDir != null && (aarDir.getPath().endsWith(DOT_AAR) || aarDir.getPath().contains(EXPLODED_AAR))) {
                        if (aarDir.getPath().contains(EXPLODED_AAR)) {
                            if (aarDir.getPath().endsWith(LIBS_FOLDER)) {
                                // Some libraries recently started packaging jars inside a sub libs folder inside jars
                                aarDir = aarDir.getParentFile();
                            }
                            // Gradle plugin version 1.2.x and later has classes in aar-dir/jars/
                            if (aarDir.getPath().endsWith(FD_JARS)) {
                                aarDir = aarDir.getParentFile();
                            }
                        }
                        AppResourceRepository appResources = AppResourceRepository.getAppResources(module, true);
                        if (appResources != null) {
                            ResourceClassRegistry.get(module.getProject()).addAarLibrary(appResources, aarDir);
                        }
                    } else if (aarDir != null) {
                        // Build cache? We need to compute the package name in a slightly different way
                        File parentFile = aarDir.getParentFile();
                        if (parentFile != null) {
                            File manifest = new File(parentFile, ANDROID_MANIFEST_XML);
                            if (manifest.exists()) {
                                AppResourceRepository appResources = AppResourceRepository.getAppResources(module, true);
                                if (appResources != null) {
                                    FileResourceRepository repository = appResources.findRepositoryFor(parentFile);
                                    if (repository != null) {
                                        ResourceClassRegistry registry = ResourceClassRegistry.get(module.getProject());
                                        registry.addLibrary(appResources, registry.getAarPackage(parentFile));
                                    }
                                }
                            }
                        }
                    }
                } catch (MalformedURLException e) {
                    LOG.error(e);
                }
            }
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MalformedURLException(java.net.MalformedURLException) FileResourceRepository(com.android.tools.idea.res.FileResourceRepository) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceClassRegistry(com.android.tools.idea.res.ResourceClassRegistry) URL(java.net.URL) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) AndroidModel(com.android.tools.idea.model.AndroidModel) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File)

Example 3 with AppResourceRepository

use of com.android.tools.idea.res.AppResourceRepository in project android by JetBrains.

the class GradleInstantRunContext method getManifestResourcesHash.

@VisibleForTesting
static HashCode getManifestResourcesHash(@NotNull AndroidFacet facet) {
    Document manifest = MergedManifest.get(facet).getDocument();
    if (manifest == null || manifest.getDocumentElement() == null) {
        return HashCode.fromInt(0);
    }
    final Hasher hasher = Hashing.goodFastHash(32).newHasher();
    SortedSet<ResourceUrl> appResourceReferences = getAppResourceReferences(manifest.getDocumentElement());
    AppResourceRepository appResources = AppResourceRepository.getAppResources(facet, true);
    // read action needed when reading the values for app resources
    ApplicationManager.getApplication().runReadAction(() -> {
        hashResources(appResourceReferences, appResources, hasher);
    });
    return hasher.hash();
}
Also used : Hasher(com.google.common.hash.Hasher) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceUrl(com.android.ide.common.resources.ResourceUrl) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with AppResourceRepository

use of com.android.tools.idea.res.AppResourceRepository in project android by JetBrains.

the class LocalResourceManager method getResourceNames.

@Override
@NotNull
public Collection<String> getResourceNames(@NotNull ResourceType resourceType, boolean publicOnly) {
    AppResourceRepository appResources = AppResourceRepository.getAppResources(myFacet, true);
    Collection<String> resourceNames;
    if (resourceType == ResourceType.STYLEABLE) {
        // Convert from the tag-oriented types that appResource hold to the inner-class oriented type.
        resourceNames = appResources.getItemsOfType(ResourceType.DECLARE_STYLEABLE);
    } else {
        resourceNames = appResources.getItemsOfType(resourceType);
    }
    // We may need to filter out public only, or if the type is attr, filter out android: attributes.
    if (publicOnly || resourceType == ResourceType.ATTR) {
        Set<String> filtered = ContainerUtil.newHashSet(resourceNames.size());
        for (String name : resourceNames) {
            if (resourceType == ResourceType.ATTR) {
                if (!name.startsWith(SdkConstants.ANDROID_NS_NAME_PREFIX)) {
                    filtered.add(name);
                }
            }
            if (publicOnly) {
                if (!appResources.isPrivate(resourceType, name)) {
                    filtered.add(name);
                }
            }
        }
        resourceNames = filtered;
    }
    return resourceNames;
}
Also used : AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with AppResourceRepository

use of com.android.tools.idea.res.AppResourceRepository in project android by JetBrains.

the class GenerateBackupDescriptorFix method isApplicable.

@Override
public boolean isApplicable(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.ContextType contextType) {
    AndroidFacet facet = AndroidFacet.getInstance(startElement);
    AppResourceRepository appResources = facet == null ? null : facet.getAppResources(true);
    return appResources == null || !appResources.getItemsOfType(ResourceType.XML).contains(myUrl.name);
}
Also used : AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Aggregations

AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)17 NotNull (org.jetbrains.annotations.NotNull)5 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)4 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)3 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)3 ResourceType (com.android.resources.ResourceType)3 Module (com.intellij.openapi.module.Module)3 File (java.io.File)3 LayoutLibrary (com.android.ide.common.rendering.LayoutLibrary)2 ResourceItem (com.android.ide.common.res2.ResourceItem)2 ResourceItem (com.android.ide.common.resources.ResourceItem)2 ResourceRepository (com.android.ide.common.resources.ResourceRepository)2 IAndroidTarget (com.android.sdklib.IAndroidTarget)2 ResourceClassRegistry (com.android.tools.idea.res.ResourceClassRegistry)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 XmlTag (com.intellij.psi.xml.XmlTag)2 VisibleForTesting (com.android.annotations.VisibleForTesting)1 HardwareConfigHelper (com.android.ide.common.rendering.HardwareConfigHelper)1