I'm writing a build system for a framework which can be extended via plugins by users (developers). My problem is that I can't include all aar files with a mask. For example I can include all jar files like this:
compile fileTree(dir: 'libs', include: '*.jar')
But if I include all aar files like this:
compile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
I get an error:
Only Jar-type local dependencies are supported. Cannot handle: /path/to/file.aar
However it is possible to include each file separately like this:
compile (name:'myfile', ext:'aar')
As a workaround, during the build, I will need to list the files in 'libs' directory, insert all the libraries manually into my build.gradle and keep it in sync (or rewrite each time). it's a hackish way of doing it and thus I wonder if there is a better way of doing it. Maybe there is a gradle script which can do it automatically or some gradle beta which supports this:
compile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
First add flatDir repository:
repositories {
flatDir {
dirs 'libs'
}
}
Then after dependencies section add:
fileTree(dir: 'libs', include: '**/*.aar')
.each { File file ->
dependencies.add("compile", [
name: file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name },
ext: 'aar'
])
}
You can put your aar files in a folder.
You have to define this folder inside the repositories block. Of course you can use the libs folder
Something like:
repositories {
mavenCentral()
flatDir {
dirs 'myAarFolder'
}
}
Then you can use in your dependencies something like:
dependencies {
compile(name:'nameOfYourAARFile', ext:'aar')
}
As I know there is no way to incluse all aars with a wildcard as *.
I know this answer is late to the game, but for others coming here, I have managed it without using the repository.flatDir.
Just add the fileTree() AFTER the dependencies section
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
fileTree(dir: 'libs', include: '*.aar')
.each { File file ->
dependencies.add("compile", files( 'libs/'+file.name ))
}
Related
I try to add a local .aar file to the project as it is described here, because I don't want to implement this routine each time when I change my library code.
So in my project build.gradle file I added:
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
Both my library and my app have two flavours - alphaTest and myRelease. I want each library to be added strictly to the corresponding flavour of the app.
And in build.gradle of my app I have the following dependencies block:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar', '**/*.aar'])
compile 'com.android.support:appcompat-v7:23.0.0'
alphaTestCompile files ('libs/myLib-alphaTest-release.aar')
myReleaseCompile files('libs/myLib-myRelease-release.aar')
}
Unfortunately as a result I always receive this kind of error:
Error:Project myApp: Only Jar-type local dependencies are supported. Cannot handle: C:\Users\...\myApp\libs\myLib-myRelease-release.aar
I tryed to use this format
alphaTestCompile ('my.package:libs/myLib-alphaTest-release:0.1#aar')
and also this
alphaTestCompile (name:'myLib-alphaTest-release', ext:'aar')
but in this case the gradle cannot event resolve the files.
How can I make .aar files also be compiled?
Below is what I config for using aar file:
//dependencies
compile fileTree(include: ['*.jar'], dir: 'libs') // dont change anything here
compile(name: 'yourLib', ext: 'aar') // yourLib.aar is put in libs folder
repositories {
flatDir {
dirs 'libs'
}
}
Say I have a library module which contains some 3rd party libraries like OkHttp. When I include this library in my application I was unable to use these 3rd party libraries. I read the following articles Article 1,
Article 2
and tried
1. compile project(':library-name')
{after importing the .aar file(of library) as a module into myproject}
2. I included the .aar file in libs folder and added following dependencies
build.gradle(project level)
allprojects {
repositories {
flatDir {
dirs 'libs'
}
}
}
build.gradle(application level)
compile fileTree(dir: 'libs', include: ['*.jar'])
compile ('com.myapp.package:library-name:1.0.0#aar'){
transitive=true
}
3. similar to 2nd but in
build.gradle(application level)
compile fileTree(dir: 'libs', include: ['*.jar'])
compile (name:'library-name', ext:'aar'){
transitive=true
}
But, still I was unable to use the transitive libraries which are present in my library. I am getting following exception.
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/squareup/okhttp/MediaType;
Can someone help
EDIT:
The following is build.gradle of my library
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'org.apache.httpcomponents:httpcore:4.4.4'
compile 'org.apache.httpcomponents:httpclient:4.5.1'
compile 'com.squareup.okhttp:okhttp:2.6.0'
compile 'petrov.kristiyan.colorpicker:colorpicker-library:1.0.3'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'org.hamcrest:hamcrest-library:1.1'
compile files('notificationlog-0.1.0.jar')
I was able to use the notificationlog in my application which was a dependency in my library, but I was unable to use okhttp and colorpicker
The aar file doesn't contain the nested (or 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.
It doesn't make sense:
compile (name:'library-name', ext:'aar'){
transitive=true
}
because the aar is without a pom file which describes the dependencies, then gradle is unable to add any dependencies because it can't know them.
I have added the .aar files to libs folder in the sub-project.
And have given the repositories as:
repositories {
mavenCentral()
mavenLocal()
flatDir {
dirs 'libs'
}
in the build.gradle of this sub-project.
Do I need to add dependencies in Main project's build.gradle also?
If yes, how should that be done?
In the repositories of your module: app
repositories {
flatDir {
dirs 'libs'
}
}
Add the following in the dependencies of your module: app
In the case that your have both a JAR and an AAR file, do the following.
dependencies
{
implementation (name: '***library name***', ext: 'aar')
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'], )
}
You need to set the dependencies too:
compile fileTree(dir: 'libs', include: ['*.jar','*.aar'])
This can be done in the subprojects build.gradle.
Edit: You might need to specify the file itself:
compile(name:'name-of-file',ext:'aar')
classpath 'com.android.tools.build:gradle:1.3.0'
File -> New Module -> Import JAR/AAR Package
"File name" select .aar fileļ¼
example:
d:/myproject/mylib/build/outputs/aar/mylib-release.aar
"Subproject name",example:mylib-release
/app/build.gradle
dependencies{
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':mylib-release')
}
For filename.aar
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name:'filename', ext:'aar')
}
You are adding a aar file in libs folder.
The aar file doesn't contain the dependencies, then you have to add these dependencies also in the main project.
In your module1/build.gradle you should have something like:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile('com.android.support:appcompat-v7:22.2.1') //for example
//..
}
In your mainModule/build.gradle you have to add all the dependencies used by your module1.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile(name:'fileName',ext:'aar')
compile('com.android.support:appcompat-v7:22.2.1') //for example
//...
}
Gradle build error - more than one library with package name android.support.v7.appcompat. This project has Chrome Cast in it, so is using android-support-v7-appcompat and CastCompanionLibary-android-master. Project structure is the following:
BaseGameUtils
CastCompanionLibrary-android-master
VideoBrowserActivity
android-support-v7-appcompat
android-support-v7-mediarouter
google-play-services_lib
Really tough error because the compiler doesn't give much information other than 'more than one library with package name 'android.support.v7.appcompat'. Understand why you don't want to be using different versions of the same library, but why doesn't the compiler reveal what versions are being used, and where they are being called from?
Trying to add leaderboard similar to type-a-number challenge (github example project)
There are a total of (7) build.gradle files. One for the top level and one for each of the packages listed above. Top level gradle file:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
BaseGameUtils:
dependencies {
compile 'com.android.support:appcompat-v7:20.0.+'
compile 'com.android.support:support-v4:20.0.+'
compile 'com.google.android.gms:play-services-games:6.5+'
compile 'com.google.android.gms:play-services-plus:6.5+'
compile 'com.google.android.gms:play-services-appstate:6.5+'
}
CastCompanionLibrary-android-master
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:appcompat-v7:20.0.+'
compile project(':android-support-v7-mediarouter')
compile 'com.google.android.gms:play-services-cast:6.5+'
compile project(':BaseGameUtils')
}
VideoBrowserActivity
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':CastCompanionLibrary-android-master')
}
android-support-v7-appcompat
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android-support-v7-mediarouter
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':android-support-v7-appcompat')
}
google-play-services_lib
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
Any help is appreciated on this.
Just go through you project's libs folder not from the Android Studio but from you windows explorer/finder (for windows)/(for mac). And search for android.support.v7.appcompat. Any evidence found related to Apcompat (whether it is v4 or v7) is recommended to be deleted.
Hopefully your problem will be solved.
In your libs folder dont miss any sub directory that has libs folder.
Follow the Steps
1) Remove This repository:
implementation 'com.android.support:appcompat-v7:28.0.0'
2) Add This Repository
implementation 'com.android.support:support-v4:21.0.3'
compile group: 'com.android.support', name: 'appcompat-v7', version: '26.0.1'
3) That's It You Are Ready To Go
I have a Gradle project, composed of two subprojects: UI and backend library.
The library on it's own relies upon another .aar library.
Extract from library's build.gradle:
dependencies {
compile(name: 'ActionBarSherlock', ext: 'aar')
}
Extract from UI's build.gradle:
dependencies {
compile project(":my_library")
compile fileTree(dir: 'libs', include: ['*.jar']) // all jars
}
When I try to build the project, build fails with following error:
> Could not resolve all dependencies for configuration ':UI:_debugCompile'.
> Could not find :ActionBarSherlock:.
Required by:
Project:UI:unspecified > Project:my_library:unspecified
I checked the .aar output for my_library and it does not contain ActionBarSherlock.arr in libs folder. How do I tell Gradle to include .arr files into library output?
transitive = true
seems to be missing. For example (in the build.gradle UI):
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile ('com.your.company.backendlib') {
transitive = true
}
}