Search in sources :

Example 71 with TaskAction

use of org.gradle.api.tasks.TaskAction in project atlas by alibaba.

the class DataBindingRenameTask method createAwbPackages.

/**
     * 生成so的目录
     */
@TaskAction
void createAwbPackages() throws ExecutionException, InterruptedException {
    AndroidDependencyTree androidDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
    if (null == androidDependencyTree) {
        return;
    }
    ExecutorServicesHelper executorServicesHelper = new ExecutorServicesHelper(taskName, getLogger(), 0);
    List<Runnable> runnables = new ArrayList<>();
    for (final AwbBundle awbBundle : androidDependencyTree.getAwbBundles()) {
        runnables.add(new Runnable() {

            @Override
            public void run() {
                try {
                    File dataBindingClazzFolder = appVariantOutputContext.getVariantContext().getJAwbavaOutputDir(awbBundle);
                    String packageName = ManifestFileUtils.getPackage(awbBundle.getOrgManifestFile());
                    String appName = appVariantContext.getVariantConfiguration().getOriginalApplicationId();
                    //删除那些已经存在的类
                    File dataMapperClazz = new File(dataBindingClazzFolder, "android/databinding/DataBinderMapper.class");
                    if (!dataMapperClazz.exists()) {
                        throw new GradleException("missing datamapper class");
                    }
                    FileInputStream fileInputStream = new FileInputStream(dataMapperClazz);
                    ClassReader in1 = new ClassReader(fileInputStream);
                    ClassWriter cw = new ClassWriter(0);
                    Map<String, String> reMapping = new HashedMap();
                    reMapping.put(appName.replace(".", "/") + "/BR", packageName.replace(".", "/") + "/BR");
                    RemappingClassAdapter remappingClassAdapter = new RemappingClassAdapter(cw, new SimpleRemapper(reMapping));
                    in1.accept(remappingClassAdapter, 8);
                    ClassReader in2 = new ClassReader(cw.toByteArray());
                    ClassWriter cw2 = new ClassWriter(0);
                    Set<String> renames = new HashSet<String>();
                    renames.add("android/databinding/DataBinderMapper");
                    in2.accept(new ClassRenamer(cw2, renames, packageName.replace(".", "/") + "/DataBinderMapper"), 8);
                    File destClass = new File(dataBindingClazzFolder, packageName.replace(".", "/") + "/DataBinderMapper.class");
                    destClass.getParentFile().mkdirs();
                    FileOutputStream fileOutputStream = new FileOutputStream(destClass);
                    fileOutputStream.write(cw2.toByteArray());
                    IOUtils.closeQuietly(fileOutputStream);
                    IOUtils.closeQuietly(fileInputStream);
                    FileUtils.deleteDirectory(new File(dataBindingClazzFolder, "android/databinding"));
                    FileUtils.deleteDirectory(new File(dataBindingClazzFolder, "com/android/databinding"));
                    File appDir = new File(dataBindingClazzFolder, appName.replace(".", "/"));
                    if (appDir.exists()) {
                        File[] files = appDir.listFiles(new FileFilter() {

                            @Override
                            public boolean accept(File pathname) {
                                return pathname.isFile() && !pathname.isDirectory();
                            }
                        });
                        for (File tmp : files) {
                            FileUtils.forceDelete(tmp);
                        }
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                    throw new GradleException("package awb failed");
                }
            }
        });
    }
    executorServicesHelper.execute(runnables);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) AndroidDependencyTree(com.taobao.android.builder.dependency.AndroidDependencyTree) FileInputStream(java.io.FileInputStream) ExecutorServicesHelper(com.taobao.android.builder.tools.concurrent.ExecutorServicesHelper) SimpleRemapper(org.objectweb.asm.commons.SimpleRemapper) RemappingClassAdapter(org.objectweb.asm.commons.RemappingClassAdapter) GradleException(org.gradle.api.GradleException) FileOutputStream(java.io.FileOutputStream) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) HashedMap(org.apache.commons.collections.map.HashedMap) FileFilter(java.io.FileFilter) File(java.io.File) Map(java.util.Map) HashedMap(org.apache.commons.collections.map.HashedMap) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 72 with TaskAction

use of org.gradle.api.tasks.TaskAction in project atlas by alibaba.

the class PrepareAwoBundleTask method doFullTaskAction.

@TaskAction
public void doFullTaskAction() throws IOException {
    AwbBundle awbBundle = libVariantContext.getAwbBundle();
    prepare(awbBundle.getBundle(), awbBundle.getFolder(), true);
    List<SoLibrary> soLibraries = awbBundle.getSoLibraries();
    for (final SoLibrary soLibrary : soLibraries) {
        prepare(soLibrary.getSoLibFile(), soLibrary.getFolder(), false);
    }
    List<? extends AndroidLibrary> aarBundles = awbBundle.getLibraryDependencies();
    for (final AndroidLibrary libraryDependency : aarBundles) {
        if (libraryDependency instanceof AarBundle) {
            AarBundle aarBundle = (AarBundle) libraryDependency;
            prepare(aarBundle.getBundle(), aarBundle.getFolder(), true);
        }
    }
}
Also used : AndroidLibrary(com.android.builder.model.AndroidLibrary) AarBundle(com.taobao.android.builder.dependency.model.AarBundle) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) SoLibrary(com.taobao.android.builder.dependency.model.SoLibrary) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 73 with TaskAction

use of org.gradle.api.tasks.TaskAction in project atlas by alibaba.

the class DexInstallTask method doTask.

@TaskAction
public void doTask() throws IOException, SigningException {
    dexFile = getDexFile();
    File targetPatch = getKernelSoFile();
    ZipOutputStream out = null;
    try {
        out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(targetPatch)));
        zip(new File[] { dexFile }, "", out);
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
    if (out != null) {
        try {
            out.close();
        } catch (Throwable e) {
        }
    }
    AwoInstaller.installAwoSo(getBuilder(), targetPatch, getPackageName(), getLogger());
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) OutputFile(org.gradle.api.tasks.OutputFile) InputFile(org.gradle.api.tasks.InputFile) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 74 with TaskAction

use of org.gradle.api.tasks.TaskAction in project atlas by alibaba.

the class TPatchDiffResAPBuildTask method doAction.

@TaskAction
protected void doAction() throws IOException {
    manifestFile = getManifestFile();
    aaptOptions = getAaptOptions();
    diffResDir = getDiffResDir();
    resourceConfigs = getResourceConfigs();
    preferredDensity = getPreferredDensity();
    //TODO  minifyManifest
    File miniManifest = new File(packageOutputFile.getParentFile(), "AndroidManifest.xml");
    try {
        ManifestFileUtils.minifyManifest(manifestFile, miniManifest);
    } catch (DocumentException e) {
        throw new GradleException(e.getMessage());
    }
    File assetsFolder = new File(diffResDir, "assets");
    File resFolder = new File(diffResDir, "res");
    if (!assetsFolder.exists()) {
        assetsFolder.mkdirs();
    }
    if (!resFolder.exists()) {
        resFolder.mkdirs();
    }
    AaptPackageProcessBuilder aaptPackageCommandBuilder = new AaptPackageProcessBuilder(miniManifest, aaptOptions).setAssetsFolder(assetsFolder).setResFolder(resFolder).setLibraries(libraries).setPackageForR(packageForR).setResPackageOutput(packageOutputFile.getAbsolutePath()).setType(type).setDebuggable(debuggable).setPseudoLocalesEnabled(pseudoLocalesEnabled).setResourceConfigs(resourceConfigs).setSplits(splits).setPreferredDensity(preferredDensity);
    @NonNull AndroidBuilder builder = getBuilder();
    MergingLog mergingLog = new MergingLog(mergeBlameLogFolder);
    ProcessOutputHandler processOutputHandler = new ParsingProcessOutputHandler(new ToolOutputParser(new AaptOutputParser(), getILogger()), new MergingLogRewriter(mergingLog, builder.getErrorReporter()));
    try {
        if (builder instanceof AtlasBuilder) {
            ((AtlasBuilder) builder).processResources(aaptPackageCommandBuilder, enforceUniquePackageName, processOutputHandler);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (ProcessException e) {
        throw new RuntimeException(e);
    }
}
Also used : ParsingProcessOutputHandler(com.android.ide.common.blame.ParsingProcessOutputHandler) ProcessOutputHandler(com.android.ide.common.process.ProcessOutputHandler) MergingLog(com.android.ide.common.blame.MergingLog) AaptOutputParser(com.android.ide.common.blame.parser.aapt.AaptOutputParser) IOException(java.io.IOException) MergingLogRewriter(com.android.ide.common.blame.MergingLogRewriter) ProcessException(com.android.ide.common.process.ProcessException) AaptPackageProcessBuilder(com.android.builder.core.AaptPackageProcessBuilder) ToolOutputParser(com.android.ide.common.blame.parser.ToolOutputParser) DocumentException(org.dom4j.DocumentException) GradleException(org.gradle.api.GradleException) NonNull(com.android.annotations.NonNull) AndroidBuilder(com.android.builder.core.AndroidBuilder) AtlasBuilder(com.android.builder.core.AtlasBuilder) File(java.io.File) InputFile(org.gradle.api.tasks.InputFile) ParsingProcessOutputHandler(com.android.ide.common.blame.ParsingProcessOutputHandler) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 75 with TaskAction

use of org.gradle.api.tasks.TaskAction in project atlas by alibaba.

the class TPatchTask method doTPatch.

@TaskAction
public void doTPatch() throws Exception {
    patchContext = getPatchContext();
    signingConfig = getSigningConfig();
    outPatchFolder = getOutPatchFolder();
    // 获取容器版本
    String baseApkVersion = patchContext.getBaseVersionName();
    String newApkVersion = patchContext.versionName;
    File baseApk = patchContext.getBaseApk();
    File newApk = patchContext.diffApkFile;
    boolean retainMainBundleRes = true;
    if (null == newApk || !newApk.exists()) {
        newApk = patchContext.newApk;
        retainMainBundleRes = false;
    }
    getLogger().info("BaseApk:" + baseApk + ",baseVersion:" + baseApkVersion + ",newApk:" + newApk + ",newApkVersion:" + newApkVersion);
    TPatchTool tPatchTool = new TPatchTool(baseApk, newApk, baseApkVersion, newApkVersion, patchContext.diffBundleDex);
    tPatchTool.setMainBundleName(patchContext.mainBundleName);
    if (StringUtils.isNotBlank(patchContext.excludeFiles)) {
        tPatchTool.setNotIncludeFiles(patchContext.excludeFiles.split(","));
    }
    tPatchTool.setRetainMainBundleRes(retainMainBundleRes);
    if (null != patchContext.artifactBundleInfos) {
        tPatchTool.setArtifactBundleInfos(patchContext.artifactBundleInfos);
    }
    tPatchTool.setBaseApkFileList(patchContext.getBaseApkFiles());
    tPatchTool.setNewApkFileList(patchContext.getNewApkFiles());
    tPatchTool.setLogger(getILogger());
    tPatchTool.setOnlyIncludeModifyBundle(patchContext.onlyBuildModifyAwb);
    tPatchTool.setNoPatchBundles(patchContext.notPatchBundles);
    ApkFileList apkFileList = AtlasBuildContext.finalApkFileList;
    try {
        tPatchTool.setDexcode(apkFileList.getMainBundle().get("classes.dex"));
        FileUtils.writeStringToFile(new File(getOutPatchFolder(), "tpatch-bundles.json"), JSON.toJSONString(patchContext.artifactBundleInfos));
        getLogger().info("start to do patch");
        tPatchTool.doPatch(outPatchFolder, true, new File(getOutPatchFolder(), "patchs.json"), StringUtils.isNotEmpty(patchContext.tpatchHistoryUrl), patchContext.tpatchHistoryUrl, patchContext.appSignName);
        getLogger().info("finish  do patch");
        File patchJson = new File(getOutPatchFolder(), "patchs.json");
        File updateJson = new File(getOutPatchFolder(), "update.json");
        String json = FileUtils.readFileToString(patchJson);
        BuildPatchInfos patchInfos = JSON.parseObject(json, BuildPatchInfos.class);
        UpdateInfo updateInfo = new UpdateInfo(patchInfos);
        FileUtils.writeStringToFile(updateJson, JSON.toJSONString(updateInfo, true));
    } catch (Exception e) {
        e.printStackTrace();
        throw new StopExecutionException(e.getMessage());
    }
    File baseVesrionApk = new File(patchContext.newApk.getParentFile(), patchContext.newApk.getName().replace(".apk", "-" + baseApkVersion + ".apk"));
    FileUtils.copyFile(patchContext.getBaseApk(), baseVesrionApk);
    if (patchContext.writeBuildInfo && StringUtils.isNotEmpty(patchContext.buildId)) {
        File buildFile = new File(getOutPatchFolder(), "build.txt");
        FileUtils.writeStringToFile(buildFile, patchContext.buildId + "," + patchContext.versionName + "," + apkFileList.getMainBundle().get("classes.dex"));
        if (buildFile != null && buildFile.exists()) {
            getLogger().debug("write build to apk!");
            BuildHelper.writeFileToApk(buildFile, baseVesrionApk, "assets/build.txt");
        }
        BuildHelper.reSign(baseVesrionApk, signingConfig);
    }
    FileUtils.forceDelete(patchContext.newApk);
}
Also used : StopExecutionException(org.gradle.api.tasks.StopExecutionException) ApkFileList(com.taobao.android.object.ApkFileList) File(java.io.File) TPatchTool(com.taobao.android.TPatchTool) BuildPatchInfos(com.taobao.android.object.BuildPatchInfos) UpdateInfo(com.taobao.android.builder.tools.update.UpdateInfo) DocumentException(org.dom4j.DocumentException) StopExecutionException(org.gradle.api.tasks.StopExecutionException) IOException(java.io.IOException) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Aggregations

TaskAction (org.gradle.api.tasks.TaskAction)92 File (java.io.File)40 MtlBaseTaskAction (com.taobao.android.builder.tasks.manager.MtlBaseTaskAction)22 GradleException (org.gradle.api.GradleException)15 ArrayList (java.util.ArrayList)11 InputFile (org.gradle.api.tasks.InputFile)11 AwbBundle (com.taobao.android.builder.dependency.model.AwbBundle)10 OutputFile (org.gradle.api.tasks.OutputFile)10 AndroidDependencyTree (com.taobao.android.builder.dependency.AndroidDependencyTree)9 IOException (java.io.IOException)8 Project (org.gradle.api.Project)8 WorkResult (org.gradle.api.tasks.WorkResult)8 BuildOperationLogger (org.gradle.internal.operations.logging.BuildOperationLogger)8 ExecutorServicesHelper (com.taobao.android.builder.tools.concurrent.ExecutorServicesHelper)7 InvalidUserDataException (org.gradle.api.InvalidUserDataException)7 HashSet (java.util.HashSet)6 SoLibrary (com.taobao.android.builder.dependency.model.SoLibrary)5 AndroidLibrary (com.android.builder.model.AndroidLibrary)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4