FileDocCategorySizeDatePackage
DexJarMaker.javaAPI DocAndroid 5.1 API3220Thu Mar 12 22:18:30 GMT 2015com.android.dexgen.util

DexJarMaker

public class DexJarMaker extends Object
Helper class used to encapsulate generated .dex file into .jar so that it fits {@code DexClassLoader} constructor.

Fields Summary
public static final String
DEX_FILE_NAME_IN_JAR
indicates name of the dex file added to jar
private final PathHolder
pathHolder
{@code non-null;} storage for all the paths related to current dex file
Constructors Summary
public DexJarMaker(PathHolder pathHolder)


       
        this.pathHolder = pathHolder;
    
Methods Summary
private voidadd(java.io.File source, java.util.jar.JarOutputStream target)
Adds indicated file to the requested archive.

param
source {@code non-null;} dex file to add
param
target {@code non-null;} target jar archive
throws
IOException


        if (!source.isFile()) {
            throw new IllegalArgumentException("Wrong source dex file provided");
        }

        BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
        JarEntry entry = new JarEntry(DEX_FILE_NAME_IN_JAR);
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);

        int curr = -1;
        while ((curr = in.read()) != -1) {
            target.write(curr);
        }
        target.closeEntry();
    
public voidcreate()
Packs previously added files into a single jar archive.

        Manifest manifest = new Manifest();
        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
        JarOutputStream target = null;
        try {
            target = new JarOutputStream(
                    new BufferedOutputStream(new FileOutputStream(pathHolder.getJarFilePath())),
                    manifest);
            add(new File(pathHolder.getDexFilePath()), target);

        } catch (IOException e) {
            throw new DexClassLoadingException(e);
        }
        finally {
            try {
                if (target != null) {
                    target.close();
                }
            } catch(IOException e) {
                // Ignoring deliberately in order to keep the original exception clear.
            }
        }