在Android Studio中创建aar文件

时间:2022-10-20 18:29:46

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文件吗?

4 个解决方案

#1


160  

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 ')。库的构建声明。等级文件),它将输出一个。aar当它被建立。它将显示在模块目录中的build/output /aar/目录中。

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

您可以选择文件>新模块中的“Android Library”类型来创建一个新的Android Library。

#2


119  

Retrieve exported .aar file from local builds

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/output /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

Library project details

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

一个库项目有一个构建。包含应用插件的渐变文件:com.android.library。对于打包为.aar文件的这个库的引用,您必须定义一些属性,比如package和version。

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

构建示例。库级文件(此示例包含发布中的混淆):

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

In your app project you can drop this .aar file in the libs folder and update the build.gradle file to reference this library using the below example:

在应用程序项目中,可以将这个.aar文件放到libs文件夹中并更新构建。使用以下示例引用这个库的渐变文件:

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/refera-a-local -in-your-android-project中找到。

Sharing dependencies using maven

If you need to share these .aar files 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/howto - - -android-archive- - - - - - -图书馆- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

About the .aar file format

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

aar文件只是一个.zip文件,包含一个可选扩展名和特定内容。有关详细信息,请查看有关aar格式的链接。

#3


7  

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.

每次都需要更新版本,这是很烦人的。这也使得不用担心名称空间的问题变得更容易。

#4


0  

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

顺便说一句,aar没有传递依赖关系。您需要一个参数来打开它:传递依赖关系没有使用gradle解析aar库

#1


160  

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 ')。库的构建声明。等级文件),它将输出一个。aar当它被建立。它将显示在模块目录中的build/output /aar/目录中。

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

您可以选择文件>新模块中的“Android Library”类型来创建一个新的Android Library。

#2


119  

Retrieve exported .aar file from local builds

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/output /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

Library project details

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

一个库项目有一个构建。包含应用插件的渐变文件:com.android.library。对于打包为.aar文件的这个库的引用,您必须定义一些属性,比如package和version。

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

构建示例。库级文件(此示例包含发布中的混淆):

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

In your app project you can drop this .aar file in the libs folder and update the build.gradle file to reference this library using the below example:

在应用程序项目中,可以将这个.aar文件放到libs文件夹中并更新构建。使用以下示例引用这个库的渐变文件:

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/refera-a-local -in-your-android-project中找到。

Sharing dependencies using maven

If you need to share these .aar files 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/howto - - -android-archive- - - - - - -图书馆- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

About the .aar file format

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

aar文件只是一个.zip文件,包含一个可选扩展名和特定内容。有关详细信息,请查看有关aar格式的链接。

#3


7  

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.

每次都需要更新版本,这是很烦人的。这也使得不用担心名称空间的问题变得更容易。

#4


0  

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

顺便说一句,aar没有传递依赖关系。您需要一个参数来打开它:传递依赖关系没有使用gradle解析aar库