Failed to resolve local AAR built from local source module - android

I have a library module that I want to include as an AAR dependency into a sample app:
:my-library
:sample-app
So in sample/build.gradle, I do the following:
repositories {
flatDir {
dirs "../my-library/build/outputs/aar"
}
}
// I have different flavors that specify whether to use the source or binary (aar) dependency
flavorDimensions "SOURCE_OR_BINARY"
productFlavors {
source { }
binary { }
}
dependencies {
sourceImplementation project(':my-library')
binaryImplementation(name: 'my-library-release', ext: 'aar') // <-- this line fails with error
}
tasks.whenTaskAdded { task ->
def taskName = task.name.toLowerCase()
if (taskName.toLowerCase().contains("binary")) {
// Prepare libs as binaries
task.dependsOn ('my-library:assembleRelease')
}
}
This works fine with ./gradlew on the command line, but Android Studio reports a Failed to resolve: :my-library-release: during gradle sync. If I do a ./gradlew assemble on the command line, then sync Android Studio, the the AS Gradle sync succeeds.
The issue has to do with the timing of binaryImplementation(name: 'my-library-release', ext: 'aar'). When Gradle Sync is executed, the aar does not exist yet because it has yet to be built.
Is there a better way to do this that will avoid the Failed to resolve Android Studio Gradle sync error?

You need to add this to your app main build.gradle.
repositories {
/...
/...
flatDir {
dirs 'libs'
}
}
Lets say if you .aar file in the lib folder,then you could do something like this.
implementation files('libs/assembleRelease.aar')

You can try import with this way,
File -> New Module -> Import .Jar/.AAR package

I suggest that you use a local maven repository rather that flatDir. Dependencies which come from FileCollection and/or flatDir are not as full-featured as those coming from a "real" repository (eg maven/ivy)
Eg:
repositories {
maven {
url file("${rootProject.projectDir}/mavenRepo")
}
}
dependencies {
binaryImplementation "my-group:my-artifact:1.0#aar"
...
}
You'd then store the artifact using the maven repository directory layout. Eg:
rootProject/mavenRepo/my-group/my-artifact/1.0/my-artifact-1.0.aar

The answer can be found here - expose a configuration with that AAR, and consume that configuration downstream
https://docs.gradle.org/current/userguide/cross_project_publications.html

Related

Gradle cannot resolve local aar dependency

I wanted to integrate my own library to an Android application.
Before I push it on remote maven repo I want to check the integration with an application locally. So I've pushed it to my mavenLocal().
Maven was installed via brew install maven
And there is my problem - Gradle cannot resolve dependency to my library.
Error I get is just ERROR: Failed to resolve: com.op.rlgen:0.0.1
In library I use maven-publish plugin:
apply plugin: 'maven-publish'
publishing {
publications {
maven(MavenPublication) {
groupId 'com.op'
artifactId 'rlgen'
version '0.0.1'
artifact("$buildDir/outputs/aar/rlgen-release.aar")
pom.withXml {
// generating pom logic here
}
}
}
// For `publish` task use mavenLocal
repositories {
mavenLocal()
}
}
to publish a library on my maven I use:
./gradlew :rlgen:publishToMavenLocal
In following path i have 2 files, one with aar extension, the second one with pom:
~/.m2/repository/com/op/rfgen
As I thought the problem is caused because of quite complicated scripts in my main app, I created a sandbox, clear Android Application.
In sandbox's gradle I have:
// project level
allprojects {
repositories {
mavenLocal()
google()
jcenter()
// also tried with
maven {
url "~/.m2/repository"
}
}
}
// app level
dependencies {
compile('com.op.rfgen:0.0.1#aar) {
transitive = true
}
}
I check if mavenLocal points to proper folder and it seems ok:
// in gradle
repositories {
println(mavenLocal().url) // file:/Users/op_user/.m2/repository/
}
And with this configuration I cannot add dependency to that aar because of:
ERROR: Failed to resolve: com.op.rlgen:0.0.1:
Affected Modules: app
Thanks for your help!
The problem was that line:
compile('com.op.rfgen:0.0.1#aar). Should be... compile('com.op:rfgen:0.0.1#aar)... :D

How can I lazily depend on an AAR that created by a task? [duplicate]

I have a library module that I want to include as an AAR dependency into a sample app:
:my-library
:sample-app
So in sample/build.gradle, I do the following:
repositories {
flatDir {
dirs "../my-library/build/outputs/aar"
}
}
// I have different flavors that specify whether to use the source or binary (aar) dependency
flavorDimensions "SOURCE_OR_BINARY"
productFlavors {
source { }
binary { }
}
dependencies {
sourceImplementation project(':my-library')
binaryImplementation(name: 'my-library-release', ext: 'aar') // <-- this line fails with error
}
tasks.whenTaskAdded { task ->
def taskName = task.name.toLowerCase()
if (taskName.toLowerCase().contains("binary")) {
// Prepare libs as binaries
task.dependsOn ('my-library:assembleRelease')
}
}
This works fine with ./gradlew on the command line, but Android Studio reports a Failed to resolve: :my-library-release: during gradle sync. If I do a ./gradlew assemble on the command line, then sync Android Studio, the the AS Gradle sync succeeds.
The issue has to do with the timing of binaryImplementation(name: 'my-library-release', ext: 'aar'). When Gradle Sync is executed, the aar does not exist yet because it has yet to be built.
Is there a better way to do this that will avoid the Failed to resolve Android Studio Gradle sync error?
You need to add this to your app main build.gradle.
repositories {
/...
/...
flatDir {
dirs 'libs'
}
}
Lets say if you .aar file in the lib folder,then you could do something like this.
implementation files('libs/assembleRelease.aar')
You can try import with this way,
File -> New Module -> Import .Jar/.AAR package
I suggest that you use a local maven repository rather that flatDir. Dependencies which come from FileCollection and/or flatDir are not as full-featured as those coming from a "real" repository (eg maven/ivy)
Eg:
repositories {
maven {
url file("${rootProject.projectDir}/mavenRepo")
}
}
dependencies {
binaryImplementation "my-group:my-artifact:1.0#aar"
...
}
You'd then store the artifact using the maven repository directory layout. Eg:
rootProject/mavenRepo/my-group/my-artifact/1.0/my-artifact-1.0.aar
The answer can be found here - expose a configuration with that AAR, and consume that configuration downstream
https://docs.gradle.org/current/userguide/cross_project_publications.html

How to include 3rd party aar into Android React Native module?

I am building an android React Native module and my code imports classes from a 3rd party SDK which is provided as an aar file. How should I bundle this file into my module? I tried adding
allprojects {
repositories {
flatDir {
dirs: 'libs'
}
}
}
and
dependencies {
... other deps ...
compile (name:'my-external-lib', ext:'aar')
}
to the build.gradle file and putting this my-external-lib.aar file into libs/ folder, but still getting an error when building MyApp react-native application with react-native-my-module included:
* What went wrong:
A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration
':app:_developmentDebugApkCopy'.
> Could not find :my-external-lib:.
Required by:
MyApp:app:unspecified > MyApp:react-native-my-module:unspecified
Any advice?
Looks like I managed to solve this question by myself.
It seems that you need to define repositories in the main project to allow sub-projects locate their dependencies.
Putting this to the main project build.gradle file:
repositories {
flatDir {
dirs: 'libs'
}
}
did the trick.
I believe though, that this is not the correct way to include sub-projects with dependencies, because you're obviously can't know how to amend your build.gradle file if this library is a 3rd party lib from npm.
So, can someone explain why this worked and how should it be done in the right way?
In your module's android/build.gradle file, you'll want to add the .aar as an artifact.
configurations.maybeCreate("default")
artifacts.add("default", file("NAME_OF_YOUR_FILE.aar"))
What ever you have written is absolutely correct but you just written in wrong place
you have written the code in the build.gradle(Project:project_name)
you just have to write the code in build.gradle(Module:app) file and the .arr file has to be paste in the projects lib folder
i.e.
repositories {
  flatDir {
   dirs 'libs'
  }
}
dependencies {
   ... other deps ...
   compile (name:'my-external-lib', ext:'aar')
}
In your settings.gradle you should include this lib e.g:
include ':app', ':my-external-lib'
And you can compile it as a normal project e.g: compile project(':my-external-lib')
Also your my-external-lib build.gradle should be like :
configurations.maybeCreate("default")
artifacts.add("default", file('my-external-lib.aar'))
I successfully included a 3rd party .aar file into android react native module.
Please look at my answer to this question: How to include AAR in React Native Android build?
after trying so Long I finally got it
there how you should do it :
Add
repositories {
  flatDir {
   dirs 'libs'
  }
}
into project:grindle (not module.grindle)
and
Add dependies into app:grindle (not module.grindle)
I you are trying to use third party software in react-native module than
Add aar file in reactnativeModule/android/libs
Add
allprojects {
repositories {
flatDir {
dirs: 'libs'
}
}
}
in build.gradle of reactnativeModule/android/build.grindle
add dependencies just in
dependencies { implementation "com.facebook.react:react-native:${safeExtGet('reactnativeVersion', '+')}" of reactnativeModule/android/build.grindle

Generate Protobuf java files with Square's Wire

I'm trying to generate .java files from the .proto files I have stored under my SRC folder in Android studio. I put the below code in my gradle file by it doesn't seem to work
apply plugin: 'com.squareup.wire'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.squareup.wire:wire-maven-plugin:2.1.1'
}
}
There is a gradle plugin for wire here: https://github.com/square/wire-gradle-plugin. However, it seems like it's not quite ready for primetime yet. I had some trouble getting it working.
But, here's a way to do it that automates generation of java code from the *.proto files using the wire compiler directly and a simple gradle task. I've provided a snippet below with the modifications to your build.gradle. Change the protoPath and wireGeneratedPath based on your source layout.
def protoPath = 'src/proto'
def wireGeneratedPath = 'build/generated/source/wire'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.squareup.wire:wire-compiler:2.2.0'
}
}
android {
sourceSets {
main {
java {
include wireGeneratedPath
}
}
}
}
dependencies {
compile 'com.squareup.wire:wire-runtime:2.2.0'
// Leave this out if you're not doing integration testing...
androidTestCompile 'com.squareup.wire:wire-runtime:2.2.0'
}
// This handles the protocol buffer generation with wire
task generateWireClasses {
description = 'Generate Java classes from protocol buffer (.proto) schema files for use with squareup\'s wire library'
delete(wireGeneratedPath)
fileTree(dir: protoPath, include: '**/*.proto').each { File file ->
doLast {
javaexec {
main = 'com.squareup.wire.WireCompiler'
classpath = buildscript.configurations.classpath
args = ["--proto_path=${protoPath}", "--java_out=${wireGeneratedPath}", "${file}"]
}
}
}
}
preBuild.dependsOn generateWireClasses
So instead of using a gradle plugin I just ended up using the square wire compiler jar. Here are the steps.
Download compiler-jar-with-dependencies from http://search.maven.org/#artifactdetails%7Ccom.squareup.wire%7Cwire-compiler%7C2.1.1%7Cjar
Put jar file into root directory of android app
Go to the directory and paste this command
java -jar wire-compiler-2.1.1-jar-with-dependencies.jar --proto_path=directory-of-protofile --java_out=app/src/main/java/ name-of-file.proto
Should work. Make sure to replace the directory-of-protofile and name-of-file with whatever you have.

Adding local .aar files to Gradle build using "flatDirs" is not working

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')

Categories

Resources