Search in sources :

Example 1 with BufferingFileWrapper

use of org.jetbrains.android.util.BufferingFileWrapper 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)

Aggregations

LayoutLibrary (com.android.ide.common.rendering.LayoutLibrary)1 LayoutLog (com.android.ide.common.rendering.api.LayoutLog)1 Bridge (com.android.layoutlib.bridge.Bridge)1 ILogger (com.android.utils.ILogger)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 File (java.io.File)1 BufferingFileWrapper (org.jetbrains.android.util.BufferingFileWrapper)1 Nullable (org.jetbrains.annotations.Nullable)1