102

I have built my Android library package (AAR) and the result of build is created in the "..\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 generate the release built, that is, "app-release.aar". How can I do this?

Also, is it possible to generate the build with another custom name, for example, "myCustomAppName-release.aar" instead of "app-release.aar"?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Willy
  • 9,848
  • 22
  • 141
  • 284

10 Answers10

124

In Android Studio 1.2+, there is a context panel listing all available Gradle tasks.

I found this panel 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"

jiminikiz
  • 2,867
  • 1
  • 25
  • 28
  • 1
    This works for me, but I noticed that the build from the standard 'build' menu also started working once I had run the cmd line grade commands suggested by user1624552 in comments for another answer here. It would be useful if someone who has not run the command line grade can confirm this side gradle menu approach works also as I noticed the command line downloaded what looked like a new version of gradle in my case before it ran and I wonder if that also is a required step. – Mick Jul 09 '15 at 16:29
  • 1
    BTW, well done finding this!!! If anyone from Google is looking at this it would be nice to know if there are any plans to link/rationalise/explain the top Build menu, this 'new' Gradle side menu, and the Build Variants view etc? – Mick Jul 09 '15 at 16:32
  • Yeah, definitely. I too ran gradle from the commandline as the others answers had suggested until I found that menu. I am not sure that running it from the commandline is required first before using the side menu... What is interesting too is that whatever task you run from the side gradle menu becomes the default action "Run" action. So, in other words, the green arrow icon and the Run menu point to the last run gradle task. Useful :) – jiminikiz Jul 10 '15 at 19:49
  • just one question,when you export aar files in release mode,will it itncludes latest proguard properties with it ? – abh22ishek Nov 27 '15 at 10:30
  • i want to export an aar file ,with having a code obfuscation which is provided by proguard ? – abh22ishek Nov 27 '15 at 10:31
  • 1
    Yes, if you run the `assemble` task, it will apply whatever progaurd rules you have defined in your project. – jiminikiz Dec 01 '15 at 16:49
  • This menu was not available for me for my library. – Saravana Kumar K R May 30 '18 at 05:51
  • @K.R.SaravanaKumar are you sure that you have the proper `gradle` configuration file within your library? – jiminikiz Nov 26 '18 at 04:07
17

Create .aar

You can use the 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

The output is located at:

<project_path>/build/outputs/aar/<module_name>-<build_variant>.aar

Alternatively, you can use the Android Studio UI:

Menu ViewTool WindowsGradle <module_name>TasksBuild or othersassembleRelease

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yoAlex5
  • 29,217
  • 8
  • 193
  • 205
16

This issue 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.

Community
  • 1
  • 1
diidu
  • 2,823
  • 17
  • 32
13

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

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Veneet Reddy
  • 2,707
  • 1
  • 24
  • 40
13

With Android Studio 3.0, it is easier to generate an AAR file. From the Gradle option, check for the option as shown in the picture:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CanCoder
  • 1,073
  • 14
  • 20
12

From Android Studio v4.x:

  1. Select the Build Variant tab
  2. Choose release mode
  3. Then menu BuildMake 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 library release

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cong Dan Luong
  • 1,577
  • 17
  • 15
7
  1. Add the following script to your android{} tag in file 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 your command line.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
codezjx
  • 9,012
  • 5
  • 47
  • 57
  • Sorry, I am newbie in Android development. Is it strictly necessary to sign an android library package (aar) to get its release output? How can I generate my own key store? – Willy Dec 25 '14 at 10:52
  • In my library project, \build\outputs\aar is already generate: XX-debug.aar, XX-release.aar. And Why you nedd a .arr file? If you want to use a library, just add following script in you build.gradle: dependencies { compile project(':YourLibraryProject') } – codezjx Dec 25 '14 at 11:07
  • 1
    I want to use this library within my android apps in future. This is a library and I want to obtain its release output but I do not understand why but I am obtaining only XX-debug.aar and not XX-release.aar in my library's "\app\build\outputs\aar" folder. – Willy Dec 25 '14 at 11:11
  • Sorry to tell you: Gradle build does not support local aar dependencies, you can visit: https://code.google.com/p/android/issues/detail?id=55863 – codezjx Dec 25 '14 at 11:27
  • 5
    From a terminal windows, once in project folder, I have executed the following commands: "./gradlew clean" and "./gradlew aR" and now "XX-release.aar" is generated into "..\app\build\outputs\aar" folder. How can I do the same from Android Studio? that is, to tell Android Studio to assemble the Release as well. – Willy Dec 25 '14 at 11:34
  • Also how can I force a custom name instead of the preceding "app" in the "app-debug.aar" and "app-release.aar" files? I would like to be able to change "app" prefix by a desired one, that is fore example, "myCustomName-debug.aar" and "myCustomName-release.aar" – Willy Dec 25 '14 at 11:45
  • Bill, this is not totaly true that Gradle does not support local aar dependencies, please, read at the end of your posted URL. There is a workaround to do it. Also described here: http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project – Willy Dec 25 '14 at 12:12
  • To set custom name for aar output file I have finally done this (instead of using applicationVariants use libraryVariants instead): http://stackoverflow.com/questions/27114073/could-not-find-property-outputfile-on-com-android-build-gradle-internal-api-ap – Willy Dec 25 '14 at 13:04
  • Did you solve on generating the release.aar file? I have the same problem and cannot solve it... – Massimo Feb 20 '15 at 10:28
  • 1
    Normally all you need to do is build the project in android studio but I have this same issue in a couple of apps. So doing it in the terminal is the only solution. – CodyMace May 13 '15 at 19:32
7

Part 1: How can we create a release-only *release.aar?

Run in Android Studio terminal (or any terminal in your project folder):

./gradlew assembleRelease

You don’t need signing the configuration for such task.

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

Add to your module gradle.build:

android{
    project.archivesBaseName = "AnotherLibraryName"
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lukassos
  • 369
  • 5
  • 12
3

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NickF
  • 5,637
  • 12
  • 44
  • 75
0

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joe
  • 2,252
  • 1
  • 22
  • 32