输出 Apk 所有的依赖

2018-02-05  本文已影响157人  轻微

dim.red
代码片段


project.getGradle().addBuildListener(new BuildListener() {

    @Override
    void buildStarted(Gradle gradle) {

    }

    @Override
    void settingsEvaluated(Settings settings) {

    }

    @Override
    void projectsLoaded(Gradle gradle) {

    }

    @Override
    void projectsEvaluated(Gradle gradle) {

    }

    @Override
    void buildFinished(BuildResult result) {

        project.android.applicationVariants.each { variant ->
            if (variant.getAssemble().state.executed) {
                if (this.gradle.gradleVersion >= "4.1") {
                    // android plugin 3.0 以上
                    def runtime = project.configurations.create("d_analyse" + variant.runtimeConfiguration.name);
                    runtime.setCanBeConsumed(false);
                    runtime.extendsFrom(variant.runtimeConfiguration);
                    runtime.getAttributes().attribute(Attribute.of("artifactType", String.class), "jar")
                    variant.runtimeConfiguration.getAttributes().keySet().forEach({
                        runtime.getAttributes().attribute(it, variant.runtimeConfiguration.getAttributes().getAttribute(it));
                    })
                    println("${variant.name}  analyse")
                    println("runtime dependency ")
                    println("------------------")
                    runtime.resolvedConfiguration.resolvedArtifacts.forEach({
                        def id = it.moduleVersion.id;
                        println(id.group + " " + id.name + " " + id.version)
                    })
                    println("------------------\n ")
                    def packageC = project.configurations.create("d_analyse_" + variant.compileConfiguration.name);
                    packageC.setCanBeConsumed(false);
                    packageC.extendsFrom(variant.compileConfiguration);
                    packageC.getAttributes().attribute(Attribute.of("artifactType", String.class), "jar")
                    variant.compileConfiguration.getAttributes().keySet().forEach({
                        packageC.getAttributes().attribute(it, variant.compileConfiguration.getAttributes().getAttribute(it));
                    })
                    println("package dependency ")
                    println("------------------")
                    packageC.resolvedConfiguration.resolvedArtifacts.forEach({
                        def id = it.moduleVersion.id;
                        println(id.group + " " + id.name + " " + id.version)
                    })
                    println("------------------\n ")
                    println("\n ")

                } else {
                    // android plugin 3.0  以下
                    def appPlugin = project.plugins.findPlugin("com.android.application");
                    appPlugin.variantManager.variantDataList.forEach({
                        def variantDependencies = it.variantDependency;

                        if (variant.name == variantDependencies.name) {
                            println("${variant.name}  analyse")
                            def runtime = project.configurations.create("d_analyse" + variantDependencies.compileConfiguration.name);
                            runtime.setCanBeConsumed(false)
                            runtime.extendsFrom(variantDependencies.compileConfiguration);
                            println("runtime dependency ")
                            println("------------------")
                            runtime.resolvedConfiguration.resolvedArtifacts.forEach({
                                def id = it.moduleVersion.id;
                                println(id.group + " " + id.name + " " + id.version)
                            })
                            println("------------------\n ")

                            def packageC = project.configurations.create("d_analyse" + variantDependencies.packageConfiguration.name);
                            packageC.setCanBeConsumed(false)
                            packageC.extendsFrom(variantDependencies.packageConfiguration);
                            println("package dependency ")
                            println("------------------")
                            packageC.resolvedConfiguration.resolvedArtifacts.forEach({
                                def id = it.moduleVersion.id;
                                println(id.group + " " + id.name + " " + id.version)
                            })
                            println("------------------\n ")
                            println("\n ")
                        }

                    })

                }
            }

        }
    }
});

上一篇下一篇

猜你喜欢

热点阅读