Android将library项目打包成jar文件

非淡泊无以明志,非宁静无以致远。这篇文章主要讲述Android将library项目打包成jar文件相关的知识,希望能为你提供帮助。
1 > > > File - new Module -  Android  Library   - finish
这样就创建了一个library项目。
这里比较libaray/build.gradle与application/build.gradle的不同之处,很清楚,就不多说了。
libaray/build.gradle

1 apply plugin: ‘com.android.library‘ 2 3 android { 4compileSdkVersion 21 5buildToolsVersion "21.1.2" 6 7defaultConfig { 8minSdkVersion 15 9targetSdkVersion 21 10versionCode 1 11versionName "1.0" 12} 13buildTypes { 14release { 15minifyEnabled false 16proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘ 17} 18} 19 } 20 21 dependencies { 22compile fileTree(dir: ‘libs‘, include: [‘*.jar‘]) 23compile ‘com.android.support:appcompat-v7:21.0.3‘ 24 }

application/build.gradle
1 apply plugin: ‘com.android.application‘ 2 3 buildscript { 4repositories { 5jcenter() 6} 7 8dependencies { 9classpath ‘com.android.tools.build:gradle:1.0.0‘ 10} 11 } 12 13 repositories { 14jcenter() 15 } 16 17 dependencies { 18compile fileTree(dir: ‘libs‘, include: ‘*.jar‘) 19compile project(‘:library‘) 20 } 21 android { 22compileSdkVersion 21 23buildToolsVersion "21.1.2" 24 25sourceSets { 26main { 27manifest.srcFile ‘AndroidManifest.xml‘ 28java.srcDirs = [‘src‘] 29res.srcDirs = [‘res‘] 30assets.srcDirs = [‘assets‘] 31} 32} 33 }

找到新建的library目录下的buidle.gradle,在末尾添加:
1 //task to delete the old jar 2 task deleteOldJar(type: Delete) { 3delete ‘release/AndroidPlugin.jar‘ 4 } 5 6 //task to export contents as jar 7 task exportJar(type: Copy) { 8from(‘build/intermediates/bundles/release/‘) 9into(‘release/‘) 10include(‘classes.jar‘) 11///Rename the jar 12rename(‘classes.jar‘, ‘AndroidPlugin.jar‘) 13 } 14 15 exportJar.dependsOn(deleteOldJar, build)


然后sync Now 或者 sync gradle message。
点击展开mylibrary,找到exportJar,双击开始打包。
【Android将library项目打包成jar文件】最后jar包存放的路径在 reslease 文件夹
  文章原文引用如下
https://blog.csdn.net/onlysnail/article/details/45123837#


    推荐阅读