com.android.build.api.transform.JarInput Java Examples

The following examples show how to use com.android.build.api.transform.JarInput. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AtlasDexArchiveBuilderCacheHander.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Nullable
public File getCachedVersionIfPresent(JarInput input) throws Exception {
    FileCache cache =
            getBuildCache(
                    input.getFile(), isExternalLib(input), userLevelCache);

    if (cache == null) {
        return null;
    }

    FileCache.Inputs buildCacheInputs =
            getBuildCacheInputs(
                    input.getFile(), dexOptions, dexer, minSdkVersion, isDebuggable);
    return cache.cacheEntryExists(buildCacheInputs)
            ? cache.getFileInCache(buildCacheInputs)
            : null;
}
 
Example #2
Source File: JarContentProvider.java    From EasyRouter with Apache License 2.0 6 votes vote down vote up
private void forActualInput(JarInput jarInput, ClassHandler processor) throws IOException {
    if (processor.onStart(jarInput)) {
        //Log.i("start trans jar "+ jarInput.getStatus() + " " + jarInput.getName() + " " + jarInput.getFile());
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(jarInput.getFile())));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                continue;
            }
            try {
                //Log.i("start trans class haha " + entry.getName());
                byte[] data = ByteStreams.toByteArray(zis);
                processor.onClassFetch(jarInput, jarInput.getStatus(), entry.getName(), data);
            } catch (EOFException e){
                break;
            }
        }
        IOUtils.closeQuietly(zis);
    }
    processor.onComplete(jarInput);
}
 
Example #3
Source File: DataLoaderTransform.java    From DataLoader with Apache License 2.0 6 votes vote down vote up
private void process(TransformInvocation transformInvocation) throws IOException {
    List<LoaderInfo> loaderInfoList = new ArrayList<>();
    for (TransformInput input : transformInvocation.getInputs()) {
        for (JarInput jarInput : input.getJarInputs()) {
            File file = jarInput.getFile();
            JarFile jarFile = new JarFile(file);
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                if (isLoaderInfoEntry(entry)) {
                    System.out.println(TAG + "entry name: " + entry.getName());
                    String content = GradleUtils.getContent(jarFile, entry);
                    System.out.println(TAG + "content: " + content);
                    Type type = new TypeToken<List<LoaderInfo>>() {
                    }.getType();
                    List<LoaderInfo> loaderInfos = gson.fromJson(content, type);
                    if (loaderInfos != null) {
                        loaderInfoList.addAll(loaderInfos);
                    }
                }
            }
        }
    }
    String json = gson.toJson(loaderInfoList);
    generateInitClass(transformInvocation, json);
}
 
Example #4
Source File: AtlasMainDexHelper.java    From atlas with Apache License 2.0 6 votes vote down vote up
public void updateMainDexFile(JarInput oldFile, File newFile) {
    for (BuildAtlasEnvTask.FileIdentity id : mainDexJar) {
        if (!id.file.exists()){
            continue;
        }
        try {
            if (Files.isSameFile(oldFile.getFile().toPath(), id.file.toPath())) {
                id.file = newFile;
                break;
            } else if (oldFile.getName().startsWith("android.local.jars")) {
                if (oldFile.getName().split(":")[1].equals(id.file.getName())) {
                    id.file = newFile;
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
 
Example #5
Source File: CollectHandler.java    From EasyRouter with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onStart(QualifiedContent content) throws IOException {
    if (content instanceof JarInput) {
        JarInput jarInput = (JarInput) content;
        File targetFile = context.getRelativeFile(content);
        switch (jarInput.getStatus()) {
            case REMOVED:
                FileUtils.deleteIfExists(targetFile);
                return false;
            case CHANGED:
                FileUtils.deleteIfExists(targetFile);
            default:
                Files.createParentDirs(targetFile);
                map.put(content, new JarWriter(targetFile));
        }
        return true;
    }
    return false;
}
 
Example #6
Source File: AbstractTransform.java    From SocialSdkLibrary with Apache License 2.0 6 votes vote down vote up
@Override
public void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
    super.transform(transformInvocation);
    UtilX.log("start transform");
    long startTime = System.currentTimeMillis();
    // 获取输入
    Collection<TransformInput> inputs = transformInvocation.getInputs();
    final TransformOutputProvider outputProvider = transformInvocation.getOutputProvider();
    // 删除之前的输出
    if (outputProvider != null) {
        outputProvider.deleteAll();
    }
    for (TransformInput transformInput : inputs) {
        for (DirectoryInput directoryInput : transformInput.getDirectoryInputs()) {
            onEachDirectory(directoryInput, outputProvider);
        }
        for (JarInput jarInput : transformInput.getJarInputs()) {
            onEachJar(jarInput, outputProvider);
        }
    }
    long endTime = System.currentTimeMillis();
    UtilX.log("end transform cost time " + (endTime - startTime) / 1000 + " s");
}
 
Example #7
Source File: ContextReader.java    From EasyRouter with Apache License 2.0 5 votes vote down vote up
/**
 * read the classes in thread pool and send class to fetcher.
 * @param fetcher the fetcher to visit classes
 * @throws IOException
 * @throws InterruptedException
 */
public void accept(ClassHandler fetcher) throws IOException, InterruptedException {
    // get all jars
    Collection<JarInput> jars = context.getAllJars();
    // accept the jar in thread pool
    List<Future<Void>> tasks = Stream.concat(jars.stream(), context.getAllDirs().stream())
                                       .filter(t -> provider.accepted(t))
                                       .map(q -> new QualifiedContentTask(q, fetcher))
                                       .map(t -> service.submit(t))
                                       .collect(Collectors.toList());

    // block until all task has finish.
    for (Future<Void> future : tasks) {
        try {
            future.get();
        } catch (ExecutionException e) {
            Throwable cause = e.getCause();
            if (cause instanceof IOException) {
                throw (IOException) cause;
            } else if (cause instanceof InterruptedException) {
                throw (InterruptedException) cause;
            } else {
                throw new RuntimeException(e.getCause());
            }
        }
    }

}
 
Example #8
Source File: AppVariantOutputContext.java    From atlas with Apache License 2.0 5 votes vote down vote up
public  File updateAwbDexFile(JarInput jarInput, File output) {
    boolean localJar = false;
    if (jarInput.getName().startsWith("android.local.jars")){
        localJar = true;
    }
    if (localJar){
        //todo awb localJars
        //subproject local


    }

    for (AwbTransform awbTransform:awbTransformMap.values()){
        if (awbTransform.getFileTransform().containsKey(jarInput.getFile())){
            File currentFile = awbTransform.getFileTransform().get(jarInput.getFile());
            awbTransform.getInputLibraries().remove(currentFile);
            awbTransform.getInputLibraries().add(output);
            return currentFile;
        }else {
           Iterator<File>iterator = awbTransform.getInputLibraries().listIterator();
           while (iterator.hasNext()){
               File file = iterator.next();
               if (file.equals(jarInput.getFile())){
                   iterator.remove();
                   awbTransform.getInputLibraries().add(output);
                   return jarInput.getFile();
               }
           }
        }

    }
    return null;
}
 
Example #9
Source File: TransformInputUtils.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static JarInput makeJarInput(File file,AppVariantContext variantContext) {
    BuildAtlasEnvTask.FileIdentity finalFileIdentity = AtlasBuildContext.atlasMainDexHelperMap.get(variantContext.getVariantName()).get(file);
    return new JarInput() {
        @Override
        public Status getStatus() {
            return Status.ADDED;
        }

        @Override
        public String getName() {

            return MD5Util.getFileMD5(file);
        }

        @Override
        public File getFile() {
            return file;
        }

        @Override
        public Set<QualifiedContent.ContentType> getContentTypes() {
            return ImmutableSet.of(QualifiedContent.DefaultContentType.CLASSES);
        }

        @Override
        public Set<? super QualifiedContent.Scope> getScopes() {
            if (finalFileIdentity == null){
                return  ImmutableSet.of(Scope.EXTERNAL_LIBRARIES);
            }
            if (finalFileIdentity.subProject) {
                return ImmutableSet.of(Scope.SUB_PROJECTS);
            } else {
                return ImmutableSet.of(Scope.EXTERNAL_LIBRARIES);
            }
        }
    };
}
 
Example #10
Source File: AtlasMainDexHelper.java    From atlas with Apache License 2.0 5 votes vote down vote up
private boolean inMainDex(String name, JarInput jarInput) {
    for (BuildAtlasEnvTask.FileIdentity fileIdentity : mainDexJar) {
        if (fileIdentity.file.getName().equals(name)) {
            fileIdentity.file = jarInput.getFile();
            return true;
        }
    }
    return false;
}
 
Example #11
Source File: AtlasMainDexHelper.java    From atlas with Apache License 2.0 5 votes vote down vote up
public boolean inMainDex(JarInput jarInput) {
    File file = jarInput.getFile();
    boolean flag =  inMainDex(file);
    if (!flag) {
        if (jarInput.getName().startsWith("android.local.jars")) {
            return inMainDex(jarInput.getName().split(":")[1], jarInput);
        }
    }
    return flag;
}
 
Example #12
Source File: AtlasMainDexHelper.java    From atlas with Apache License 2.0 5 votes vote down vote up
public void updateMainDexFiles(Map<JarInput, File> fileFileMap) {
        for (Map.Entry<JarInput,File> entry : fileFileMap.entrySet()) {
            if (entry.getKey().getFile().exists() && entry.getValue().exists()) {
                updateMainDexFile((JarInput) entry.getKey(), (File) entry.getValue());
            }else {
                remove(entry.getKey().getFile());
            }
        }

}
 
Example #13
Source File: MtlInjectTransform.java    From atlas with Apache License 2.0 5 votes vote down vote up
protected File getOutputFile(TransformOutputProvider outputProvider, JarInput jarInput) {

        if (null != logger) {
            logger.debug("[ClassInject]" + jarInput.getFile().getAbsolutePath());
        }

        return outputProvider.getContentLocation(getUniqueJarName(jarInput),
                                                 jarInput.getContentTypes(),
                                                 jarInput.getScopes(),
                                                 Format.JAR);

    }
 
Example #14
Source File: TransformContext.java    From EasyRouter with Apache License 2.0 4 votes vote down vote up
public Collection<JarInput> getRemovedJars() {
    return Collections.unmodifiableCollection(removedJars);
}
 
Example #15
Source File: TransformContext.java    From EasyRouter with Apache License 2.0 4 votes vote down vote up
public Collection<JarInput> getChangedJars() {
    return Collections.unmodifiableCollection(changedJars);
}
 
Example #16
Source File: TransformContext.java    From EasyRouter with Apache License 2.0 4 votes vote down vote up
public Collection<JarInput> getAddedJars() {
    return Collections.unmodifiableCollection(addedJars);
}
 
Example #17
Source File: TransformContext.java    From EasyRouter with Apache License 2.0 4 votes vote down vote up
public Collection<JarInput> getAllJars() {
    return Collections.unmodifiableCollection(allJars);
}
 
Example #18
Source File: TransformContext.java    From EasyRouter with Apache License 2.0 4 votes vote down vote up
public File getRelativeFile(QualifiedContent content) {
    return invocation.getOutputProvider().getContentLocation(content.getName(), content.getContentTypes(), content.getScopes(),
            (content instanceof JarInput ? Format.JAR : Format.DIRECTORY));
}
 
Example #19
Source File: JarContentProvider.java    From EasyRouter with Apache License 2.0 4 votes vote down vote up
@Override
public boolean accepted(QualifiedContent qualifiedContent) {
    return qualifiedContent instanceof JarInput;
}
 
Example #20
Source File: JarContentProvider.java    From EasyRouter with Apache License 2.0 4 votes vote down vote up
@Override
public void forEach(QualifiedContent content, ClassHandler processor) throws IOException {
    forActualInput((JarInput) content, processor);
}
 
Example #21
Source File: CollectHandler.java    From EasyRouter with Apache License 2.0 4 votes vote down vote up
@Override
public void onComplete(QualifiedContent content) throws IOException {
    if (content instanceof JarInput && ((JarInput) content).getStatus() != Status.REMOVED) {
        map.get(content).close();
    }
}
 
Example #22
Source File: AbstractTransform.java    From SocialSdkLibrary with Apache License 2.0 4 votes vote down vote up
private void onEachJar(JarInput input, TransformOutputProvider provider) {
    File file = input.getFile();
    if (!file.getAbsolutePath().endsWith("jar")) {
        return;
    }
    String jarName = input.getName();
    String md5Name = DigestUtils.md5Hex(file.getAbsolutePath());
    if (jarName.endsWith(".jar")) {
        jarName = jarName.substring(0, jarName.length() - 4);
    }
    try {
        JarFile jarFile = new JarFile(file);
        Enumeration<JarEntry> entries = jarFile.entries();
        File tmpFile = new File(file.getParent() + File.separator + "classes_temp.jar");
        //避免上次的缓存被重复插入
        if (tmpFile.exists()) {
            tmpFile.delete();
        }
        JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(tmpFile));
        //用于保存
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            String entryName = jarEntry.getName();
            ZipEntry zipEntry = new ZipEntry(entryName);
            InputStream inputStream = jarFile.getInputStream(jarEntry);
            // 插桩class
            byte[] bytes = IOUtils.toByteArray(inputStream);
            if (isAttentionFile(entryName)) {
                // class文件处理
                jarOutputStream.putNextEntry(zipEntry);
                byte[] code = TransformX.visitClass(bytes, onEachClassFile(entryName));
                jarOutputStream.write(code);
            } else {
                jarOutputStream.putNextEntry(zipEntry);
                jarOutputStream.write(bytes);
            }
            jarOutputStream.closeEntry();
        }
        // 结束
        jarOutputStream.close();
        jarFile.close();
        File dest = provider.getContentLocation(jarName + md5Name,
                input.getContentTypes(), input.getScopes(), Format.JAR);
        FileUtils.copyFile(tmpFile, dest);
        tmpFile.delete();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #23
Source File: AtlasMainDexHelper.java    From atlas with Apache License 2.0 4 votes vote down vote up
public BuildAtlasEnvTask.FileIdentity get(JarInput jarInput) {
    File file = jarInput.getFile();
    return get(file);
}
 
Example #24
Source File: MtlInjectTransform.java    From atlas with Apache License 2.0 2 votes vote down vote up
protected String getUniqueJarName(JarInput jarInput) {

        return FileNameUtils.getUniqueJarName(jarInput.getFile());
    }