Search in sources :

Example 31 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 32 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 33 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)

Example 34 with GradleException

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

the class ExecutorServicesHelper method execute.

public void execute(List<Runnable> runnables) throws InterruptedException {
    if (runnables.isEmpty()) {
        return;
    }
    final CountDownLatch countDownLatch = new CountDownLatch(runnables.size());
    final int size = runnables.size();
    for (final Runnable runnable : runnables) {
        this.executorService.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    if (!hasException) {
                        info("excute " + name + " task at " + index.incrementAndGet() + "/" + size);
                        runnable.run();
                    }
                } catch (GradleException gradleException) {
                    hasException = true;
                    exception = gradleException;
                } finally {
                    countDownLatch.countDown();
                }
            }
        });
    }
    countDownLatch.await();
    if (hasException) {
        throw new GradleException(exception.getMessage(), exception);
    }
}
Also used : GradleException(org.gradle.api.GradleException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 35 with GradleException

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

the class ExecutorServicesHelper method execute.

public <T> void execute(BlockingQueue<T> blockingQueue, Handler<T> handler) throws InterruptedException {
    if (blockingQueue.isEmpty()) {
        return;
    }
    final CountDownLatch countDownLatch = new CountDownLatch(this.threadCount);
    for (int i = 0; i < this.threadCount; i++) {
        this.executorService.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    while (true) {
                        T t = blockingQueue.poll();
                        if (null == t) {
                            break;
                        }
                        handler.handle(t);
                    }
                } catch (GradleException gradleException) {
                    hasException = true;
                    exception = gradleException;
                } finally {
                    countDownLatch.countDown();
                }
            }
        });
    }
    countDownLatch.await();
    if (hasException) {
        throw new GradleException(exception.getMessage(), exception);
    }
}
Also used : GradleException(org.gradle.api.GradleException) CountDownLatch(java.util.concurrent.CountDownLatch)

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