Search in sources :

Example 6 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 7 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)

Example 8 with GradleException

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

the class PreparePackageIdsTask method generate.

/**
     * packageid 范围是 30 - 127
     */
@TaskAction
void generate() throws IOException {
    File packageIdFile = appVariantContext.getAtlasExtension().getTBuildConfig().getPackageIdFile();
    File apPackageIdFile = appVariantContext.apContext.getPackageIdFile();
    boolean isAutoPackageId = appVariantContext.getAtlasExtension().getTBuildConfig().isAutoPackageId();
    Map<String, String> autoConfigMap = new HashMap<String, String>();
    if (null != apPackageIdFile && apPackageIdFile.exists()) {
        autoConfigMap.putAll(loadPackageIdProperties(apPackageIdFile));
    } else if (null != packageIdFile && packageIdFile.exists()) {
        autoConfigMap.putAll(loadPackageIdProperties(packageIdFile));
    }
    AndroidDependencyTree androidDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
    for (AwbBundle awbBundle : androidDependencyTree.getAwbBundles()) {
        String key = awbBundle.getResolvedCoordinates().getGroupId() + ":" + awbBundle.getResolvedCoordinates().getArtifactId();
        if (autoConfigMap.containsKey(key)) {
            continue;
        }
        File customPackageIDFile = new File(awbBundle.getFolder(), "customPackageID.txt");
        String packageId = getCustomPackageId(customPackageIDFile);
        if (StringUtils.isNotEmpty(packageId) && StringUtils.isNumeric(packageId)) {
            autoConfigMap.put(key, packageId);
        } else {
            autoConfigMap.put(key, "");
        }
    }
    if (isAutoPackageId && autoConfigMap.containsValue("")) {
        //自动分配autoConfig
        List<String> keys = new ArrayList<String>(autoConfigMap.keySet());
        Collections.sort(keys);
        for (String key : keys) {
            if ("".equals(autoConfigMap.get(key))) {
                for (int i = 30; i <= 127; i++) {
                    if (!autoConfigMap.values().contains(String.valueOf(i))) {
                        autoConfigMap.put(key, String.valueOf(i));
                        break;
                    }
                }
            }
        }
    }
    AtlasBuildContext.customPackageIdMaps = autoConfigMap;
    //wite package Id file
    File outPkgFile = new File(getProject().getBuildDir(), "outputs/packageIdFile.properties");
    writeProperties(autoConfigMap, outPkgFile);
    appBuildInfo.setPackageIdFile(outPkgFile);
    //check value
    if (autoConfigMap.containsValue("")) {
        getLogger().error(JSON.toJSONString(autoConfigMap, true));
        throw new GradleException("packageId 配置错误,请检查");
    }
    if (autoConfigMap.size() != new HashSet(autoConfigMap.values()).size()) {
        //            getLogger().error(JSON.toJSONString(autoConfigMap, true));
        Map<String, PackageIdItem> idItemMap = new HashMap<String, PackageIdItem>();
        for (String key : autoConfigMap.keySet()) {
            String customPackageId = autoConfigMap.get(key);
            PackageIdItem packageIdItem = idItemMap.get(customPackageId);
            if (null == packageIdItem) {
                String[] split = customPackageId.split("\\.");
                packageIdItem = new PackageIdItem();
                packageIdItem.packageId = Integer.valueOf(split[0]);
                if (split.length > 1) {
                    packageIdItem.typeIdOffset = Integer.valueOf(split[1]);
                }
                idItemMap.put(customPackageId, packageIdItem);
            }
            packageIdItem.bundles.add(key);
        }
        Collection<PackageIdItem> collection = idItemMap.values();
        List<PackageIdItem> packageList = new ArrayList<PackageIdItem>(collection);
        Collections.sort(packageList, new Comparator<PackageIdItem>() {

            @Override
            public int compare(PackageIdItem o1, PackageIdItem o2) {
                return o1.packageId - o2.packageId;
            }
        });
        getLogger().error(JSON.toJSONString(packageList, true));
        throw new GradleException("packageId 配置错误,有重复,请检查");
    }
    // check if packageID is not used
    Map<String, String> autoConfigMap2 = new HashMap<>(autoConfigMap);
    for (AwbBundle awbBundle : androidDependencyTree.getAwbBundles()) {
        String key = awbBundle.getResolvedCoordinates().getGroupId() + ":" + awbBundle.getResolvedCoordinates().getArtifactId();
        autoConfigMap2.remove(key);
    }
    if (autoConfigMap2.size() > 0) {
        File outPkgFile2 = new File(getProject().getBuildDir(), "outputs/warning-unusedPackageIdFile.properties");
        writeProperties(autoConfigMap2, outPkgFile2);
    }
}
Also used : AndroidDependencyTree(com.taobao.android.builder.dependency.AndroidDependencyTree) GradleException(org.gradle.api.GradleException) AwbBundle(com.taobao.android.builder.dependency.model.AwbBundle) File(java.io.File) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 9 with GradleException

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

the class PreProcessManifestTask method preProcess.

@TaskAction
public void preProcess() throws IOException, DocumentException, InterruptedException {
    getLogger().info("[MTLPlugin]Start PreProcess Lib manifest files,main manifestFile is:" + getMainManifestFile());
    ExecutorServicesHelper executorServicesHelper = new ExecutorServicesHelper("preProcessDex", getLogger(), 0);
    List<Runnable> runnables = new ArrayList<>();
    ManifestFileObject mainManifestFileObject = ManifestFileUtils.getManifestFileObject(mainManifestFile);
    mainManifestFileObject.init();
    for (File file : getLibraryManifests()) {
        runnables.add(new Runnable() {

            @Override
            public void run() {
                try {
                    ManifestFileUtils.updatePreProcessManifestFile(file, mainManifestFileObject, true);
                } catch (Throwable e) {
                    throw new GradleException("preprocess manifest", e);
                }
            }
        });
    }
    executorServicesHelper.execute(runnables);
    //ManifestFileUtils.preProcessManifests(getMainManifestFile(), getLibraryManifests(), true);
    //BundleInfoUtils.setupAwbBundleInfos(appVariantOutputContext.getVariantContext());
    //collectBundleInfo();
    addAwbManifest2Merge();
}
Also used : ExecutorServicesHelper(com.taobao.android.builder.tools.concurrent.ExecutorServicesHelper) ManifestFileObject(com.taobao.android.builder.tools.manifest.ManifestFileObject) GradleException(org.gradle.api.GradleException) ArrayList(java.util.ArrayList) File(java.io.File) InputFile(org.gradle.api.tasks.InputFile) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Example 10 with GradleException

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

the class DiffBundleInfoTask method getMainArtifactBundInfo.

private static ArtifactBundleInfo getMainArtifactBundInfo(File manifestFile) {
    ArtifactBundleInfo mainBundleInfo = new ArtifactBundleInfo();
    SAXReader reader = new SAXReader();
    // 读取XML文件
    Document document = null;
    try {
        document = reader.read(manifestFile);
    } catch (DocumentException e) {
        throw new GradleException(e.getMessage(), e);
    }
    // 得到根节点
    Element root = document.getRootElement();
    List<? extends Node> metadataNodes = root.selectNodes("//meta-data");
    for (Node node : metadataNodes) {
        Element element = (Element) node;
        Attribute attribute = element.attribute("name");
        if (attribute.getValue().equals("label")) {
            Attribute labelAttribute = element.attribute("value");
            mainBundleInfo.setName(labelAttribute.getValue());
        }
    }
    List<? extends Node> applicatNodes = root.selectNodes("//application");
    for (Node node : applicatNodes) {
        Element element = (Element) node;
        Attribute attribute = element.attribute("name");
        if (attribute != null) {
            mainBundleInfo.setApplicationName(attribute.getValue());
        }
    }
    if ("manifest".equalsIgnoreCase(root.getName())) {
        List<Attribute> attributes = root.attributes();
        for (Attribute attr : attributes) {
            if (StringUtils.equalsIgnoreCase(attr.getName(), "versionName")) {
                String versionName = attr.getValue();
                mainBundleInfo.setVersion(versionName);
            }
        }
    }
    String pkgName = root.attributeValue("package");
    mainBundleInfo.setPkgName(pkgName);
    return mainBundleInfo;
}
Also used : ArtifactBundleInfo(com.taobao.android.object.ArtifactBundleInfo) SAXReader(org.dom4j.io.SAXReader) GradleException(org.gradle.api.GradleException)

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