android aar 使用

原文链接http://www.jianshu.com/p/59efa895589e

一. 简介

aar是一个类似于jar的文件格式。但是他们之间是有区别的。
jar:仅仅包含class和清单文件,没有资源文件。
aar:包含了class文件和资源文件。说白了就是Android的专属“jar”

将代码打包成aar文件,可以在一定程度上加快AndroidStudio的速度。
尤其是将Module打包成aar文件,提升的效果很显著。

二. 如何得到aar

1. Module的aar文件

将一个AndroidStudio项目中的Module打包成aar其实很简单。
在每一个Module的目录下面都会有这样一个文件夹:build\outputs\aar
这个文件夹下面就放着这个Module对应的aar文件。
一般情况下会有两个aar文件,一个debug版本,一个release版本。
我们选择release的就ok。






三. 如何使用aar

想要使用aar文件,需要经过以下几步:

1. 在app的build.gradle中加入以下配置
repositories {    
    flatDir {        
        dirs 'libs'   // aar目录
      }
}
2. 将aar文件拷贝到app/libs目录下
3. 在dependencies中加入aar引用
compile(name: 'zbar-release', ext: 'aar')

四:Demo

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '25.0.0'
    defaultConfig {
        applicationId "xxx"
        minSdkVersion 20
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

  

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
	//生成jniLibs目录 存放 aar(可有可无)
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
	// 可以放在android{} 也可以与android 同级别。
    repositories {
        flatDir {
            dirs 'libs'
        }
    }

}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+', {
        exclude group: 'com.android.support', module: 'appcompat-v7'
    }
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile(name: '生成的aar名称', ext: 'aar')
}










猜你喜欢

转载自blog.csdn.net/ding1145536113/article/details/78084383