Android Gradle 引用本地 AAR 的几种方式

折衷方案:

    1.方式2 - 不完美解决办法2
    2.再使用"自定义Gradle代码"来减轻重复设置的问题.

自定义Gradle代码如下:

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    //================================================================================
    // 批量导入 libs 里的所有 .aar 类库.
    //================================================================================
    def batchImportAar = {
        fileTree(dir: 'libs', include: '*.aar').each { File file ->
            def name = file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name };
            compile(name: name, ext: 'aar')
            //在内部类外时,可用以下等价形式.
            //dependencies.add("compile", [name: name, ext: 'aar'])
        }
    }
    batchImportAar()
    //================================================================================
    // 批量导入 libs 里的所有 .jar 类库.
    //================================================================================
    compile fileTree(dir: 'libs', include: ['*.jar'])
    //================================================================================
    // 正确引用自定义类库
    //================================================================================
    def compileLibrary = { name ->
        //鉴于某些类库同样引用了.aar类库,所以必须把所在路径也添加到当前项目的flatDir
        //否则会出现无法正确解析识别其引用的 .aar 类库.
        repositories.flatDir.dirs(project(name).file('libs'))
        //因为Studio本身的缺陷,导致无法正确处理 debug,release,导致引用的类库无法区分
        //是否为调试模式.所以需要同时添加两条配置,并结合类库自身的相关设置,方能解决.
        debugCompile project(path: name, configuration: 'debug')
        releaseCompile project(path: name, configuration: 'release')
    }
    compileLibrary(':EasyAndroid')
    compileLibrary(':DataSync_Android')
    compileLibrary(':EasyAndroid_QrCodeScan')
    compileLibrary(':EasyAndroid_Printer')
    compileLibrary(':EasyAndroid_OpenCamera')
    compileLibrary(':EasyAndroid_GifImageView')
    compileLibrary(':EasyAndroid_Downloader')
}

方式3:

    1. 把 AAR 放入 libs
    2. 在 build.gradle 添加 repositories{flatDir{dirs 'libs'}}
    3. 在 build.gradle 添加 dependencies{compile '包名:类库名:版本号@aar'}

优点√:

  1. 自己类库可以自己维护自己内部的AAR引用.
  2. 能像维护libs里的jar类库一样简单.
  3. dependencies 设置方式和在线解析引用的方式一样.

缺点×:

  1. 方式2的缺点
  2. dependencies 设置时需要放在 compile fileTree 的上面,否则无法识别.
  3. dependencies 设置的名字 和 在线解析引用的方式不一样.如
    在线解析方式:compile 'com.android.support:support-v4:23.+@aar'
    本地AAR方式:compile 'android.support:v4:23.+@aar'

如何设置正确的本地AAR名称?

  1. 解压AAR包,看AndroidManifest.xml里的 package="android.support.v4"
  2. 对应的就是名称就是 android.support:v4
  3. 然后必须设置AAR文件名为:v4-1.0.0.aar
  4. 最后拼接正确的版本号就是 android.support:v4:1.0.0

方式2:

    1. 把 AAR 放入 libs
    2. 在 build.gradle 添加 repositories{flatDir{dirs 'libs'}}
    3. 在 build.gradle 添加 dependencies{compile(name:'nameOfYourAARFile', ext:'aar')}

优点√:

  1. 自己类库可以自己维护自己内部的AAR引用.
  2. 能像维护libs里的jar类库一样简单.

缺点×:

  1. 在类库(library)中引用AAR库,在别的类库(library)或项目(application)中引用该类库时无法解析识别其引用的AAR库.

此问题可通过以下方法不太完美的解决掉:

1.复制一份引用的AAR库文件到类库、项目的libs中,同时设置 flatDir.
(意味着有多少地方引用该类库,就要复制多少份AAR,都要设置 flatDir)
2.在所有引用该类库的 build.gradle 中 设置 flatDir 为以下代码即可.
repositories {
flatDir {
dirs 'libs', project(':类库名').file('libs')
}
}
(意味着有所有引用该类库的地方的 build.gradle,都要设置一遍 flatDir)

方式1:

    1. File -> New Module -> Import .JAR/.AAR
    2. import the .AAR.
    3. 在 build.gradle 添加 dependencies{compile project(':Name-Of-Your-Module-aar')}

优点√:

  1. 可以像引用自己类库一样.
  2. 可以供多个库、项目引用此AAR类库.

缺点×:

  1. 需要像自己类库一样去维护.

参考资料:

  1. How to manually include external aar package using new Gradle Android Build System - Stack Overflow

官网解释:当同一个JAR,AAR类库需要多个地方同时引用时的折衷配置方案.(其实就是方式1)

Handling transitive dependencies for local artifacts (jars and aar)

If you have a local jar or aar library that you want to use in more than one project, you cannot just reference it directly as a local dependency. This is because the android plugin will complain if it finds the same jar file twice when dexing the project and all its dependencies. (Note that right now you can't actually use a local aar file even if you only reference it once).

One way to fix this is to deploy the artifact in a repository. While it's possible, it might not be convenient due to the overhead of managing such a repository.

Another option is to create a new Gradle sub-project, and to make this project's published artifact be the jar or aar file that you want to reuse. Then you can simply have other Gradle sub-projects depend on this new sub-project.

In this new sub-project, simply create a build.gradle with the following:

configurations.create("default") artifacts.add("default", file('somelib.jar'))

posted @ 2016-10-18 17:41  Asion Tang  阅读(26203)  评论(0编辑  收藏  举报