用Gradle构建库jar [英] Build library jar with Gradle

查看:161
本文介绍了用Gradle构建库jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android Studio上开发一个库(一个SDK,我们假设模块A)。为了测试它,我还得到了一个demoApp



我还有另一个带有SDK依赖关系的库(一个插件,比方说模块B)

所以在我的项目中,有3个模块:SDK(A),demoApp和插件(B)

现在我需要将我的SDK和/或我的插件分发到jar文件中,以便人们可以在他们的项目中使用它。问题是:如何为SDK生成一个jar文件,以及如何生成一个jar文件jar包的插件(包括libs文件夹中的所有必需品依赖关系)与Gradle任务(我不需要任何资源或资产)?

I'对Gradle很新颖,我交叉这个答案,但我不' t知道它是否符合我的需求(需要与Android兼容)

编辑:澄清我添加的信件




  • 我需要一个jar文件作为A

  • B需要A来运行(依赖关系)

  • B的一个jar文件


解决方案

您需要创建一个新的gradle任务,类似于我的

 任务clearJar(类型:删除){
删除'../location/'+ LIBRARY_NAME +'_' + LIBRARY_VERSION +'.jar'
}

任务makeJar(类型:复制){
dependsOn clearJar,从'build / intermediates / bundles / release / '
转换为'../../location/'
包含'classes.jar'
重命名'classes.jar',LIBRARY_NAME +'_'+ LIBRARY_VERSION +'.jar'
}

其中 LIBRARY_NAME LIBRARY_VERSION 是我在gradle构建文件的顶部设置的字符串。



您可以添加此任务,然后选择它像这样




I'm developing a library on Android Studio (a SDK, let's say module A). To test it out, I also got a demoApp

I also have another lib with the SDK dependencies (a plugin, let's say module B)

So in my project, there are 3 modules : the SDK (A), the demoApp and the plugin (B)

Now I need to distribute my SDK and / or my plugin in jar files so people can use it in their project

The question is : how can I generate a jar for the SDK and a jar for the plugin (including all the necessaries dependencies of course like jar in libs folder) with Gradle tasks (I don't need any ressources or assets) ?

I'm very new to Gradle, I cross this answer but I don't know if it match my needs (need to be Android compatible)

EDIT : to clarify I added letter

  • I need a jar file for A
  • B need A to run (dependencies)
  • I need a jar file for B

解决方案

You need to create a new gradle task, similar to mine

task clearJar(type: Delete) {
    delete '../location/' + LIBRARY_NAME + '_' + LIBRARY_VERSION + '.jar'
}

task makeJar(type: Copy) {
    dependsOn clearJar, build
    from 'build/intermediates/bundles/release/'
    into '../../location/'
    include 'classes.jar'
    rename 'classes.jar', LIBRARY_NAME + '_' + LIBRARY_VERSION + '.jar'
}

Where LIBRARY_NAME and LIBRARY_VERSION are strings I set at the top of the gradle build file.

You can add this task and then select it like this

这篇关于用Gradle构建库jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆