Gradle任务构建Jar不包括android类

时间:2018-06-05 14:52:39

标签: java android gradle jar

我正在编写一个简单的Gradle任务,该任务应该接受所有中间类并将它们打包到.jar文件中,该任务将插入到用于构建Android库的一系列gradle任务中。 / p>

请注意,Android库未打包在*.aar存档中,我有一些任务可以构建jar,构建一些共享对象并将它们放在目录中。

到目前为止我写的任务如下:

task createDevJarRelease(type: Jar) {
    from 'build/intermediates/classes/release'

    manifest {
        attributes 'Implementation-Title': 'MYJar library',  
            'Implementation-Version': '1.0'
    }

    archiveName "my-jar.jar"
    doLast {
        // Copy the jar in the target directory.
        copy {
            include "my-jar.jar"
            from    "build/libs/"
            into    "targetDir"
        }
    }
}

正在创建的jar是正确创建并放置在我的targetDir中但是我无法在android项目中实际使用它,因为jar文件包含了我的build/intermediates/classes/release文件类,也是Android依赖项中的其他类,当包含在Android应用程序中时会导致冲突。

我进入build/intermediates/classes/release |_ android | |_ arch | | |_ core | | |_ ... | | |_ lifecycle | | |_ ... | |_ support | |_ ... | | |_ ... | |_ ... |_ my |_ package |_ name |_ foo.class |_ bar.class 并注意到以下目录层次结构:

android

我尝试过在线找到的不同解决方案,以排除exclude目录下的类但没有成功。

我尝试在上面的Gradle任务中明确添加exclude 'build/intermediates/classes/release/android' 命令,但根本不起作用(类仍在那里)。

jar

使用此> Task :app:transformDexArchiveWithDexMergerForDebug Dex: Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files define Landroid/arch/lifecycle/R; UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Landroid/arch/lifecycle/R; com.android.dex.DexException: Multiple dex files define Landroid/arch/lifecycle/R; at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:661) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:616) at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:598) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171) at com.android.dx.merge.DexMerger.merge(DexMerger.java:198) at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:61) at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:36) at java.util.concurrent.ForkJoinTask$AdaptedCallable.exec(ForkJoinTask.java:1424) at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289) at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056) at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692) at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157) FAILURE: Build failed with an exception. 构建应用程序时看到的错误是:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'com.android.support:appcompat-v7:27.1.1'
    testImplementation 'junit:junit:4.12'
}

项目依赖项是:

new T[size]

1 个答案:

答案 0 :(得分:1)

实际的解决方案不是exclude gradle任务中的jar类,而是使用compileOnly代替implementation更改项目的依赖关系设置com.android.support依赖项。

修复方法是:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
     androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compileOnly 'com.android.support:appcompat-v7:27.1.1'
    testImplementation 'junit:junit:4.12'
}