Android Studio编译、导入aar库的方法

来源:互联网 发布:好一点的cos淘宝店 编辑:程序博客网 时间:2024/04/30 22:51
1.如何将一个工程编译成aar?
在工程的build.gradle中配置
[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. apply plugin: 'com.android.library'  
注意:在defaultConfig中不要定义applicationId,因为aar是库,无applicationId

2.如何在工程中导入aar?
(1)将编译好的aar文件放到libs下,类似jar的做法。譬如,libs/mylib.aar。
(2)在工程的build.gradle中添加:
[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. repositories{  
  2.     flatDir {  
  3.         dirs 'libs'  
  4.     }  
  5. }  
并且在dependencies中添加:
[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. compile(name: 'mylib', ext: 'aar')  

一个完整的build.gradle例子如下:
[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. apply plugin: 'com.android.application'  
  2.   
  3. repositories{  
  4.     flatDir {  
  5.         dirs 'libs'  
  6.     }  
  7. }  
  8.   
  9. android {  
  10.     compileSdkVersion 23  
  11.     buildToolsVersion "23.0.3"  
  12.   
  13.     defaultConfig {  
  14.         applicationId "com.example.testimportaar"  
  15.         minSdkVersion 21  
  16.         targetSdkVersion 23  
  17.         versionCode 1  
  18.         versionName "1.0"  
  19.     }  
  20.     buildTypes {  
  21.         release {  
  22.             minifyEnabled false  
  23.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  
  24.         }  
  25.     }  
  26. }  
  27.   
  28. dependencies {  
  29.     compile fileTree(dir: 'libs', include: ['*.jar'])  
  30.     testCompile 'junit:junit:4.12'  
  31.     compile 'com.android.support:appcompat-v7:23.4.0'  
  32.     compile(name: 'mylib', ext: 'aar')  
  33. }  
0 0
原创粉丝点击