How to manually include external aar package using new Gradle Android Build System

后端 未结 23 1796
故里飘歌
故里飘歌 2020-11-22 03:58

I\'ve been experimenting with the new android build system and I\'ve run into a small issue. I\'ve compiled my own aar package of ActionBarSherlock which I\'ve called \'act

相关标签:
23条回答
  • 2020-11-22 04:11

    You can reference an aar file from a repository. A maven is an option, but there is a simpler solution: put the aar file in your libs directory and add a directory repository.

        repositories {
          mavenCentral()
          flatDir {
            dirs 'libs'
          }
        }
    

    Then reference the library in the dependency section:

      dependencies {
            implementation 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    }
    
    

    You can check out Min'an blog post for more info.

    0 讨论(0)
  • 2020-11-22 04:11

    You can add multiple aar dependencies with just few lines of code.

    Add local flatDir repository:

    repositories {
        flatDir {
            dirs 'libs'
        }
    } 
    

    Add every aar in libs directory to compile dependency configuration:

    fileTree(dir: 'libs', include: '**/*.aar')
            .each { File file ->
        dependencies.add("compile", [name: file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }, ext: 'aar'])
    }
    
    0 讨论(0)
  • 2020-11-22 04:11

    I found this workaround in the Android issue tracker: https://code.google.com/p/android/issues/detail?id=55863#c21

    The trick (not a fix) is to isolating your .aar files into a subproject and adding your libs as artifacts:

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

    More info: Handling-transitive-dependencies-for-local-artifacts-jars-and-aar

    0 讨论(0)
  • 2020-11-22 04:11

    If you use Gradle Kotlin DSL, you need to add a file in your module directory.

    For example: libs/someAndroidArchive.aar

    After just write this in your module build.gradle.kts in dependency block:

    implementation(files("libs/someAndroidArchive.aar"))
    
    0 讨论(0)
  • 2020-11-22 04:12

    There are 2 ways:

    The first way

    1. Open your Android Studio and navigate to the Create New Module window by File -> New -> New Module

    1. Select the Import .JAR/.AAR Package item and click the Next button

    2. Add a dependency in the build.gradle file that belongs to your app module.

        dependencies {
            ...
            implementation project(path: ':your aar lib name')
        }
    

    That's all.

    The second way

    1. Create a folder in libs directory, such as aars.

    2. Put your aar lib into the aars folder.

    3. Add the code snippet

    repositories {
        flatDir {
            dirs 'libs/aars'
        }
    }
    

    into your build.gradle file belongs to the app module.

    1. Add a dependency in the build.gradle file that belongs to your app module.
    dependencies {
        ...
        implementation (name:'your aar lib name', ext:'aar')
    }
    

    That's all.

    If you can read Chinese, you can check the blog 什么是AAR文件以及如何在Android开发中使用

    0 讨论(0)
  • 2020-11-22 04:13

    In my case just work when i add "project" to compile:

    repositories {
        mavenCentral()
        flatDir {
            dirs 'libs'
        }
    }
    
    
    dependencies {
       compile project('com.x.x:x:1.0.0')
    }
    
    0 讨论(0)
提交回复
热议问题