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)));
}
}
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;
}
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();
}
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;
}
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);
}
Aggregations