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'))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM