Android Studio Gradle Configuration with name 'default' not found - android

I am having problems compiling my app with Android Studio (0.1.5).
The app uses 2 libraries which I have included as follows:
settings.gradle
include ':myapp',':library',':android-ColorPickerPreference'
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
compile project(':library')
compile project(':android-ColorPickerPreference')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
When compiling I get this message:
Gradle: A problem occurred configuring project ':myapp'.
> Failed to notify project evaluation listener.
> Configuration with name 'default' not found.
Could you help me with this message? Thanks!

In my case, after compiling with gradle tasks --info, the log was there :
Evaluating project ':libraries:VolleyLibrary' using empty build
file.
So it failed to find build.gradle file of the library.
Reason is the folder structure.
It was
-libraries
--volley
---VolleyLibrary
It is supposed to be
-libraries
--VolleyLibrary

I forgot to pull all submodules. So my
compile project(':something')
could not be resolved.
Solution
git submodule update --init

compile fileTree(dir: '//you libraries location//', include: ['android-ColorPickerPreference'])
Use above line in your app's gradle file instead of
compile project(':android-ColorPickerPreference')
Hope it helps

The following procedure solved my issue:
Go to your sub-project-module/library-module settings. (press F4 after selecting the module)
Right Click on Add > Android-Gradle.
Add build.gradle to your module.
Add the following script
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "18.1.0"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
add include ':yourModuleName' in your settings.gradle

I've also faced with this error - I forgot to create build.gradle script for library project.

Yet another cause - I was trying to include a module in settings.gradle using
include ':MyModule'
project(':MyModule').projectDir = new File(settingsDir, '../../MyModule')
Only problem was, I had just imported the module from Eclipse an forgot to move the directory outside my application project, i.e. the path '../../MyModule' didn't exist.

My solution is to simply remove a line from the settings.gradle file, which represents a module that doesn't exist:
include ':somesdk'
and also remove the corresponding line from the main project's build.gradle:
compile project(':somesdk')

I had a git submodule problem when getting this error. Try running gradle tasks --info for a more detailed problem description. I first used gradle tasks --debug, which was not nearly as helpful as --info.

Suppose you want to add a library (for example MyLib) to your project, You should follow these steps:
Prepare your library as a gradle Android library if your library's type is Eclipse ADT
For this job it's enough to import it in Android Studio
Copy your library folder, in this case MyLib folder, and paste it in your main project folder. It's a good practice to create a new folder in your main project folder and name it Libraries
Go to Android Studio and open Settings.gradle file, there you should include your library in your main project. For example include ':MyPrj', 'Libraries:MyLib'. Note: Current directory for this include statement is your main project folder. If you paste your project into a folder you must mention it's address relative to the main project directory. In my case I paste MyLib into Libraries folder. This Libraries name is not a keyword and you can choose other names for it
Open your Project Structure page(File->Project Structure...) click on modules then click on your main project go to dependencies tab click on + then choose Module dependency and there is your libraries and you can select them as you want
Hope it help

In my case I was using Gradle files that work under Windows but failed on Linux. The include ':SomeProject' and compile project(':SomeProject') were case sensitive and were not found.

For me folder was missing which was declared under settings.gradle.

Everything looks fine at first blush, but some poking around on here found an answer that could be helpful: https://stackoverflow.com/a/16905808/7944
Even the original answer writer doesn't sound super confident, but it's worth following up on. Also, you didn't say anything about your directory structure and so I'm assuming it's boring default stuff, but can't know for sure.

One other potential cause of this precise error: I found this error was resolved by commenting some unused libraries in build.gradle dependencies section. Make sure these paths and such are all correct.
I'd look real close at your compile project(':android-ColorPickerPreference') entry in the build.gradle - try commenting out the related code and this line in build.gradle and see if that compiles - then go from there resolving the path or library issue.

I had similar issue and found very simple way to add a library to the project.
Create folder "libs" in the root of your project.
Copy JAR file into that folder.
Go back to Android Studio, locate your JAR file and right click it, choose "Add As Library...", it will ask you only to which module you want to add it, well choose "app".
Now in your "app" module you can use classes from that JAR, it will be able to locate and add "import" declarations automatically and compile just okay. The only issue might be is that it adds dependency with absolute path like:
compile files('/home/user/proj/theproj/libs/thelib-1.2.3.jar')
in your "app/build.gradle".
Hope that helps!

I solved this issue by fixing some paths in settings.gradle as shown below:
include ':project-external-module'
project(':project-external-module').projectDir = file('/project/wrong/path')
I was including an external module to my project and had the wrong path for it.

I had this issue when I manually pasted google-play-services_lib into my project. Obviously, play-services didn't have a build.gradle file in it. The solution, I learned, is to put this dependency in my project's build.gradle (instead of hard-copying the play-services directory):
compile 'com.google.android.gms:play-services:4.0.+'

When i import my library manually i had same issue. I tried to add my library with file > import module and it solved my issue.

In my case I received this error when I misspelled the module name of the library (dependency) in build.gradle file.
So remember to check if the name of the module is correct.
build.gradle
dependencies {
compile project(':module-name-of-the-library')
}

I recently encountered this error when I refereneced a project that was initiliazed via a git submodule.
I ended up finding out that the root build.gradle file of the submodule (a java project) did not have the java plugin applied at the root level.
It only had
apply plugin: 'idea'
I added the java plugin:
apply plugin: 'idea'
apply plugin: 'java'
Once I applied the java plugin the 'default not found' message disappeared and the build succeeded.

Also... check if you have the module files inside your project.
For example, I have an app which uses the Volley module. During my studies on Android development, I accidentally removed the files which were inside the "volley" directory..
~/git/Sandbox/android/HelloWorld/volley $ ll
total 8
drwxrwxr-x 2 ivanleon ivanleon 4096 Jun 16 22:26 ./
drwxrwxr-x 6 ivanleon ivanleon 4096 Jun 17 01:51 ../
I just cloned the project (see bellow) and then, I made the Sync of the project at Android Studio (Tools > Android > Sync Project with Gradle Files), and Gradle build finished normally (Gradle Console: bottom right corner of Android Studio) ;).
~/git/Sandbox/android/HelloWorld/volley $ git clone https://android.googlesource.com/platform/frameworks/volley
Cloning into 'volley'...
remote: Counting objects: 164, done
remote: Finding sources: 100% (164/164)
remote: Total 3222 (delta 307), reused 3222 (delta 307)
Receiving objects: 100% (3222/3222), 1.22 MiB | 114.00 KiB/s, done.
Resolving deltas: 100% (307/307), done.
Checking connectivity... done.
~/git/Sandbox/android/AndroidGetJSON $ ls volley
Android.mk build build.xml pom.xml
proguard-project.txt src bintray.gradle build.gradle
custom_rules.xml proguard.cfg rules.gradle volley.iml

Your build.gradle for the module/library could be as simple as:
apply plugin: 'java'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
If your module is just a collection of .java POJO classes.
If it's model / entity classes and you're using annotations and have some dependencies you could add those in:
apply plugin: 'java'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
repositories {
mavenCentral()
}
dependencies {
compile 'com.j256.ormlite:ormlite-core:4.48'
compile 'com.j256.ormlite:ormlite-android:4.48'
compile 'com.j256.ormlite:ormlite-jdbc:4.48'
}

This happens when you are compiling imported or copied folder/project as module in libraries folder. This issue was raising when I did not include the build.gradle file. when I added the file all went just fine.

For me it turned out to be an relative symbolic link (to the referenced project) that couldn't be used by grade. (I was referencing a library). Thats a pretty edgy edge-case but maybe it helps someone in the future.
I solved it by putting a absolute symbolic link.
Before ln -s ../library after ln -s /the/full/path/to/the/library

I also faced the same issue and it resolved by changing one flag (gradle.ext.set("allowLocalEdits", true)) to false in settings.xml.

For me, one of dependent library does not exist in correct path, but the error message does not point THAT library correctly.
For example, what I missed is :library-3 but the error throws at :library-1.
poor gradle.

Try adding Volley library and sync and run the program. if one has pulled and i has volley usage and the error shows as -Android Studio Gradle Configuration with name 'default' not found then follow the step of adding the volley library in your gradle. hope it helps. I cleared my problem this way.

Related

failed to import org.opencv.features2d.DMatch; in android studio

i'm using imageComparator to compare two images with OpenCV library but i can't import DMatch class in my app org.opencv.features2d.DMatch any one knows how should i fix this problem ?
I got the imageComparator project working in Android Studio with openCV 3.1 by following this SO answer
and to fix the failed to import error replace:
import org.opencv.features2d.DMatch;
with
import org.opencv.core.DMatch;
well finally I've found my answer by my self :D
if you guys have this problem too u should download OpenCV library version 2.4.9
EDIT
if you are using newer versions the package name is changed to core
so after a quick research you are right implementing OpenCV with Android Studio requires a few steps to actually get it working properly.
Adding OpenCV to your new project
Create a folder called “libraries” inside your Android Studio
project, and copy there entire content of sdk/java out of your OpenCV
Android folder.
Rename that folder to “opencv”, so that you will end up having your Android Studio project with subfolder “libraries/opencv”.
Now, inside this “opencv” folder, create a build.gradle file, with the following content:
apply plugin: 'android-library'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
}
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 23
versionCode 3000
versionName "3.0.0"
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
aidl.srcDirs = ['src']
}
}
}
** Weird bug Code tag change indentation on the code.
Edit your settings.gradle file in your application’s main directory and add this line:
include ':libraries:opencv'
Open Android Studio
Do this in Android Studio: Tools/Android/Sync Project with Grade files
Go to File/Project Structure, inside Modules pick your ‘app’, then from the Tab pick: Dependencies, click + to add new dependency, pick Module Dependency, and add :library:opencv dependency to your project. Click OK.
Create a jniLibs folder in the /app/src/main/ location and copy the all the folder with *.so files (armeabi, armeabi-v7a, mips, x86) in the jniLibs from the OpenCV Android SDK/native/libs folder.
Make sure that you do have Android SDK 19 installed (as per above gradle files), or use a version that you have installed.
Try to sync Gradle again, after adding the dependency. You may need to delete section “android” from your top-level build.gradle if the sync complains.
Build the project.
Source :: https://blog.hig.no/gtl/2015/10/01/android-studio-opencv/
EDIT 1:
https://www.youtube.com/watch?v=OTw_GIQNbD8 - Youtube video with all the steps required to do from scratch by Md. Zakir Hossen.

Unable to add AndEngine to Android Studio

I am trying to almost 2 days to add AndEngine to Android Studio but unable to do so. I tried the following two methods, neither worked.
1st Try
I download the AndEngine code from GitHub Link -- this is NOT a Gradle Project
and tried to add it to my Android Studio build.gradle and settings.gradle, but i get this error, my screenshot: https://postimg.cc/image/5mcvpvsar/
(I think I am getting this error because AndEngine is not a gradle project - HOW TO MAKE IT A GRADLE PROJECT??)
2nd Try
I have also tried adding the andengine.jar (file I just googled for) in /libs folder and do right-click --> "Add as Library" but still I cannot do "import org.andengine...." in my project files.
All tutorials available online are in Eclipse, I am using Android Studio.
I am not even able to start.
UPDATE: Yes, I gave up using Android Studio for AndEngine! Took me 15mins to do this in Eclipse, compared to the unsuccessful weekend (which i will never get back!) I spent on Android Studio
I use this techique: I set this in my settings.gradle
include 'andengine'
project(':andengine').projectDir = new File(settingsDir, '../relative/path/to/andengine')
that is in the root directory of the project (I think gradle has already created it for your main project).
In AndEngine use a build.gradle like the following for the AndEngine project
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
and add in the dependencies of your project
compile project(':andengine')
Maybe you have to close and reopen Android Studio, but normally for me this works.
BTW after have write the answer I see that someone has opened a pull request for a gradle build file.
I created a tutorial for this - How to add Andengine, Andengine Tile Map, Andengine PhysicsBox2D to Android Studio 0.8.9.
Here is the link, I hope everything works - https://docs.google.com/document/d/1zk2QjNiPvkj52G4qSVivEPrLfkCUVqmnCVH8TfsnER8/edit
ANDENGINE WITH ANDROID STUDIO 0.8.9
Note: I am using the AnchorCenter brach and TortoiseGit to get all the files.
Download Andengine from github using TortoiseGit: https://github.com/nicolasgramlich/AndEngine
After the dowload use TortoiseGit to switch to branch GLES2-AnchorCenter
Create new project in Android Studio
Create new module:
Select File -> New Module -> Android Library
Set Application name to AndEngine
Set Module Name to AndEngine
Set Package Name to org.andengine
Set Minimum SDK 14
Target SDK 19
Compile with 19
Theme None
Keep clicking next until module is created (no difference what you pick)
Enter the folder where you have downloaded Andengine, enter src/org/andengine and copy all the files inside.
Paste the copied files into your new module in your project src/java/org.andengine. After pasting everything remove tha MainActivity that was created on default
Enter the folder where you have downloaded Andengine, copy AndroidManifest and paste it into your new module (src/main)
Add the module to the project:
Select File-> Project Structure -> app-> Dependencies
Click the “+” button and pick “Module Dependency”
Select from the list your AndEngine Module
Check your project gradle in app folder (build.gradle) and make sure you have a line like this under dependencies - compile project(':AndEngine')
You should now be able to use AndEngine in your project
ANDENGINE TMX TILED MAP EXTENSION WITH ANDROID STUDIO 0.8.9
Note: We do this the same way like with Andengine but we change a few things:
Download AndengineTMX from github using TortoiseGit: https://github.com/nicolasgramlich/AndEngineTMXTiledMapExtension
After the dowload use TortoiseGit to switch to branch GLES2-AnchorCenter
Create new project in Android Studio
Create new module:
Select File -> New Module -> Android Library
Set Application name to AndEngineTMXTiledMapExtension
Set Module Name to AndEngineTMXTiledMapExtension
Set Package Name to org.andengine.extension.tmx
Set Minimum SDK 14
Target SDK 19
Compile with 19
Theme None
Keep clicking next until module is created (no difference what you pick)
Enter the folder where you have downloaded AndengineTMX , enter src/org/andengine/extension/tmx and copy all the files inside.
Paste the copied files into your new module in your project src/java/org.andengine.extension.tmx. After pasting everything remove tha MainActivity that was created on default
Enter the folder where you have downloaded AndengineTMX, copy AndroidManifest and paste it into your new module (src/main)
Add the module to the project:
Select File-> Project Structure -> app-> Dependencies
Click the “+” button and pick “Module Dependency”
Select from the list your AndengineTMX Module
Check your project gradle in app folder (build.gradle) and make sure you have a line like this under dependencies - compile project(':AndEngineTMXTiledMapExtension')
You should now be able to use AndEngineTMXTiledMapExtension in your project.
ANDENGINE PHYSICSBOX2D WITH ANDROID STUDIO 0.8.9
Download this file - http://d-h.st/FyC
Unzip the file
You should have 2 jar files, copy them to your project app/libs
Right click on andenginephysicsbox2dextension.jar and select “Add as library” (or something like this)
Open your build.gradle in your app folder
Under dependencies add compile files('lib/physicsbox2d_so_files.jar')
You should now be able to use PhysicsBox2D in your project.
As the question's answers havn't been accepted yet, and I know people are still searching for solutions to this, I found this great website with very very clear and concise instructions to import AndEngine to your Android Studio projects. Here's the link:
http://geq-i.blogspot.com/2014/02/how-to-setup-andengine-in-android-studio.html
All credits go to the user who created this page. I can attest to the fact that this is working perfectly. I JUST used this website after trying 10s of different ways.
Only thing to note on the link is the last part:
$ cd <project folder>/AndEngine/src/main
$ rm -r java/org
$ mv org java
This part is copying the org folder from . to ./main/java. Best way to do this is to just drag and drop the org folder into main/java when the project has finished building once.
Hope this helps!
Well i had the same problem - This helped me to solve it.
http://www.makethegame.net/android-andengine/how-to-setup-andengine-with-android-studio/
Check out this tutorial on how to use andengine in android studio
http://javaprogrammernotes.blogspot.in/2014/05/settings-up-andengine-in-android-studio.html
Brief Summary of the Tutorial(Check out the full tutorial if you face any issue):
Let's assume that you already have created a project and it has default structure. First create folder named third_party in the root directory of the project. Then in third_party directory create subdirectories called andengine and andenginebox2d. I assume that you already downloaded or cloned AndEngine and Box2d extension for it. Put AndEngine and AndEngineBox2d in andengine and andenginebox2d directories respectively. Create file named build.gradle in andengine directory and andenginebox2d directory. Build.gradle files is a file that tells gradle how to build your project.
apply plugin: 'android-library'
android {
compileSdkVersion 17
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-project.txt')
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
Open settings.gradle that is located in your project's root directory and add two lines to it:
include ':third_party:andengine'
include ':third_party:andenginebox2d'
Next open build.gradle which locates in app directory and add
compile project(':third_party:andengine')
The final step is to open AndroidManifest.xml in andegine and andenginebox2d directories and make them look like this:
<!--?xml version="1.0" encoding="utf-8"?-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.andengine">
<application>
</application>
</manifest>
application block is needed because of bug in manifest merging tool.
That's it! Now clean your project and press run. Everything should work just fine.

How can I import a new module (android library project) using Android studio 0.3.0

The android tools team made a huge step when introducing android studio 0.3.0 with the new user interface for modifying the build.grade file using the project structure.
But how can I import an android library project into my general project? When I press the '+' button in the Project structore -> Modules section I can only create NEW module.
The way I just did it was by copy pasting the library project in the root of your project (not using android studio, this gives a 'cannot create class-file error', but just using the file manager of your operating system. When using android studio for this it didn't copy some of the files).
Then mark the java folder of the library project as 'source root' (right mouse on the folder).
Then to settings.gradle add your lib project like this:
include ':youApp', ':yourLibrary'
then to build.gradle of yourApp add dependancy:
dependencies {
compile project(':yourLibrary')
}
Now rebuild your project.
Now add a function of the library project (showing red) when you click on it and press Alt-Enter it should say "add dependancy on module yourAppProject"
In android studio 0.3.1 they fixed it.
Project structure -> Modules -> "+" -> Import module.
It's still broken 0.3.6...
The manual way can be a good solution. For example, to import a library like Android-Validator (https://github.com/throrin19/Android-Validator) in your project "Test":
The structure will be:
Test
build.gradle
Android-Validator (the library sources)
build.gradle
src
res
settings.gradle
And now, let's gonna edit the files...
In Test/build.gradle add:
dependencies {
compile project(':Android-Validator')
}
Android-Validator/build.gradle (remember to change xx and yy):
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion XX
buildToolsVersion "XX.0.0"
defaultConfig {
minSdkVersion YY
targetSdkVersion XX
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
settings.gradle:
include ':Test', ':Android-Validator'
I had a very similar issues with gradle builds / adding .jar library. I got it working by a combination of :
Moving the libs folder up to the root of the project (same directory as 'src'), and adding the library to this folder in finder (using Mac OS X)
In Android Studio, Right-clicking on the folder to add as library
Editing the dependencies in the build.gradle file, adding: compile fileTree(dir: 'libs', include: '*.jar')}
But annoyingly, HOURS after I get it working, Android Studio have just released 0.3.7, which claims to have solved a lot of gradle issues such as adding .jar libraries
http://tools.android.com/recent
Hope this helps!

How do I add Guava to my Android Studio project?

First and foremost, I am aware of the existence of this question - How do I add a library project to Android Studio? - and unfortunately, it has not helped me.
My goal is rather simple. I want to write an Android app using the Android Studio IDE (0.2.11), and the Guava libraries in my code.
I do not know Gradle, I've only started using Android Studio and my Visual Studio/C# background has dumbed me down, for which I apologize (in that Mickey Mouse world, you typically just add a library reference and off you go).
I will document my steps with screenshots. I mostly followed advice given in this answer.
I created a libraries folder under my project folder.
I cloned Guava repository into it.
Files successfully appeared.
I went to Project Structure and selected Import Module.
I selected Create module from existing sources and agreed to all the default choices.
I updated my settings.gradle file to include ':libraries:guava', ':Test':
And my build.gradle file with compile project(":libraries:guava"):
But all I'm getting whenever I'm trying to rebuild the project is:
Error: Gradle: A problem occurred configuring project ':Test'.
> Failed to notify project evaluation listener.
> Configuration with name 'default' not found.
I did try putting a build.gradle as below in the guava folder:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:13.0.+'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
(as the aforementioned answer says).
I have googled up and down to find the "correct" build.gradle for Guava.
https://code.google.com/p/guava-libraries/wiki/UseGuavaInYourBuild - didn't help me, either.
I did try countless things which I will not describe here as they were rather haphazard - I tried adding a module dependency, I tried turning Use auto-import on in Gradle settings, etc.
I know it's not a way of solving issues and I promise I will diligently read Gradle's User Guide from 1 through 5.4.2 to 7.3, but I can't believe this is really prerequisite to achieve something as unremarkable as merely adding a library to a project? Why is there no default build.gradle file from which one could start to fiddle with all sorts of things if necessary?
And the real question - how do I create an app (in Android Studio) that builds, actually runs on an Android device and on the top of that allows me to use Guava so I could sort a map by values without writing 50 lines of code? :)
Sorry about the chatty tone of my question, I know the drill around here, it's just my way of venting my frustration off.
Judging by how many votes were casted for questions and answers that tackled similar issues, I'm sure I'm not the only one who would benefit from some more instructions. I would start a bounty on it straight away, but the rules forbid me.
If you just need to use a stable, released version of the Guava libraries, importing it is extremely easy.
Just go to the build.gradlefile of the module where you want to use the library (i.e GuavaTestProject/GuavaTest/build.gradle) and, right after
repositories {
mavenCentral()
}
add a Maven dependency:
dependencies {
compile group: 'com.google.guava', name: 'guava', version: '15.0'
}
Rebuild your project if needed and that's all (tested right now with a fresh project created with Android Studio 0.2.13).
If you really need to include the source code of the Guava library and compile it yourself as a module of your Gradle build that's an entirely different problem because Guava is build with Maven and so you need to move the Guava build system from Maven to Gradle, which I think is overwhelmingly complex for your goals.
If you just need to browse the source or view it while debugging, what I would do is:
Download Guava source code on a separate folder:
git clone https://code.google.com/p/guava-libraries/
git checkout v15.0
When Android Studio doesn't find the sources, click on "Attach sources" and point to this alternative location.
I think if you don't need to actually modify and compile Guava source code this is the easiest solution.
dependencies {
compile 'com.google.guava:guava:19.0'
}
Source: https://github.com/google/guava
EDIT:
As of v22, there's specific guava version for Android.
dependencies {
compile 'com.google.guava:guava:22.0-android'
}
* Thanks Sam :)
Thats a lot of info in your question.
I am using Guava too. But I don't remember going through any of this trouble..
Guava is available on the mavencentral. So adding guava to your project should be fairly simple. AFAIK, you do not need to checkout, build and add a project dependency etc..
See gradle file for my app below.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
// Dependencies
dependencies {
compile fileTree(dir: 'libs', include: '*.jar') // jar files under the libs folder.
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar' // actionbarsherlock,
compile 'com.android.support:support-v4:18.0.0' // android support lib
compile 'com.google.code.gson:gson:2.2.4' // google GSON
compile 'com.google.guava:guava:14.0.1' // guava 14.0.1
}
do not use the full guava library, use the version specific for android,
dependencies {
compile 'com.google.guava:guava:23.0-android'
}
In Android Studio 0.3.0 This is now working as it should on Mac. See the release notes for additional options in adding libraries
http://tools.android.com/recent/androidstudio030released
I have been working on Windows now and there are two options
Right click on a jar in libs and you have an add to library
Or you can push F4 and get to open library settings so it seems you do not have to struggle as much on Windows as on a Mac
This is what I did. Please read to the end since it works inconsistently and if you do not see the blue arrow first try there is a resolution. First I created a libs folder. I do not think this is needed though but it is a habit.
First try go to File , Project Structure.
If you see a little blue arrow at the top left push it and you will go to the screen where you can add a library
If you manage to get to this screen below push the plus sign and add the library.
You may also see a red line at the bottom saying Guava not used with a light bulb. This fill add the dependancy to the Gradle Build File
IF YOU DONT SEE THE BLUE ARROW
GO instead to File, Other settings, Default project structure.
From that screen you can add the library
Then go back to project structure and add it. The thing is that it will remain as a default for all your projects so you can add and remove it for each individual project via the project structure menu. I am not sure if this is a bug in Android Studio the fact that you can be blocked from adding it to the individual project without changing the default.
For Android Studio 3.2.1:
Click the File/Project Structure menu (ctrl+alt+shift+S). Press the + button in the Dependencies tab from the app menu to add a Library dependency.
Look for com.google.guava:guava:(whatever number is the most recent one) and add it.

How to import relative project with the Android Studio(not jar) [duplicate]

How do I add a library project (such as Sherlock ABS) to Android Studio?
(Not to the old ADT Eclipse-based bundle, but to the new Android Studio.)
Update for Android Studio 1.0
Since Android Studio 1.0 was released (and a lot of versions between v1.0 and one of the firsts from the time of my previous answer) some things has changed.
My description is focused on adding external library project by hand via Gradle files (for better understanding the process). If you want to add a library via Android Studio creator just check the answer below with visual guide (there are some differences between Android Studio 1.0 and those from screenshots, but the process is very similar).
Before you start adding a library to your project by hand, consider adding the external dependency. It won’t mess in your project structure. Almost every well-known Android library is available in a Maven repository and its installation takes only one line of code in the app/build.gradle file:
dependencies {
implementation 'com.jakewharton:butterknife:6.0.0'
}
Adding the library
Here is the full process of adding external Android library to our project:
Create a new project via Android Studio creator. I named it HelloWorld.
Here is the original project structure created by Android Studio:
HelloWorld/
app/
- build.gradle // local Gradle configuration (for app only)
...
- build.gradle // Global Gradle configuration (for whole project)
- settings.gradle
- gradle.properties
...
In the root directory (HelloWorld/), create new folder: /libs in which we’ll place our external libraries (this step is not required - only for keeping a cleaner project structure).
Paste your library in the newly created /libs folder. In this example I used PagerSlidingTabStrip library (just download ZIP from GitHub, rename library directory to „PagerSlidingTabStrip" and copy it). Here is the new structure of our project:
HelloWorld/
app/
- build.gradle // Local Gradle configuration (for app only)
...
libs/
PagerSlidingTabStrip/
- build.gradle // Local Gradle configuration (for library only)
- build.gradle // Global Gradle configuration (for whole project)
- settings.gradle
- gradle.properties
...
Edit settings.gradle by adding your library to include. If you use a custom path like I did, you have also to define the project directory for our library. A whole settings.gradle should look like below:
include ':app', ':PagerSlidingTabStrip'
project(':PagerSlidingTabStrip').projectDir = new File('libs/PagerSlidingTabStrip')
5.1 If you face "Default Configuration" error, then try this instead of step 5,
include ':app'
include ':libs:PagerSlidingTabStrip'
In app/build.gradle add our library project as an dependency:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:21.0.3'
implementation project(":PagerSlidingTabStrip")
}
6.1. If you followed step 5.1, then follow this instead of 6,
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:21.0.3'
implementation project(":libs:PagerSlidingTabStrip")
}
If your library project doesn’t have build.gradle file you have to create it manually. Here is example of that file:
apply plugin: 'com.android.library'
dependencies {
implementation 'com.android.support:support-v4:21.0.3'
}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
Additionally you can create a global configuration for your project which will contain SDK versions and build tools version for every module to keep consistency. Just edit gradle.properties file and add lines:
ANDROID_BUILD_MIN_SDK_VERSION=14
ANDROID_BUILD_TARGET_SDK_VERSION=21
ANDROID_BUILD_TOOLS_VERSION=21.1.3
ANDROID_BUILD_SDK_VERSION=21
Now you can use it in your build.gradle files (in app and libraries modules) like below:
//...
android {
compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
defaultConfig {
minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
}
}
//...
That’s all. Just click‚ synchronise the project with the Gradle’ icon . Your library should be available in your project.
Google I/O 2013 - The New Android SDK Build System is a great presentation about building Android apps with Gradle Build System: As Xavier Ducrohet said:
Android Studio is all about editing, and debugging and profiling.
It's not about building any more.
At the beginning it may be little bit confusing (especially for those, who works with Eclipse and have never seen the ant - like me ;) ), but at the end Gradle gives us some great opportunities and it worth to learn this build system.
Here is the visual guide:
Update for Android Studio 0.8.2:
In Android Studio 0.8.2, go to Project Structure -> under Modules just hit the plus button and select Import Existing Project and import actionbarsherlock. Then synchronise your Gradle files.
If you face the error
Error: The SDK Build Tools revision (xx.x.x) is too low. Minimum
required is yy.y.y
just open the build.gradle file in actionbarsherlock directory and update the buildToolsVersion to the suggested one.
android {
compileSdkVersion 19
buildToolsVersion 'yy.y.y'
Menu File -> Project Structure...:
Module -> Import Module
After importing the library module, select your project module and add the dependency:
And then select the imported module:
Use menu File -> Project Structure -> Modules.
I started using it today. It is a bit different.
For Sherlock, maybe you want to delete their test directory, or add the junit.jar file to the classpath.
To import the library using gradle, you can have to add it to the dependencies section of your build.gradle (the module's one).
E.g.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.0'
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
}
Android Studio is changing.
There exist a section named "Open module settings" if you
right-click on a module folder in the project section of Android
Studio (I'm using the version 0.2.10).
I would consider Dependencies, Android Libraries and Multi-project setup necessary reading. Please take a few minutes to do so.
Particularly, in the case of a non-jar library project, read the following snippet from above source:
Gradle projects can also depend on other gradle projects by using a multi-project setup.
A multi-project setup usually works by having all the projects as sub folders of a given root project.
For instance, given to following structure:
MyProject/
+ app/
+ libraries/
+ lib1/
+ lib2/
We can identify 3 projects. Gradle will reference them with the following name:
:app
:libraries:lib1
:libraries:lib2
Each projects will have its own build.gradle declaring how it gets built.
Additionally, there will be a file called settings.gradle at the root declaring the projects.
This gives the following structure:
MyProject/
| settings.gradle
+ app/
| build.gradle
+ libraries/
+ lib1/
| build.gradle
+ lib2/
| build.gradle
The content of settings.gradle is very simple:
include ':app', ':libraries:lib1', ':libraries:lib2'
This defines which folder is actually a Gradle project.
The :app project is likely to depend on the libraries, and this is done by declaring the following dependencies:
dependencies {
compile project(':libraries:lib1')
}
Kindly note that there was little or no use of Android Studio GUI to make this happen.
I am currently using git submodules to link the nested library to the actual library git repo to avoid a dependency mess.
I have just found an easier way (rather than writing directly into the .gradle files).
This is for Android Studio 1.1.0.
Menu File -> New Module...:
Click on "Import Existing Project".
Select the desired library and the desired module.
Click finish.
Android Studio will import the library into your project. It will sync gradle files.
Add the imported module to your project's dependencies.
Right click on the app folder -> Open Module settings -> go to the dependencies tab -> Click on the '+' button -> click on Module Dependency.
The library module will be then added to the project's dependencies.
???
Profit
The easiest way I found to include external library project is (for example to include a Facebook library which is stored one directory up in the dependencies folder):
In settings.gradle add
include ':facebook'
project(':facebook').projectDir = new File(settingsDir, '../dependencies/FacebookSDK')
In build.gradle dependencies section, add
compile project ('facebook')
All left to do is synchronise the project with gradle files.
A simple way to add a JAR file as a library to your Android Studio project:
a) Copy your *.jar files
b) Paste into the libs directory under your projects:
c) Add to build.gradle:
dependencies {
...
compile files('libs/ScanAPIAndroid.jar', 'libs/ScanAPIFactoryAndroid.jar', .., ..)
}
b) If your project from example com.example.MYProject and libraries com.example.ScanAPI has the same namespace com.example, Android Studio will check your build and create all necessary changes in your project. After that you can review these settings in menu File -> Project Structure.
c) If your project and libraries have a different namespace you have to right click on the library and select option "Add as Library" and select the type what you need.
Remember the "Project structure" option is not doing any auto changes in "build.gradle" in the current version of Android Studio (0.2.3). Maybe this feature will be available in the next versions.
Option 1: Drop Files Into Project's libs/directory
The relevant build.gradle file will then update automatically.
Option 2: Modify build.gradle File Manually
Open your build.gradle file and add a new build rule to the dependencies closure. For example, if you wanted to add Google Play Services, your project's dependencies section would look something like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services:6.5.+'
}
Option 3: Use Android Studio's User Interface
In the Project panel, Control + click the module you want to add the dependency to and select Open Module Settings.
Select the Dependencies tab, followed by the + button in the bottom-left corner. You can choose from the following list of options:
Library Dependency
File Dependency
Module Dependency
You can then enter more information about the dependency you want to add to your project. For example, if you choose Library Dependency, Android Studio displays a list of libraries for you to choose from.
Once you've added your dependency, check your module-level build.gradle file. It should have automatically updated to include the new dependency.
Source
You can do this easily. Go to menu File -> New -> Import Module...:
Browse for the directory which contains the module. Click Finish:
Go to Project Structure and add Module Dependency:
Note: If you receive an SDK error, update that one.
This is how it works for me in Android Studio 1.5+
In the project where you want to add external library project,
go to menu File -> New -> *Import new Module**, navigate to the library project which you want to add to your project, select to add 'library' module in your project. You will get settings.gradle in your projects, beside app, included library, something like this:
include ':app', ':library'
Add in build.gradle(Module :app) in the dependencies section:
Compile project(':library')
Rebuild the project, and that's it.
*You can add as many libraries (modules) as you want. In that case in settings.gradle you will have:
include ':app', ':lib1', ':lib2', ...
And in build.gradle, you'll need to have:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// Some other dependencies...
compile project(':lib1')
compile project(':lib2')
...
}
Press F4 to show Project Structure, click libraries or Global libraries, and click + to add the JAR file.
Click Modules what you want add jar, select the Dependencies tab, click +, and add Library.
If you need access to the resources of a library project (as you do with ABS) ensure that you add the library project/module as a "Module Dependency" instead of a "Library".
Editing library dependencies through the GUI is not advisable as that doesn't write those changes to your build.gradle file. So your project will not build from the command-line. We should edit the build.gradle file directly as follows.
For instance, given to following structure:
MyProject/
app/
libraries/
lib1/
lib2/
We can identify three projects. Gradle will reference them with the following names:
:app
:libraries:lib1
:libraries:lib2
The :app project is likely to depend on the libraries, and this is done by declaring the following dependencies:
dependencies {
compile project(':libraries:lib1')
}
Android Studio 3.0
Just add the library name to the dependencies block of your app's build.gradle file.
dependencies {
// ...
implementation 'com.example:some-library:1.0.0'
}
Note that you should use implementation rather than compile now. This is new with Android Studio 3.0. See this Q&A for an explanation of the difference.
To add to the answer: If the IDE doesn't show any error, but when you try to compile, you get something like:
No resource found that matches the given name 'Theme.Sherlock.Light'
Your library project is probably compiled as an application project. To change this, go to:
Menu File -> Project structure -> Facets -> [Library name] -> Check "Library module".
First Way This is working for MacBook.
First select your builder.gradle file as given screen:
Add dependencies like as on the selected screen:
Select sync project.
If you are getting an error like "Project with path':signature-pad' could not be found in project ':app'", then please use the second way:
Select menu File -> New -> Import Module...:
After clicking on Import Module,
give the path of library like as my MacBook path:
Click on Finish. Now your library are added.
After importing the ABS Module (from File > Project Structure) and making sure it has Android 2.2 and Support Library v4 as dependencies, I was still getting the following error as you #Alex
Error retrieving parent for item: No resource found that matches the given name 'Theme.Sherlock.Light.DarkActionBar'
I added the newly imported module as a dependency to my main app module and that fixed the problem.
To resolve this problem, you just need to add the abs resource path to your project build file, just like below:
sourceSets {
main {
res.srcDirs = ['src/main/res','../../ActionBarSherlock/actionbarsherlock/res']
}
}
So, I again compile without any errors.
If you have Android Studio .0.4.0, you can create a new folder in your build path, YourApp/libraries. Copy the JAR file. There in, right click on it and "Add As Library". Now you have a popup. Just select your directory and press OK, and that's it.
Simply import the Android library project as a module and in Build.gradle.
Apply plugin: 'com.android.library'
After that, follow these steps:
Right click on Module & select open Module settings
Select dependencies, click on +, select library dependencies, and add the previously imported module.
https://www.dropbox.com/s/1e3eteu3h0pmkf7/Android%20studio%20_doc.doc?dl=0 is the Dropbox link of how to add a JAR file and library project in the latest version of Android Studio 1.0.1.
Please see the documentation with screenshots. It's very easy for a new user.
I found the solution. It's so simple. Follow froger_mcs instructions.
Make sure that you make the src folder a Source folder in Project Structure -> Modules (Sources).
Basically, you can include your JAR files in three different ways. The last one is remote library that is using https://bintray.com/ jcenter online repository. But, if you do it in one of the two other ways, the JAR file will be included physically in your project. Please read this link https://stackoverflow.com/a/35369267/5475941 for more information. In this post I explained how to import your JAR file in Android studio and I explained all possible ways.
In summary, if it is like this (local address), they are downloaded and these JAR files are physically in the project:
But, if it is an internet address like this, they are remote libraries (bintray.com jcenter part) and they will be used remotely:
I hope it helps.
Open the build gradle module app file and add your dependency. If you download the library, just import and build as gradle.
Otherwise add repositories in side gradle module app:
repositories {
maven { url 'http://clinker.47deg.com/nexus/content/groups/public' }
}
The first repositories will download the library for you.
And compile the downloaded library:
compile ('com.fortysevendeg.swipelistview:swipelistview:1.0-SNAPSHOT#aar') {
transitive = true
}
If you are creating a library, you just need to import the project as import new module.
I had a different cause of the problem so for people:
repositories {
mavenCentral()
}
change mavenCentral() to jcenter() and add
allprojects {
repositories {
jcenter()
}
}
In Android Studio, go to inside app folder, and open build.gradle file. Here you will see dependencies{}. Inside it you can add the library project and synchronise. Now after synchronising the library it will be added to your project, and you can use its functions and classes in your project.
For Android Studio:
Click on Build.gradle (module: app).
And add for
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/commons-io-2.4.jar')
}
and in your directory "app", create a directory, "libs". Add the file yourfile.jar:
Finally, compile the Gradle Files:
I also encountered the same problem then I did following things.
I import the library project into my AndroidStudio IDE as a module using menu File -> Import module menus
Then I went to my main module in which I want the library project as a dependent project
Right click on the main module (in my case its name is app) -> open module setting -> go into dependencies tab -> click on + button (you will get it on right side of window) -> click on module dependency -> select your library project from list
Apply the changes and click the OK button.
It worked for me. I hope it will help others too.
You are able to use Gradle dependency configuration[About] to add some dependency into your project
<module_name>/build.gradle
dependencies {
//<gradle_dependency_configuration> '<dependency_name>:<dependency_version>'
//e.g.
implementation 'com.android.support:appcompat-v7:1.1.1'
}
Indeed as versions are changing, so is changing the user interface and options available on the menu. After reading most of the answers to these questions I had to guess what would work for Android Studio 1.1.0.
With your mouse, select the project at the main level (this is where it shows the name of your app).
Right click, and select the menu options New, Folder, Assets Folder.
After creating the assets folder, paste or copy in it, whatever JAR file you need for your library.
From Android Studio's main menu (top of the screen) select File -> Project Structure.
Then select your project name and go to the Dependencies tab.
Click on the plus sign (+) on the lower left of the dialog box and select file dependency.
Finally open the recently created assets folder, select the JAR files that you copied, and then click apply and OK.
Clean and rebuild your project.

Categories

Resources