android studio 将library导出为jar 亲测成功

本人使用的是helloChart这个开源项目,其主要用于图表,来自git 地址为:https://github.com/lecho/hellocharts-android

下载命令为 git clone https://github.com/lecho/hellocharts-android.git

由于项目需求修改了hellocharts-library这个module中的源码,并且集成进eclipse项目因此需要制作jar包。也可以自己新建library module项目尝试。

步骤一:(mac下)添加全局变量gradle,打开终端,输入gradle,出现下面信息表示以成功添加全局的gradle,若没有则打开Finder➡️应用程序➡️Android studio.app➡️点击鼠标右键选择“显示包内容”➡️Contents➡️gradle➡️gradle-2.10(可能与你不一致但差不多)➡️bin,打开终端将bin目录拖入,获取bin目录的绝对路径,接着打开bash_profile设置全局,输入命令:open -t ~/.bash_profile 会打开一个文件,添加一下export PATH=$PATH:/Applications/Android\ Studio.app/Contents/gradle/gradle-2.10/bin其中冒号:之后的为刚刚获取的bin的绝对路径,保存后关闭。之后在终端中输入gradle,显示以下图片内容(注意:bash_profile 操作与windows下设置环境变量效果相同,不清楚请先百度。)

步骤二:打开属于hellocharts-library项目的build.gradle,在文件的最最末尾(最后一个“}”下面)输入以下代码保存。其中helloChart为需要保存的文件名。

task makeJar(type: Copy) {
    delete 'build/libs/helloChart.jar'
    from('build/intermediates/bundles/release/')
    into('build/libs/')
    include('classes.jar')
    rename ('classes.jar', 'helloChart.jar')
}
makeJar.dependsOn(build)

步骤三:cd 命令进入hellocharts-library文件夹 输入命令  gradle makeJar

xxxxdeMacBook-Pro:hellocharts-library minyuchun$ gradle makeJar

如果为第一次使用程序应该会下载与mave2有关的内容,等着让它下,内容不多,最后在爆出100个警告后终端中输出以下内容

:hellocharts-library:prepareReleaseUnitTestDependencies
:hellocharts-library:compileReleaseUnitTestJavaWithJavac UP-TO-DATE
:hellocharts-library:processReleaseUnitTestJavaRes UP-TO-DATE
:hellocharts-library:compileReleaseUnitTestSources UP-TO-DATE
:hellocharts-library:assembleReleaseUnitTest UP-TO-DATE
:hellocharts-library:testReleaseUnitTest
:hellocharts-library:test
:hellocharts-library:check
:hellocharts-library:build
:hellocharts-library:makeJar

BUILD SUCCESSFUL

Total time: 8 mins 2.892 secs

This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.10/userguide/gradle_daemon.html

我们就可以查看下 hellocharts-library下的build下的libs下是否存在helloChart.jar文件。将其拷贝如项目尝试。

eslipse中的library项目导出  http://blog.csdn.net/wwj_748/article/details/21937257

若出现文件出错请先解决代码中waring警告,clean代码后重新导出。

注意:在制作jar包的时候 尽量注意要制作的项目的版本,最好是版本一样或者低版本。

posted @ 2016-07-19 10:59  化身程序猿  阅读(7845)  评论(0编辑  收藏  举报