gradle 编译打包并使用 aar

来源:互联网 发布:淘宝二手苹果能买吗 编辑:程序博客网 时间:2024/06/06 20:22

一般在开发过程中我们会将一些公共组件库进行打包使用,在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 文件

          

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">buildscript {  
  2.     repositories {  
  3.         mavenCentral()  
  4.     }  
  5.     dependencies {  
  6.         classpath 'com.android.tools.build:gradle:0.9.+'  
  7.     }  
  8. }  
  9.   
  10. apply plugin: 'android-library'  
  11. apply plugin: 'maven'  
  12.   
  13. android {  
  14.     compileSdkVersion 19  
  15.     buildToolsVersion "19.0.0"  
  16.     packagingOptions {  
  17.         exclude 'META-INF/LICENSE.txt'  
  18.         exclude 'META-INF/NOTICE.txt'  
  19.     }  
  20.     lintOptions {  
  21.         abortOnError false  
  22.     }  
  23. }  
  24. dependencies {  
  25.     compile fileTree(dir: 'libs', include: ['*.jar'])  
  26. }  
  27.   
  28.   
  29. uploadArchives {  
  30.     repositories.mavenDeployer {  
  31.         name = 'mavenCentralReleaseDeployer'  
  32.         repository(url: "http://xxx.xxx.xxx.xx:8080/content/repositories/releases") {  
  33.             authentication(userName: "xxxx", password: "xxxx")  
  34.         }  
  35.         snapshotRepository(url: "http://xxx.xxx.xxx.xx:8080/content/repositories/snapshots") {  
  36.             authentication(userName: "xxxx", password: "xxxx")  
  37.         }  
  38.         pom.version = "1.0.1-SNAPSHOT"  
  39.         pom.artifactId = "sdk"  
  40.         pom.groupId = "com.xx.xxx.sdk"  
  41.         pom.name = "xxx"  
  42.         pom.packaging = 'aar'  
  43.     }  
  44. }  
  45. </span>  
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <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'  


0 0