Search in sources :

Example 1 with GradleException

use of org.gradle.api.GradleException in project atlas by alibaba.

the class AtlasBuilder method processResources.

@Override
public void processResources(Aapt aapt, Builder aaptConfigBuilder, boolean enforceUniquePackageName) throws IOException, InterruptedException, ProcessException {
    if (aapt instanceof AaptV1) {
        try {
            getTargetInfo();
            AaptOptions aaptOptions = (AaptOptions) FieldUtils.readField(aaptConfigBuilder.build(), "mAaptOptions", true);
            ProcessExecutor processExecutor = (ProcessExecutor) FieldUtils.readField(aapt, "mProcessExecutor", true);
            ProcessOutputHandler processOutputHandler = (ProcessOutputHandler) FieldUtils.readField(aapt, "mProcessOutputHandler", true);
            BuildToolInfo buildToolInfo = (BuildToolInfo) FieldUtils.readField(aapt, "mBuildToolInfo", true);
            PngProcessMode processMode = (PngProcessMode) FieldUtils.readField(aapt, "mProcessMode", true);
            int cruncherProcesses = aaptOptions.getCruncherProcesses();
            aapt = new AtlasAapt(processExecutor, processOutputHandler, buildToolInfo, getLogger(), processMode, cruncherProcesses);
        } catch (Throwable e) {
            throw new GradleException("aapt exception", e);
        }
    }
    super.processResources(aapt, aaptConfigBuilder, enforceUniquePackageName);
}
Also used : ProcessOutputHandler(com.android.ide.common.process.ProcessOutputHandler) PngProcessMode(com.android.builder.internal.aapt.v1.AaptV1.PngProcessMode) BuildToolInfo(com.android.sdklib.BuildToolInfo) AaptOptions(com.android.build.gradle.internal.dsl.AaptOptions) GradleException(org.gradle.api.GradleException) AaptV1(com.android.builder.internal.aapt.v1.AaptV1) ProcessExecutor(com.android.ide.common.process.ProcessExecutor) JavaProcessExecutor(com.android.ide.common.process.JavaProcessExecutor)

Example 2 with GradleException

use of org.gradle.api.GradleException in project atlas by alibaba.

the class AtlasBuilder method getTargetInfo.

@Override
public TargetInfo getTargetInfo() {
    if (!updateAapt && useCustomAapt) {
        BuildToolInfo defaultBuildToolInfo = super.getTargetInfo().getBuildTools();
        File customAaptFile = getAapt();
        try {
            Method method = defaultBuildToolInfo.getClass().getDeclaredMethod("add", PathId.class, File.class);
            method.setAccessible(true);
            method.invoke(defaultBuildToolInfo, PathId.AAPT, customAaptFile);
        } catch (Throwable e) {
            throw new GradleException(e.getMessage());
        }
        updateAapt = true;
    }
    return super.getTargetInfo();
}
Also used : BuildToolInfo(com.android.sdklib.BuildToolInfo) GradleException(org.gradle.api.GradleException) Method(java.lang.reflect.Method) File(java.io.File)

Example 3 with GradleException

use of org.gradle.api.GradleException in project atlas by alibaba.

the class PackageAwbsTask method createAwbPackages.

/**
     * 生成so的目录
     */
@TaskAction
void createAwbPackages() throws ExecutionException, InterruptedException {
    AndroidDependencyTree androidDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
    if (null == androidDependencyTree) {
        return;
    }
    final ProcessOutputHandler outputHandler = new ParsingProcessOutputHandler(new ToolOutputParser(new DexParser(), Message.Kind.ERROR, getILogger()), new ToolOutputParser(new DexParser(), getILogger()), getBuilder().getErrorReporter());
    ExecutorServicesHelper executorServicesHelper = new ExecutorServicesHelper(taskName, getLogger(), 0);
    List<Runnable> runnables = new ArrayList<>();
    final AtomicLong dexTotalTime = new AtomicLong(0);
    final AtomicLong packageTotalTime = new AtomicLong(0);
    final Map<String, Long[]> monitors = new HashMap<String, Long[]>();
    long startTime = System.currentTimeMillis();
    for (final AwbBundle awbBundle : androidDependencyTree.getAwbBundles()) {
        runnables.add(new Runnable() {

            @Override
            public void run() {
                try {
                    long start = System.currentTimeMillis();
                    //create dex
                    File dexOutputFile = appVariantContext.getAwbDexOutput(awbBundle.getName());
                    emptyFolder(dexOutputFile);
                    // if some of our .jar input files exist, just reset the inputDir to null
                    AwbTransform awbTransform = appVariantOutputContext.getAwbTransformMap().get(awbBundle.getName());
                    List<File> inputFiles = new ArrayList<File>();
                    inputFiles.addAll(awbTransform.getInputFiles());
                    inputFiles.addAll(awbTransform.getInputLibraries());
                    if (null != awbTransform.getInputDir()) {
                        inputFiles.add(awbTransform.getInputDir());
                    }
                    AtlasBuildContext.androidBuilder.convertByteCode(inputFiles, dexOutputFile, appVariantContext.getVariantData().getVariantConfiguration().isMultiDexEnabled(), null, androidConfig.getDexOptions(), true, outputHandler);
                    //create package
                    long endDex = System.currentTimeMillis();
                    //PACKAGE APP:
                    File resourceFile = appVariantOutputContext.getAwbAndroidResourcesMap().get(awbBundle.getName()).getPackageOutputFile();
                    Set<File> dexFolders = new HashSet<File>();
                    dexFolders.add(dexOutputFile);
                    Set<File> jniFolders = Sets.newHashSet();
                    if (appVariantOutputContext.getAwbJniFolder(awbBundle) != null && appVariantOutputContext.getAwbJniFolder(awbBundle).exists()) {
                        jniFolders.add(appVariantOutputContext.getAwbJniFolder(awbBundle));
                    }
                    Set<File> javaResourcesLocations = Sets.newHashSet();
                    if (appVariantContext.getAtlasExtension().getTBuildConfig().getMergeAwbJavaRes()) {
                        javaResourcesLocations.addAll(awbBundle.getLibraryJars());
                    }
                    //getBuilder().packageCodeSplitApk();
                    getBuilder().oldPackageApk(resourceFile.getAbsolutePath(), dexFolders, javaResourcesLocations, jniFolders, null, getAbiFilters(), config.getBuildType().isJniDebuggable(), null, getOutputFile(awbBundle), config.getMinSdkVersion().getApiLevel(), new Predicate<String>() {

                        @Override
                        public boolean apply(@Nullable String s) {
                            return false;
                        }
                    });
                    long endPackage = System.currentTimeMillis();
                    dexTotalTime.addAndGet(endDex - start);
                    packageTotalTime.addAndGet(endPackage - endDex);
                    monitors.put(awbBundle.getName(), new Long[] { endDex - start, endPackage - endDex });
                } catch (Throwable e) {
                    e.printStackTrace();
                    throw new GradleException("package " + awbBundle.getName() + " failed");
                }
            }
        });
    }
    executorServicesHelper.execute(runnables);
    if (getLogger().isInfoEnabled()) {
        getLogger().info(">>>>> packageAwbs >>>>>>>>>>>>");
        getLogger().info("totalTime is " + (System.currentTimeMillis() - startTime));
        getLogger().info("dexTotalTime is " + dexTotalTime);
        getLogger().info("packageTotalTime is " + packageTotalTime);
        String format = "[packageawb]  bundle:%50s  dexTime: %10d  packageTime: %10d ";
        for (String bundle : monitors.keySet()) {
            Long[] value = monitors.get(bundle);
            getLogger().info(String.format(format, bundle, value[0], value[1]));
        }
        getLogger().info(">>>>> packageAwbs >>>>>>>>>>>>");
    }
}
Also used : HashSet(java.util.HashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AndroidDependencyTree(com.taobao.android.builder.dependency.AndroidDependencyTree) Predicate(com.google.common.base.Predicate) ExecutorServicesHelper(com.taobao.android.builder.tools.concurrent.ExecutorServicesHelper) ToolOutputParser(com.android.ide.common.blame.parser.ToolOutputParser) ArrayList(java.util.ArrayList) List(java.util.List) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) ParsingProcessOutputHandler(com.android.ide.common.blame.ParsingProcessOutputHandler) ParsingProcessOutputHandler(com.android.ide.common.blame.ParsingProcessOutputHandler) ProcessOutputHandler(com.android.ide.common.process.ProcessOutputHandler) AtomicLong(java.util.concurrent.atomic.AtomicLong) DexParser(com.android.ide.common.blame.parser.DexParser) AwbTransform(com.android.build.gradle.internal.api.AwbTransform) GradleException(org.gradle.api.GradleException) AtomicLong(java.util.concurrent.atomic.AtomicLong) File(java.io.File) Nullable(javax.annotation.Nullable) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 4 with GradleException

use of org.gradle.api.GradleException in project atlas by alibaba.

the class ResolvedDependencyContainer method reslovedDependencies.

/**
     * 进行依赖仲裁
     */
public synchronized DependencyTree reslovedDependencies() {
    if (inited) {
        return dependencyTree;
    }
    if (!conflictDependencies.isEmpty()) {
        for (String str : conflictDependencies) {
            logger.error(str);
        }
        if (null == forceMap) {
            throw new GradleException("conflict dependencis");
        } else {
            AtlasBuildContext.conflictDependencies = conflictDependencies;
        }
    }
    // 仲裁后的依赖关系.结构为父类-子类
    Multimap<ModuleVersionIdentifier, ResolvedDependencyInfo> resolvedDependenciesMap = LinkedHashMultimap.create();
    Map<ModuleVersionIdentifier, ResolvedDependencyInfo> directDependencies = new HashMap<ModuleVersionIdentifier, ResolvedDependencyInfo>();
    for (String key : dependenciesMap.keySet()) {
        Collection<ResolvedDependencyInfo> dependencyLevels = dependenciesMap.get(key);
        if (dependencyLevels.size() > 0) {
            List<ResolvedDependencyInfo> resolvedDependencyInfos = Lists.newArrayList();
            resolvedDependencyInfos.addAll(dependencyLevels);
            Collections.sort(resolvedDependencyInfos);
            ResolvedDependencyInfo resolvedDependencyInfo = resolvedDependencyInfos.get(0);
            ResolvedDependencyInfo parent = resolvedDependencyInfo.getParent();
            //对于放入resolvedDependenciesMap中的信息不需要加入children
            resolvedDependencyInfo.setChildren(Lists.<ResolvedDependencyInfo>newArrayList());
            if (null != parent) {
                //如果存在的父依赖,就把当前依赖加入到父依赖的子依赖中
                resolvedDependenciesMap.put(parent.getModuleVersionIdentifier(), resolvedDependencyInfo);
            } else {
                //如果没有父依赖,就是一级依赖
                directDependencies.put(resolvedDependencyInfo.getModuleVersionIdentifier(), resolvedDependencyInfo);
            }
        }
    }
    // 开始构建依赖树
    dependencyTree.clear();
    for (ModuleVersionIdentifier key : directDependencies.keySet()) {
        ResolvedDependencyInfo resolvedDependencyInfo = directDependencies.get(key);
        //清空children
        resolvedDependencyInfo.setChildren(Lists.<ResolvedDependencyInfo>newArrayList());
        addResolvedDependencyInfo(resolvedDependencyInfo, resolvedDependenciesMap);
        dependencyTree.addDependency(resolvedDependencyInfo);
    }
    inited = true;
    return dependencyTree;
}
Also used : ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) HashMap(java.util.HashMap) GradleException(org.gradle.api.GradleException)

Example 5 with GradleException

use of org.gradle.api.GradleException in project atlas by alibaba.

the class LogDependenciesTask method generate.

@TaskAction
void generate() {
    AndroidDependencyTree androidDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
    if (null == androidDependencyTree) {
        return;
    }
    File treeFile = new File(getProject().getBuildDir(), "outputs/dependencyTree-" + getVariantName() + ".json");
    File dependenciesFile = new File(getProject().getBuildDir(), "outputs/dependencies.txt");
    File versionProperties = new File(getProject().getBuildDir(), "outputs/version.properties");
    File buildInfo = new File(getProject().getBuildDir(), "outputs/build.txt");
    File atlasConfig = new File(getProject().getBuildDir(), "outputs/atlasConfig.json");
    appBuildInfo.setDependencyTreeFile(treeFile);
    appBuildInfo.setDependenciesFile(dependenciesFile);
    appBuildInfo.setVersionPropertiesFile(versionProperties);
    appBuildInfo.setBuildInfoFile(buildInfo);
    treeFile.delete();
    dependenciesFile.delete();
    versionProperties.delete();
    buildInfo.delete();
    treeFile.getParentFile().mkdirs();
    try {
        DependencyJson dependencyJson = androidDependencyTree.getDependencyJson();
        Collections.sort(dependencyJson.getMainDex());
        FileUtils.write(treeFile, JSON.toJSONString(dependencyJson, true));
        //add to ap
        appBuildInfo.getOtherFilesMap().put("awo/dependencyTree.json", treeFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        FileUtils.writeStringToFile(dependenciesFile, JSON.toJSONString(androidDependencyTree.getDependencyJson()));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        FileUtils.writeLines(versionProperties, getSortVersionList(androidDependencyTree));
    } catch (IOException e) {
        e.printStackTrace();
    }
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("gradleVersion:").append(getProject().getGradle().getGradleVersion()).append("\r\n");
    stringBuilder.append("mtlVersion:").append(Version.ANDROID_GRADLE_PLUGIN_VERSION).append("\r\n");
    stringBuilder.append("androidVersion:").append(PathUtil.getJarFile(AndroidBuilder.class)).append("\r\n");
    try {
        FileUtils.writeStringToFile(buildInfo, stringBuilder.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        FileUtils.write(atlasConfig, JSON.toJSONString(new AtlasExtensionOutput(appVariantContext.getAtlasExtension(), getVariantName()), true));
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (null != AtlasBuildContext.conflictDependencies && !AtlasBuildContext.conflictDependencies.isEmpty()) {
        try {
            FileUtils.writeLines(new File(getProject().getBuildDir(), "outputs/warning-dependencyConflict.properties"), AtlasBuildContext.conflictDependencies);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (appVariantContext.getAtlasExtension().getTBuildConfig().isAbortIfDependencyConflict()) {
            throw new GradleException("依赖冲突,具体见warning-dependencyConflict.properties");
        }
    }
}
Also used : AtlasExtensionOutput(com.taobao.android.builder.tools.guide.AtlasExtensionOutput) GradleException(org.gradle.api.GradleException) AndroidDependencyTree(com.taobao.android.builder.dependency.AndroidDependencyTree) DependencyJson(com.taobao.android.builder.dependency.output.DependencyJson) IOException(java.io.IOException) File(java.io.File) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Aggregations

GradleException (org.gradle.api.GradleException)99 File (java.io.File)33 IOException (java.io.IOException)25 TaskAction (org.gradle.api.tasks.TaskAction)12 ArrayList (java.util.ArrayList)10 MtlBaseTaskAction (com.taobao.android.builder.tasks.manager.MtlBaseTaskAction)9 AndroidDependencyTree (com.taobao.android.builder.dependency.AndroidDependencyTree)8 UncheckedIOException (org.gradle.api.UncheckedIOException)7 HashSet (java.util.HashSet)6 AwbBundle (com.taobao.android.builder.dependency.model.AwbBundle)5 FileOutputStream (java.io.FileOutputStream)5 Method (java.lang.reflect.Method)5 ProcessOutputHandler (com.android.ide.common.process.ProcessOutputHandler)4 URL (java.net.URL)4 List (java.util.List)4 Map (java.util.Map)4 ConsoleRenderer (org.gradle.internal.logging.ConsoleRenderer)4 ClassReader (org.objectweb.asm.ClassReader)4 ExecutorServicesHelper (com.taobao.android.builder.tools.concurrent.ExecutorServicesHelper)3 MalformedURLException (java.net.MalformedURLException)3