Search in sources :

Example 26 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 27 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 28 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)

Example 29 with GradleException

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

the class JavacAwbsTask method run.

@TaskAction
void run() 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 {
                    AwbJavaCompileConfigAction awbJavaCompileConfigAction = new AwbJavaCompileConfigAction(awbBundle, appVariantOutputContext);
                    JavaCompile awbJavaCompile = TaskCreater.create(getProject(), awbJavaCompileConfigAction.getName(), awbJavaCompileConfigAction.getType());
                    awbJavaCompileConfigAction.execute(awbJavaCompile);
                    awbJavaCompile.execute();
                    AwbTransform awbTransform = appVariantOutputContext.getAwbTransformMap().get(awbBundle.getName());
                    awbTransform.setInputDir(awbJavaCompile.getDestinationDir());
                } catch (Throwable e) {
                    e.printStackTrace();
                    throw new GradleException("javac " + awbBundle.getName() + " failed");
                }
            }
        });
    }
    executorServicesHelper.execute(runnables);
}
Also used : ExecutorServicesHelper(com.taobao.android.builder.tools.concurrent.ExecutorServicesHelper) AwbTransform(com.android.build.gradle.internal.api.AwbTransform) GradleException(org.gradle.api.GradleException) ArrayList(java.util.ArrayList) AndroidDependencyTree(com.taobao.android.builder.dependency.AndroidDependencyTree) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) JavaCompile(org.gradle.api.tasks.compile.JavaCompile) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 30 with GradleException

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

the class MergeAwbResourceConfigAction method execute.

@Override
public void execute(MergeResources mergeResourcesTask) {
    VariantScope scope = variantContext.getScope();
    final BaseVariantData<? extends BaseVariantOutputData> variantData = scope.getVariantData();
    final AndroidConfig extension = scope.getGlobalScope().getExtension();
    //final VariantConfiguration variantConfig = variantData.getVariantConfiguration();
    mergeResourcesTask.setMinSdk(variantData.getVariantConfiguration().getMinSdkVersion().getApiLevel());
    mergeResourcesTask.setAndroidBuilder(scope.getGlobalScope().getAndroidBuilder());
    mergeResourcesTask.setVariantName(scope.getVariantConfiguration().getFullName());
    GlobalScope globalScope = scope.getGlobalScope();
    mergeResourcesTask.setIncrementalFolder(scope.getIncrementalDir(getName()));
    try {
        FieldUtils.writeField(mergeResourcesTask, "variantScope", scope, true);
    } catch (IllegalAccessException e) {
        throw new GradleException("exception", e);
    }
    // where blame is useful, and once for packaging where it is not.
    if (includeDependencies) {
        mergeResourcesTask.setBlameLogFolder(scope.getResourceBlameLogDir());
    } else {
    //            if (variantContext instanceof AppVariantContext) {
    //                mergeResourcesTask.setBlameLogFolder(((AppVariantContext) variantContext).getAwbBlameLogFolder(awbBundle));
    //            }
    }
    mergeResourcesTask.setProcess9Patch(process9Patch);
    mergeResourcesTask.setCrunchPng(extension.getAaptOptions().getCruncherEnabled());
    VectorDrawablesOptions vectorDrawablesOptions = variantData.getVariantConfiguration().getMergedFlavor().getVectorDrawables();
    Set<String> generatedDensities = vectorDrawablesOptions.getGeneratedDensities();
    mergeResourcesTask.setGeneratedDensities(Objects.firstNonNull(generatedDensities, Collections.<String>emptySet()));
    mergeResourcesTask.setDisableVectorDrawables(vectorDrawablesOptions.getUseSupportLibrary() || mergeResourcesTask.getGeneratedDensities().isEmpty());
    //mergeResourcesTask.setUseNewCruncher(extension.getAaptOptions().getUseNewCruncher());
    final boolean validateEnabled = AndroidGradleOptions.isResourceValidationEnabled(scope.getGlobalScope().getProject());
    mergeResourcesTask.setValidateEnabled(validateEnabled);
    ConventionMappingHelper.map(mergeResourcesTask, "inputResourceSets", new Callable<List<ResourceSet>>() {

        @Override
        public List<ResourceSet> call() throws Exception {
            List<ResourceSet> resourceSets = Lists.newArrayList();
            List<? extends AndroidLibrary> bundleDeps = awbBundle.getLibraryDependencies();
            // the list of dependency must be reversed to use the right overlay order.
            for (int n = bundleDeps.size() - 1; n >= 0; n--) {
                AndroidLibrary dependency = bundleDeps.get(n);
                File resFolder = dependency.getResFolder();
                if (resFolder.isDirectory()) {
                    ResourceSet resourceSet = new ResourceSet(dependency.getFolder().getName(), true);
                    resourceSet.addSource(resFolder);
                    resourceSets.add(resourceSet);
                }
            }
            File awbResourceFolder = awbBundle.getResFolder();
            if (awbResourceFolder.isDirectory()) {
                ResourceSet resourceSet = new ResourceSet(awbBundle.getFolder().getName(), true);
                resourceSet.addSource(awbResourceFolder);
                resourceSets.add(resourceSet);
            }
            return resourceSets;
        }
    });
    mergeResourcesTask.setOutputDir(variantContext.getMergeResources(awbBundle));
    mergeResourcesTask.setGeneratedPngsOutputDir(variantContext.getPngsOutputDir(awbBundle));
//        if (variantContext instanceof AppVariantContext) {
//            AppVariantContext appVariantContext = (AppVariantContext) variantContext;
//            appVariantContext.getAwbMergeResourceTasks().put(awbBundle.getName(), mergeResourcesTask);
//        }
}
Also used : GlobalScope(com.android.build.gradle.internal.scope.GlobalScope) VariantScope(com.android.build.gradle.internal.scope.VariantScope) ResourceSet(com.android.ide.common.res2.ResourceSet) AndroidConfig(com.android.build.gradle.AndroidConfig) GradleException(org.gradle.api.GradleException) VectorDrawablesOptions(com.android.builder.model.VectorDrawablesOptions) AndroidLibrary(com.android.builder.model.AndroidLibrary) GradleException(org.gradle.api.GradleException) List(java.util.List) File(java.io.File)

Aggregations

GradleException (org.gradle.api.GradleException)114 File (java.io.File)40 IOException (java.io.IOException)32 TaskAction (org.gradle.api.tasks.TaskAction)16 ArrayList (java.util.ArrayList)10 UncheckedIOException (org.gradle.api.UncheckedIOException)10 MtlBaseTaskAction (com.taobao.android.builder.tasks.manager.MtlBaseTaskAction)9 AndroidDependencyTree (com.taobao.android.builder.dependency.AndroidDependencyTree)8 HashSet (java.util.HashSet)8 FileOutputStream (java.io.FileOutputStream)6 Method (java.lang.reflect.Method)6 UncheckedException (org.gradle.internal.UncheckedException)6 AwbBundle (com.taobao.android.builder.dependency.model.AwbBundle)5 URL (java.net.URL)5 List (java.util.List)5 ConsoleRenderer (org.gradle.internal.logging.ConsoleRenderer)5 ProcessOutputHandler (com.android.ide.common.process.ProcessOutputHandler)4 FileInputStream (java.io.FileInputStream)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 MalformedURLException (java.net.MalformedURLException)4