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'
}
}
Related
I imported a project into Android Studio, but the project is not getting built.
the error message I get is:
Error:(20, 0) Could not find method compile() for arguments [file collection]
on object of type
org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
<a href="openFile:C:\Users\xxx\AndroidStudioProjects\WSAppAndroid
\build.gradle">Open File</a>
As shown in image-1 posted below, in gradle.build (project) I have these libraries in the dependencies section.
I googled how to solve this issue and among the posts I found was this one which tackles the same problem. But the accepted answer does not solve the problem i have.
As an attempt to solve this issue, can I use compile instead of compile files
in build.gradle? how it can be done?
my question is
image-1
build.gradle (project):
// Top-level build file where you can add configuration options common to
all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7' // 1.8?
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
dependencies {
compile files('gradle/wrapper/gradle-wrapper.jar')
compile files('app/libs/android-viewbadger.jar')
compile files('app/libs/iDappsImagesLib_v0.2.jar')
compile files('app/libs/iDappsToolsLib_v0.1.jar')
compile 'com.android.support:support-v4:25.2.0'
}
compile-on-org-gradle-api-internal-artifacts-dsl-depen/33991915#33991915
compile has been replaced by implementation in newer version of gradle, replace compile with implementation and compile group with implmenetation group in your build.gradle file.
If you want to add your local jars as dependencies I would suggest.
Adding apply plugin: 'java'
Adding your folder as a repository
repositories {
flatDir {
dirs 'libs'
}
}
then add them to the dependencies.
dependencies {
compile name: 'whatever'
}
another thing is why do you add the Gradle wrapper as a dependency?
well the problem here is that in the project uses local libs , so you need to add compile fileTree(include: ['*.jar'], dir: 'libs') now gradle will import the jars located in libs folder.
so you will have
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('gradle/wrapper/gradle-wrapper.jar')
compile files('app/libs/android-viewbadger.jar')
compile files('app/libs/iDappsImagesLib_v0.2.jar')
compile files('app/libs/iDappsToolsLib_v0.1.jar')
compile 'com.android.support:support-v4:25.2.0'
}
I wrote a library-project cameraBarcodeScanner that is built into an aar file. This library has the following dependencies defined in its build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.google.zxing:core:3.2.1'
compile 'com.journeyapps:zxing-android-embedded:3.2.0#aar'
}
I'm using the library in a test-application like so:
dependencies {
compile(name: 'cameraBarcodeScanner-release', ext: 'aar')
}
Gradle finds the application and is able to build it. However, on runtime, android is not able to find the classes, that are located in zxing-android-embedded. It seems to me, that gradle is not downloading the dependencies of the library-project.
I also tried:
dependencies {
compile(name: 'cameraBarcodeScanner-release', ext: 'aar'){
transitive = true
}
But this didn't help either. Do I have to expose the library's dependencies in some way? How would you use a library and make gradle download its dependencies?
Thanks in advance!
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.
You should use a maven repository (you have to publish the library in a private or public maven repo), 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 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
//...
}
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 ))
}
i am new to android studio and wants to include boofcv library in my project. I am using Android studio for development. I have done the following steps in order to include the library and is stuck with build.gradle configuration.
Step 1: Have downloaded per-compiled jar files from http://boofcv.org/index.php?title=Download:BoofCV
Step 2: Have updated settings.gradle as
include ':app'
include ':libs:boofcv-libs'
Step 3: My build.gradle looks like:
apply plugin: 'com.android.application'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
allprojects {
repositories {
jcenter()
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
As the note of your project's build.gradle file will suggest:
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Remove the compile statements in that gradle file:
compile project(":libs:boofcv-libs")
And copy them to other (module's) build.gradle and make dependencies look like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:20.+'
compile project(":libs:boofcv-libs")
}
BoofCV is on maven central so you could just do the following too:
['calibration','feature','geo','ip','recognition','sfm','android'].each
{ String a -> compile group: 'org.boofcv', name: a, version: '0.18' }
In the next it will be even easier if you just want everything:
compile group: 'org.boofcv', name: "all", version: '0.19-SNAPSHOT'
Get latest version from this page
https://boofcv.org/index.php?title=Download
Convert maven to to gradle using this website and make sure to
change artifactId to boofcv-android:
http://sagioto.github.io/maven2gradle
so it will be something like this:
compile "org.boofcv:boofcv-android:0.27"
as mentioned in this page, to avoid library conflict add this to app.gradle:
// Remove libraries that conflict with libraries already included with Android
configurations {
all*.exclude group: "xmlpull", module: "xmlpull"
all*.exclude group: "org.apache.commons", module: "commons-compress"
}