在 Android Studio 中创建 aar 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24309950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:01:04  来源:igfitidea点击:

Create aar file in Android Studio

androidandroid-studiojaraar

提问by AndroidEnthusiast

I'd like to create an aar file for my library in Android Studio, i would've gone with a jar option but my library has resources.

我想在 Android Studio 中为我的库创建一个 aar 文件,我会使用 jar 选项,但我的库有资源。

Any idea how to create an aar file from a library?

知道如何从库创建 aar 文件吗?

回答by Scott Barta

If your library is set up as an Android library (i.e. it uses the apply plugin: 'com.android.library'statement in its build.gradle file), it will output an .aar when it's built. It will show up in the build/outputs/aar/directory in your module's directory.

如果您的库设置为 Android 库(即它使用apply plugin: 'com.android.library'其 build.gradle 文件中的语句),它将在构建时输出一个 .aar。它将显示在模块目录的build/outputs/aar/目录中。

You can choose the "Android Library" type in File > New Module to create a new Android Library.

您可以在 File > New Module 中选择“Android Library”类型来创建一个新的 Android Library。

回答by hcpl

Retrieve exported .aar file from local builds

从本地构建中检索导出的 .aar 文件

If you have a module defined as an android library project you'll get .aar files for all build flavors (debug and release by default) in the build/outputs/aar/directory of that project.

如果您将模块定义为 android 库项目,您将在该build/outputs/aar/项目的目录中获得所有构建风格(默认为调试和发布)的 .aar 文件。

your-library-project
    |- build
        |- outputs
            |- aar
                |- appframework-debug.aar
                 - appframework-release.aar

If these files don't exist start a build with

如果这些文件不存在,则开始构建

gradlew assemble

for macOS users

对于 macOS 用户

./gradlew assemble

Library project details

图书馆项目详情

A library project has a build.gradlefile containing apply plugin: com.android.library. For reference of this library packaged as an .aarfile you'll have to define some properties like package and version.

库项目有一个build.gradle包含apply plugin: com.android.library. 为了参考打包为.aar文件的这个库,您必须定义一些属性,如包和版本。

Example build.gradlefile for library (this example includes obfuscation in release):

build.gradle库的示例文件(此示例包括发布中的混淆):

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 21
        versionCode 1
        versionName "0.1.0"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

Reference .aar file in your project

在您的项目中引用 .aar 文件

In your app project you can drop this .aarfile in the libsfolder and update the build.gradlefile to reference this library using the below example:

在您的应用项目中,您可以将此.aar文件放在libs文件夹中,并build.gradle使用以下示例更新文件以引用此库:

apply plugin: 'com.android.application'

repositories {
    mavenCentral()
    flatDir {
        dirs 'libs' //this way we can find the .aar file in libs folder
    }
}

android {
    compileSdkVersion 21
    buildToolsVersion "21.0.0"

    defaultConfig {

        minSdkVersion 14
        targetSdkVersion 20
        versionCode 4
        versionName "0.4.0"

        applicationId "yourdomain.yourpackage"
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
        }
    }
}

dependencies {
    compile 'be.hcpl.android.appframework:appframework:0.1.0@aar'
}

Alternative options for referencing local dependency files in gradle can be found at: http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project

可以在以下位置找到在 gradle 中引用本地依赖文件的替代选项:http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project

Sharing dependencies using maven

使用 maven 共享依赖项

If you need to share these .aarfiles within your organization check out maven. A nice write up on this topic can be found at: https://web.archive.org/web/20141002122437/http://blog.glassdiary.com/post/67134169807/how-to-share-android-archive-library-aar-across

如果您需要.aar在组织内共享这些文件,请查看 maven。可以在以下位置找到有关此主题的精彩文章:https: //web.archive.org/web/20141002122437/http: //blog.glassdiary.com/post/67134169807/how-to-share-android-archive-跨图书馆

About the .aar file format

关于 .aar 文件格式

An aar file is just a .zipwith an alternative extension and specific content. For details check this link about the aar format.

aar 文件只是.zip具有替代扩展名和特定内容的文件。有关详细信息,请查看有关 aar 格式的链接

回答by j2emanue

just like user hcpl said but if you want to not worry about the version of the library you can do this:

就像用户 hcpl 说的,但如果你不想担心库的版本,你可以这样做:

dependencies {
    compile(name:'mylibrary', ext:'aar')
}

as its kind of annoying to have to update the version everytime. Also it makes the not worrying about the name space easier this way.

因为每次都必须更新版本很烦人。此外,通过这种方式,可以更轻松地不必担心名称空间。

回答by Dhananjay M

After following the first and second steps mentioned in the hcpl's answer in the same thread, we added , '*.aar'], dir: 'libs'in the our-android-app-project-based-on-gradle/app/build.gradle file as shown below:

同一线程中按照hcpl 的回答中提到的第一步和第二步之后,我们在 our-android-app-project-based-on-gradle/app/ 中添加了, '*.aar'], dir: 'libs'build.gradle 文件如下图:

...

dependencies {
       implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')

...

Our gradle version is com.android.tools.build:gradle:3.2.1

我们的 gradle 版本是 com.android.tools.build:gradle:3.2.1

回答by Tarun A

To create AAR

创建 AAR

while creating follow below steps.

创建时请按照以下步骤操作。

File->New->New Module->Android Library and create.

File->New->New Module->Android Library and create.

To generate AAR

生成 AAR

Go to gradle at top right pane in android studio follow below steps.

按照以下步骤转到 android studio 右上角窗格中的 gradle。

Gradle->Drop down library name -> tasks-> build-> assemble or assemble release

Gradle->Drop down library name -> tasks-> build-> assemble or assemble release

AAR will be generated in build/outputs/aar/

AAR 将在build/outputs/aar/ 中生成

But if we want AAR to get generated in specific folder in project directory with name you want, modify your app level build.gradlelike below

但是,如果我们希望在项目目录中的特定文件夹中以您想要的名称生成 AAR,请修改您的应用程序级别,build.gradle如下所示

defaultConfig {
    minSdkVersion 26
    targetSdkVersion 28
    versionCode System.getenv("BUILD_NUMBER") as Integer ?: 1
    versionName "0.0.${versionCode}"


    libraryVariants.all { variant ->

        variant.outputs.all { output ->
            outputFileName = "/../../../../release/" + ("your_recommended_name.aar")
        }
    }
}

Now it will create folder with name "release" in project directory which will be having AAR.

现在它将在项目目录中创建名为“release”的文件夹,该文件夹将具有 AAR。

回答by cn123h

btw @aar doesn't have transitive dependency. you need a parameter to turn it on: Transitive dependencies not resolved for aar library using gradle

btw @aar 没有传递依赖。您需要一个参数来打开它: 使用 gradle 未解析 aar 库的传递依赖项