Search in sources :

Example 1 with ResourceClassRegistry

use of com.android.tools.idea.res.ResourceClassRegistry 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 2 with ResourceClassRegistry

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

the class ModuleClassLoaderTest method testAARPriority.

/**
   * Verifies that the AAR generated R classes are given priority vs the build generated files. This is important in cases like support
   * library upgrades/downgrades. In those cases, the build generated file, will be outdated so it shouldn't be used by the ModuleClassLoader.
   * By preferring the AAR geneated versions, we make sure we are always up-to-date.
   * See <a href="http://b.android.com/229382">229382</a>
   */
public void testAARPriority() throws ClassNotFoundException, IOException {
    LayoutLibrary layoutLibrary = mock(LayoutLibrary.class);
    Module module = myFixture.getModule();
    File tmpDir = Files.createTempDir();
    File outputDir = new File(tmpDir, CompilerModuleExtension.PRODUCTION + "/" + module.getName() + "/test");
    assertTrue(FileUtil.createDirectory(outputDir));
    CompilerProjectExtension.getInstance(getProject()).setCompilerOutputUrl(pathToIdeaUrl(tmpDir));
    generateRClass("test", new File(outputDir, "R.class"));
    AppResourceRepository appResources = AppResourceRepository.getAppResources(module, true);
    ResourceClassRegistry rClassRegistry = ResourceClassRegistry.get(module.getProject());
    rClassRegistry.addLibrary(appResources, "test");
    AtomicBoolean noSuchField = new AtomicBoolean(false);
    ApplicationManager.getApplication().runReadAction(() -> {
        ModuleClassLoader loader = ModuleClassLoader.get(layoutLibrary, module);
        try {
            Class<?> rClass = loader.loadClass("test.R");
            rClass.getDeclaredField("ID");
        } catch (NoSuchFieldException e) {
            noSuchField.set(true);
        } catch (ClassNotFoundException e) {
            fail("Unexpected exception " + e.getLocalizedMessage());
        }
    });
    assertTrue(noSuchField.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LayoutLibrary(com.android.ide.common.rendering.LayoutLibrary) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) Module(com.intellij.openapi.module.Module) ResourceClassRegistry(com.android.tools.idea.res.ResourceClassRegistry) File(java.io.File)

Aggregations

AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)2 ResourceClassRegistry (com.android.tools.idea.res.ResourceClassRegistry)2 Module (com.intellij.openapi.module.Module)2 File (java.io.File)2 LayoutLibrary (com.android.ide.common.rendering.LayoutLibrary)1 AndroidModel (com.android.tools.idea.model.AndroidModel)1 FileResourceRepository (com.android.tools.idea.res.FileResourceRepository)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1