如何在 Android Studio 中创建一个发行版 Android 库包(aar)(而不是调试)

我已经构建了我的 android 库包(aar) ,并且在 "..\app\build\outputs\aar" folder. The file within this folder is called "app-debug.aar" so I guess it has been built in debug mode so I would like to know how to genereate the release built, that is, "app-release.aar". How can I do this? Also, is it possible to genereate the build with another custom name, for example, "myCustomAppName-release.aar" instead of "app-release.aar".

81350 次浏览

1.add following script to your android{} tag in build.gradle to generate a release build:

signingConfigs {
testConfig{
storeFile file("X:/XXXX/yourkeystore")
storePassword "yourKeyPassword"
keyAlias "yourAlias"
keyPassword "yourAliasPassword"
}
}


buildTypes{
release {
signingConfig  signingConfigs.testConfig
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

2.run command "gradle clean build" in you command line.

Set up your project as an Android library
In build.gradle file: plugin: 'com.android.library'
You can see the output in: build/outputs/aar/[your module].
Also see this link

In Android Studio 1.2+, there is a complete gradle menu that lists all of the available gradle tasks.

I found this menu on the right side of the IDE with default options enabled.

The gradle menu is on the right side of the IDE by default...

Right click the task you want and click "Run".

Right click the task you want a click "RUN"

This issue of can already be handled with the answers like execute

./gradlew assembleRelease

or choose assembleRelease from Android Studio's Gradle menu. However, for completeness, with Android Studio 1.5.1 (and probably also older versions) building release version of a .aar can be accomplished by selecting Build->Build APK in Android Studio. It seems to execute assembleRelease. If your project only contains the library project, it does not ask for any signing information.

I faced this issue in AS 2.3.3 and solved it by changing the build variant of the module to release and building it again:

enter image description here

Yet another 2 part question where nobody answers the second part...

For the future generations, here are simple answers:

Part 1 How to create release only *release.aar ?

Run in android studio terminal (or any terminal in your project folder).:

./gradlew assembleRelease

You don`t need signing config for such task.

Part 2 How to rename the output library to AnotherLibraryName-release.aar ?

Add to your module gradle.build :

android{
project.archivesBaseName = "AnotherLibraryName"
}

With Android Studio 3.0 is easier to generate aar file. From the Gradle option, chek for the option as shown in the picture enter image description here

for me, simply fixing a layout bug in the library that did not produce compile error (missing androidx plugin) fixed it and <lib>-release.aar appeared alongside the <lib>-debug.aar in build/outputs/aar/ folder.

Create .aar

You can use command line

./gradlew <moduleName>:assemble


./gradlew <moduleName>:assemble<build_variant>
//for example
./gradlew <moduleName>:assembleRelease


//or
./gradlew <moduleName>:bundle<build_variant>Aar
//for example
./gradlew <moduleName>:bundleReleaseAar
//output is located
<project_path>/build/outputs/aar/<module_name>-<build_variant>.aar

Alternatively you can use AndroidStudio UI

View -> Tool Windows -> Gradle
<module_name> -> Tasks -> build or others -> assembleRelease

From Android Studio v4.x:

  1. Select Build Variant tab
  2. Choose release mode
  3. Then Menu > Build > Make module...
  4. Click build from the top menu > Rebuild project

Your .aar file will be found in the project file hierarchy on the left, under MyLibrary/Build/outputs (you may need to first, change the view from Android view to Project view, to see these files - using the dropdown at the top left corner)

Build AAR lib release