I have an android library distributed in aar format. It weight 300kb.
I want to create another library and also in aar format, where first library in dependencies.
But result library weight is 30kb, so obviously it does not not include first library.
I tried to add first library using flatDir:
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name:'mylib-1.0', ext:'aar')
}
And publish to mavenLocal using maven-publish:
repositories {
mavenLocal()
}
dependencies {
compile 'com.mylib:mylib:1.0.0#aar'
}
but still it does not include to result aar.
How to include my first aar to result aar?
The best way to achieve it is to distribute the aar with a maven repo.
The aar file doesn't contain the transitive dependencies and doesn't have a pom file which describes the dependencies used by the library.
It means that, if you are importing a aar file using a flatDir repo you have to specify the dependencies also in your project.
Using a maven repository you will not have the same issue.
In this case, gradle downloads the dependencies using the pom file which will contains the dependencies list.
I am trying to use some of the awesome android arsenal libraries. For example: https://android-arsenal.com/details/1/703. If you go to main page and the package page it gives instructions on what to add to the build.gradle file. I recently updated Android Studio and the build.grade files are now in the same "Gradle Scripts" Tab. I want to know where to put the lines:
// stock actionBar
compile 'com.balysv.materialmenu:material-menu:1.x.x'
// Toolbar and ActionBarCompat-v21 (includes support-v7:21.0.x)
compile 'com.balysv.materialmenu:material-menu-toolbar:1.x.x'
// actionBarCompat-v20 (up to support-v7:20.0.0 - does not support Toolbar)
compile 'com.balysv.materialmenu:material-menu-abc:1.x.x'
// actionBarSherlock
compile 'com.balysv.materialmenu:material-menu-abs:1.x.x'
and/or
repositories {
maven {
url "https://jitpack.io"
}
}
and
dependencies {
compile 'com.github.balysv:material-menu:v1.5.1'
}
I am confused because of the two build.gradle files listed here:
The Project:Socio file looks like this:
The Module:app file looks like this:
You should put all dependencies in the module/build.gradle file.
For example
compile 'com.balysv.materialmenu:material-menu:1.x.x'
compile 'com.balysv.materialmenu:material-menu-toolbar:1.x.x'
compile 'com.github.balysv:material-menu:v1.5.1'
About the repositories you can put it in the top level o in module gradle file. It depends if any project uses the same repos.
For example: root/build.gradle
allprojects{
repositories {
jcenter()
maven {
url "https://jitpack.io"
}
}
}
or module/build.gradle
repositories {
jcenter()
maven {
url "https://jitpack.io"
}
}
Put them in the Module:app gradle file.
The other gradle file is for project-wide build configuration. From the documentation:
Android Studio projects contain a top-level project Gradle build file that allows you to add the configuration options common to all application modules in the project. Each application module also has its own build.gradle file for build settings specific to that module.
My project contains one main module ('app') and one library module ('mylibrary'). In my library I have a libs folder, which contains an aar file to a library that I want to use in my 'library' module.
I'm referencing my aar library using the following gradle script:
repositories{
flatDir{
dirs 'libs'
}
}
dependencies{
compile(name:'nameOfAar', ext:'aar')
}
This doesn't help it's trying to look in the mavenCentral for the aar library, but doesn't want to look in the flat directory that I've specified. Does anybody got any clue, how can I reference an aar library from an library module?
I had the same problem but unfortunately the only solution that I found is to add '../<library_dir>/libs' to build.gradle of the app
repositories {
flatDir {
dirs 'libs', '../<library_dir>/libs'
}
}
How to add AAR library in Android Studio 1.0.2? I search about this and I found a posts, where saying that I need to put library file into libs folder. But it doesn't work. This library is for internal use and never published in open access. File build.gradle contains this line:
compile fileTree(dir: 'libs', include: ['*.aar', '*.jar'])
But Studio can't find resources from library. I've try to add it manually:
compile(name:'somelib-1.32', ext:'aar')
And when I get this error:
"Error:Failed to find: :somelib-1.32:"
How to fix it?
The build System didn't manage very well the -, as for res files.
Replace it by _ or CamelCase, so somelib-1.32.aar become somelib_1.32.aar
EDIT : Also don't forget to add flatDirs[...] to your project build.gradle
allprojects {
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
}
I'm aware of this question: Adding local .aar files to my gradle build but the solution does not work for me.
I tried adding this statement to the top level of my build.gradle file:
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
I've also put the slidingmenu.aar file into /libs and referenced it in the dependencies section: compile 'com.slidingmenu.lib:slidingmenu:1.0.0#aar' but it did not work at all.
I tried compile files('libs/slidingmenu.aar') as well but with no luck.
What am I missing? Any ideas?
P.S. Android Studio 0.8.2
Building upon Josiah's answer, here's how I got it to work.
Following his instructions (under edit) (File -> New-> New Module -> Import .JAR/.AAR) and import your .AAR.
Then in your project build.gradle (not the top level one, the one under 'app') add the following (in the dependencies section):
dependencies {
compile project(':Name-Of-Your-Project')
}
Note Name-Of-Your-Project should match the name of the folder that was added after you imported the AAR file (at the same level as app/.idea under the top most level folder). Or to put it another way...
MyApplication
.idea
app
build.gradle (here's where to add compile project(':ProjectName') to dependency section)
ProjectName (added automatically after importing, matching the name of your aar file)
build
gradle
etc
This worked for me running Android Studio 0.8.0. Don't forget to synchronize gradle (using toolbar button or in File->Synchronize) after you do this.
(Thanks to Josiah for getting me going in the right direction)
(Note: prior to this I tried adding it to the libs folder, trying to manipulate the top level build.gradle and the app level build.gradle, but none of that worked for my aars files--jar's will work fine, but not the aar files)
Update : As #amram99 mentioned, the issue has been fixed as of the release of Android Studio v1.3.
Tested and verified with below specifications
Android Studio v1.3
gradle plugin v1.2.3
Gradle v2.4
What works now
Now you can import a local aar file via the File>New>New
Module>Import .JAR/.AAR Package option in Android Studio v1.3
However the below answer holds true and effective irrespective of the Android Studio changes as this is based of gradle scripting.
Old Answer :
In a recent update the people at android broke the inclusion of local aar files via the Android Studio's add new module menu option.
Check the Issue listed here.
Irrespective of anything that goes in and out of IDE's feature list , the below method works when it comes to working with local aar files.(Tested it today):
Put the .aar file in the libs directory (create it if needed), then, add the following code:
In the module build.gradle:
dependencies {
compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
}
In the project build.gradle:
repositories{
flatDir{
dirs 'libs'
}
}
Edit:
The correct way (currently) to use a local AAR file as a build dependency is to use the module import wizard (File | New Module | Import .JAR or .AAR package) which will automatically add the .aar as a library module in your project.
Old Answer
Try this:
allprojects {
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
}
...
compile(name:'slidingmenu', ext:'aar')
I got this working on Android Studio 2.1. I have a module called "Native_Ads" which is shared across multiple projects.
First, I created a directory in my Native_ads module with the name 'aars' and then put the aar file in there.
Directory structure:
libs/
aars/ <-- newly created
src/
build.gradle
etc
Then, the other changes:
Top level Gradle file:
allprojects {
repositories {
jcenter()
// For module with aar file in it
flatDir {
dirs project(':Native_Ads').file('aars')
}
}
}
App module's build.gradle file:
- no changes
Settings.gradle file (to include the module):
include ':app'
include 'Native_Ads'
project(':Native_Ads').projectDir = new File(rootProject.projectDir, '../path/to/Native_Ads')
Gradle file for the Native_Ads module:
repositories {
jcenter()
flatDir {
dirs 'aars'
}
}
dependencies {
compile(name:'aar_file_name_without_aar_extension', ext:'aar')
}
That's it. Clean and build.
This solution is working with Android Studio 4.0.1.
Apart from creating a new module as suggested in above solution, you can try this solution.
If you have multiple modules in your application and want to add aar to just one of the module then this solution come handy.
In your root project build.gradle
add
repositories {
flatDir {
dirs 'libs'
}}
Then in the module where you want to add the .aar file locally. simply add below lines of code.
dependencies {
api fileTree(include: ['*.aar'], dir: 'libs')
implementation files('libs/<yourAarName>.aar')
}
Happy Coding :)
The easiest way now is to add it as a module
This will create a new module containing the aar file, so you just need to include that module as a dependency afterwards
This is my structure, and how I solve this:
MyProject/app/libs/mylib-1.0.0.aar
MyProject/app/myModulesFolder/myLibXYZ
On build.gradle
from Project/app/myModulesFolder/myLibXYZ
I have put this:
repositories {
flatDir {
dirs 'libs', '../../libs'
}
}
compile (name: 'mylib-1.0.0', ext: 'aar')
Done and working fine, my submodule XYZ depends on somelibrary from main module.
You can do it this way. It needs to go in the maven format:
repositories {
maven { url uri('folderName')}
}
And then your AAR needs to go in a folder structure for a group id "com.example":
folderName/
com/
example/
verion/
myaar-version.aar
Then reference as a dependency:
compile 'com.example:myaar:version#aar'
Where version is the version of your aar file (ie, 3.0, etc)
In my case, I just put the AAR file in libs, and add
dependencies {
...
api fileTree(dir: 'libs', include: ['*.aar'])
...
}
in build.gradle and it works. I think it is similar with default generated dependency:
implementation fileTree(dir: 'libs', include: ['*.jar'])
In my case the none of the answers above worked! since I had different productFlavors just adding
repositories {
flatDir {
dirs 'libs'
}
}
did not work! I ended up with specifying exact location of libs directory:
repositories{
flatDir{
dirs 'src/main/libs'
}
}
Guess one should introduce flatDirs like this when there's different productFlavors in build.gradle
For anyone who has this problem as of Android Studio 1.4, I got it to work by creating a module within the project that contains 2 things.
build.gradle with the following contents:
configurations.create("default")
artifacts.add("default", file('facebook-android-sdk-4.7.0.aar'))
the aar file (in this example 'facebook-android-sdk-4.7.0.aar')
Then include the new library as a module dependency. Now you can use a built aar without including the sources within the project.
Credit to Facebook for this hack. I found the solution while integrating the Android SDK into a project.
If you already use Kotlin Gradle DSL, the alternative to using it this way:
Here's my project structure
|-root
|----- app
|--------- libs // I choose to store the aar here
|-------------- my-libs-01.aar
|-------------- my-libs-02.jar
|--------- build.gradle.kts // app module gradle
|----- common-libs // another aar folder/directory
|----------------- common-libs-01.aar
|----------------- common-libs-02.jar
|----- build.gradle.kts // root gradle
My app/build.gradle.kts
Using simple approach with fileTree
// android related config above omitted...
dependencies {
// you can do this to include everything in the both directory
// Inside ./root/common-libs & ./root/app/libs
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar", "*.aar"))))
implementation(fileTree(mapOf("dir" to "../common-libs", "include" to listOf("*.jar", "*.aar"))))
}
Using same approach like fetching from local / remote maven repository with flatDirs
// android related config above omitted...
repositories {
flatDir {
dirs = mutableSetOf(File("libs"), File("../common-libs")
}
}
dependencies {
implementation(group = "", name = "my-libs-01", ext = "aar")
implementation(group = "", name = "my-libs-02", ext = "jar")
implementation(group = "", name = "common-libs-01", ext = "aar")
implementation(group = "", name = "common-libs-02", ext = "jar")
}
The group was needed, due to its mandatory (not optional/has default value) in kotlin implementation, see below:
// Filename: ReleaseImplementationConfigurationAccessors.kt
package org.gradle.kotlin.dsl
fun DependencyHandler.`releaseImplementation`(
group: String,
name: String,
version: String? = null,
configuration: String? = null,
classifier: String? = null,
ext: String? = null,
dependencyConfiguration: Action<ExternalModuleDependency>? = null
)
Disclaimer:
The difference using no.1 & flatDirs no.2 approach, I still don't know much, you might want to edit/comment to this answer.
References:
https://stackoverflow.com/a/56828958/3763032
https://github.com/gradle/gradle/issues/9272
This line includes all aar and jar files from libs folder:
implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs/')
Add below in app gradle file
implementation project(path: ':project name')