0

使用 Gradle Copy 任务的问题:

def loopJar(File file) {
    zipTree(file).findAll { that ->
        that
    }.collect { the ->
        if (the.getName().endsWith(".jar") || the.getName().endsWith(".aar")) {
            loopJar(the)
        }
        the;
    }
}

task fatJar(type: Copy) {
    into 'libs'
    include { it.toString().contains("META-INF") }
    from {
        configurations.compile.findAll {
            it.getName() != 'android.jar'
        }.collect {
            loopJar(it);
        }
    }
}

非常简单,遍历所有依赖项,如果它们是存档,则遍历它们的 zipTrees 以确保它们本身没有 Jar。

完成此操作后,我想将 META-INF 文件夹中的所有文件复制到我的目录中。

出于某种原因,它将所有文件复制到目录中。但是,如果我在 eachFile 中 println,它只会显示我想要复制的文件。

      file '/{Dir Structure Here}/build/tmp/expandedArchives/activeandroid-3.1.0-SNAPSHOT.jar_duhgtuy24rfyj4gq0cc29tyjl/META-INF/MANIFEST.MF'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-annotations-2.4.1.jar_asorliiuo3vke643z99zgpvqb/META-INF/LICENSE'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-annotations-2.4.1.jar_asorliiuo3vke643z99zgpvqb/META-INF/MANIFEST.MF'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-annotations-2.4.1.jar_asorliiuo3vke643z99zgpvqb/META-INF/maven/com.fasterxml.jackson.core/jackson-annotations/pom.properties'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-annotations-2.4.1.jar_asorliiuo3vke643z99zgpvqb/META-INF/maven/com.fasterxml.jackson.core/jackson-annotations/pom.xml'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-databind-2.4.1.3.jar_av3gri85ykjeek8zq4zffxj7c/META-INF/LICENSE'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-databind-2.4.1.3.jar_av3gri85ykjeek8zq4zffxj7c/META-INF/MANIFEST.MF'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-databind-2.4.1.3.jar_av3gri85ykjeek8zq4zffxj7c/META-INF/NOTICE'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-databind-2.4.1.3.jar_av3gri85ykjeek8zq4zffxj7c/META-INF/maven/com.fasterxml.jackson.core/jackson-databind/pom.properties'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-databind-2.4.1.3.jar_av3gri85ykjeek8zq4zffxj7c/META-INF/maven/com.fasterxml.jackson.core/jackson-databind/pom.xml'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-databind-2.4.1.3.jar_av3gri85ykjeek8zq4zffxj7c/META-INF/services/com.fasterxml.jackson.core.ObjectCodec'
file '/{Dir Structure Here}/build/tmp/expandedArchives/support-annotations-23.1.1.jar_59cc6fqykevukbtu3fq75wbi0/META-INF/MANIFEST.MF'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-core-2.4.1.1.jar_cf7fa55yohvqtmkkzf26lmy55/META-INF/LICENSE'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-core-2.4.1.1.jar_cf7fa55yohvqtmkkzf26lmy55/META-INF/MANIFEST.MF'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-core-2.4.1.1.jar_cf7fa55yohvqtmkkzf26lmy55/META-INF/NOTICE'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-core-2.4.1.1.jar_cf7fa55yohvqtmkkzf26lmy55/META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.properties'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-core-2.4.1.1.jar_cf7fa55yohvqtmkkzf26lmy55/META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.xml'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-core-2.4.1.1.jar_cf7fa55yohvqtmkkzf26lmy55/META-INF/services/com.fasterxml.jackson.core.JsonFactory'

这是每个文件的输出。然而所有文件都进入 libs 文件夹......什么!?

此脚本的用法:复制所有 meta-inf 文件(最终只是许可、通知信息)并将它们添加到我的 AAR 文件中的自己的文件夹中,以允许其他希望包含我的 aar 的人这样做而不会出现冲突的文件问题: Android Gradle 插件 0.7.0:“打包 APK 期间重复文件”

4

1 回答 1

1
def loopJar(files, Map l) {
    files.findAll {
        if (!(it instanceof File))
            return false;
        it.getName() != 'android.jar'
    }.collect { file ->
        List e = new ArrayList();
        zipTree(file).each { the ->
            if (the.getName().endsWith(".jar") || the.getName().endsWith(".aar")) {
                loopJar(the, l)
            }
            if (the.getPath().contains("META-INF"))
                e.add(the);
        }
        if(!e.isEmpty())
        l.put(file.getName(), e);
    }
}

task fatJar << {
    Map l = new HashMap();
    loopJar(configurations.compile, l);
    l.each { key,val ->
        val.each {
            copy {
                from val
                into "licensing/"+key+"/"
            }
        }
    }
}

很简单的解决方法:用 Java 方式编写。

于 2015-12-03T17:30:07.207 回答