Android将library项目打包成jar文件

时间:2021-09-12 21:21:01

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

application/build.gradle

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

找到新建的library目录下的buidle.gradle,在末尾添加:

 1 //task to delete the old jar
 2 task deleteOldJar(type: Delete) {
 3     delete 'release/AndroidPlugin.jar'
 4 }
 5  
 6 //task to export contents as jar
 7 task exportJar(type: Copy) {
 8     from('build/intermediates/bundles/release/')
 9     into('release/')
10     include('classes.jar')
11     ///Rename the jar
12     rename('classes.jar', 'AndroidPlugin.jar')
13 }
14  
15 exportJar.dependsOn(deleteOldJar, build)


然后sync Now 或者 sync gradle message。

点击展开mylibrary,找到exportJar,双击开始打包。

最后jar包存放的路径在 reslease 文件夹

 文章原文引用如下

https://blog.csdn.net/onlysnail/article/details/45123837#