一般在开发过程中我们会将一些公共组件库进行打包使用,在Android下打包莫过于2种一种是jar ,一种是aar,关于如何通过android studio打包 jar 可以参考我的另外一篇文章: http://blog.csdn.net/wangjia55/article/details/31377637 , 本节主要介绍如何打包aar 。
gradle 对maven仓库的依赖非常好,所以在使用aar之前,需要将aar上传到maven仓库中,(一般都是部门自己搭建的私有maven仓库,如何搭建此处跳过)。
步骤如下:
1. 整理工程结构
新版本的android studio 新建工程默认除了包含有主project之外还有一个moudle,现在需要将工程结构改成library的结构。
1.1 将moudle中的xx.iml 文件中内容copy 到主项目下的xxx.iml 文件中。
1.2 删除默认创建的moudle
1.3 修改主工程的build.gradle 文件
- <span style="font-size:14px;">buildscript {
- repositories {
- mavenCentral()
- }
- dependencies {
- classpath 'com.android.tools.build:gradle:0.9.+'
- }
- }
-
- apply plugin: 'android-library'
- apply plugin: 'maven'
-
- android {
- compileSdkVersion 19
- buildToolsVersion "19.0.0"
- packagingOptions {
- exclude 'META-INF/LICENSE.txt'
- exclude 'META-INF/NOTICE.txt'
- }
- lintOptions {
- abortOnError false
- }
- }
- dependencies {
- compile fileTree(dir: 'libs', include: ['*.jar'])
- }
-
-
- uploadArchives {
- repositories.mavenDeployer {
- name = 'mavenCentralReleaseDeployer'
- repository(url: "http://xxx.xxx.xxx.xx:8080/content/repositories/releases") {
- authentication(userName: "xxxx", password: "xxxx")
- }
- snapshotRepository(url: "http://xxx.xxx.xxx.xx:8080/content/repositories/snapshots") {
- authentication(userName: "xxxx", password: "xxxx")
- }
- pom.version = "1.0.1-SNAPSHOT"
- pom.artifactId = "sdk"
- pom.groupId = "com.xx.xxx.sdk"
- pom.name = "xxx"
- pom.packaging = 'aar'
- }
- }
- </span>
- <span style="font-size:14px;">最有运行 </span><pre style="margin: 1em 1em 1em 1.6em; padding: 8px; background-color: rgb(250, 250, 250); border: 1px solid rgb(226, 226, 226); width: auto; overflow-x: auto; overflow-y: hidden; color: rgb(72, 72, 72); "><span style="font-size:14px;">gradle clean uploadArchives </span>
这样就可以打包成aar包,并上传到maven仓库中了。
2.如何在新的工程中使用刚才的aar呢?
在新工程的build.gradle文件中加上:
compile 'com.xxx.xxx.sdk:SDK:1.0.1-SNAPSHOT'