2

I am adding a Easypost library to my Android peoject and from my understanding I need to reference it at both settings.gradle and build.gradle. But when I do that, I get the error:

Error:Circular reference between projects: 
:Libs:easypost-java-master -> :Libs:easypost-java-master

If I remove the reference at either one of them, I am able to sync without errors but unable to import com.easypost.EasyPost. So I am guessing I do need to reference at both places but how do I avoid the circular reference error?

Settings.Gradle

include ':app'
include ':Libs:easypost-java-master'

Build.Gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile project(':Libs:easypost-java-master')
}

My project structure

Project Structure

KFunk
  • 2,956
  • 22
  • 33
JasSy
  • 583
  • 5
  • 17

5 Answers5

3

The build.gradle file you marked contains a reference to itself as dependency. This is the shortest possible circular reference. If you remove

compile project(':Libs:easypost-java-master')

you might be fine. I am saying might because I gave up on IntelliJ Idea. Instead of making things easier with regard to Gradle, it just introduced an additional, redundant layer of complexity that I had deal with instead. IntelliJ Idea just did not work out for me.

Best advice I can give is to setup your project manually first in order to understand how gradle handles projects. It is a steep learning curve but in my case it saved a lot more time and headaches in the long run.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Removing it does let me sync fine but its useless as I am not able to do the necessary import com.easypost.EasyPost – JasSy Dec 30 '15 at 00:03
  • Where are you doing the import? I am assuming in another module? You will have to declare the dependency in that project's/module's build.gradle file. –  Dec 30 '15 at 00:06
  • Yes I am declaring in the related module's build.gradle. – JasSy Dec 30 '15 at 00:10
  • So which one are you declaring it? In the screenshot it looks like you are declaring it in the same module. I see at least three build.gradle files. Which one belongs to the module that has the class that needs the import? –  Dec 30 '15 at 00:14
  • The one right at bottom is for the whole project. Fairly certain I should not be messing anything inside there. The one I am pointing to (center) is inside the library (easypost-java-master) which I am trying to add. Right on the top is the one for the project when I initially create the project where MainActivity exists. – JasSy Dec 30 '15 at 00:18
  • You still have not answered the most important question: Where is the class that needs the import statement? –  Dec 30 '15 at 00:21
  • I am trying to import at MainActivity.class – JasSy Dec 30 '15 at 00:25
  • Then you will have to declare the dependency in that build.gradle file. The one in the "app" folder. –  Dec 30 '15 at 00:27
  • I am not familiar with the way IntelliJ creates those default build.gradle files, however, make sure that you do NOT declare the dependency inside a buildscript{ ... } block. This block is recommended by Google to have and it also declares some dependencies inside. Not sure if Idea creates it, too. Your dependency must be declared inside its own dependency block which is not enclosed by another block. Post all your build.gradle files and settings files if this does not fix it. –  Dec 30 '15 at 00:48
1

Just make sure that you have used the exact same name in your build.gradle file for the app and in settings.gradle file in Gradle Scripts.

Like compile project(':lib') in build.gradle file & in settings.gradle file include ':sample', ':lib' both should have same name ':lib' for the library module.

This will solve the issue.

0

Remove include ':Libs:easypost-java-master' from Settings.gradle and remove compile project(':Libs:easypost-java-master') from build.gradle.

If you put your library in the "libs", the line compile fileTree(dir: 'libs', include: ['*.jar']) will add it the the build steps.

Distwo
  • 11,569
  • 8
  • 42
  • 65
  • If I remove that, I am not able to import com.easypost.EasyPost. Keeping it there doesn't let me sync either thus not sure if I indeed need it at both places. – JasSy Dec 29 '15 at 23:43
  • Make sure the jar is in the lib folder, then remove imports in both, settings and build.gradle – Distwo Dec 29 '15 at 23:47
  • It is a project with .java files. There is no jar files. It is from https://github.com/EasyPost/easypost-java – JasSy Dec 29 '15 at 23:51
  • Removed previous include and compile and added your compile fileTree(dir: 'libs', include: ['*.jar']). Able to sync fine but still unable to import com.easypost.EasyPost – JasSy Dec 29 '15 at 23:59
0

You can add the following snippet with your dependency issue appropriately:

compile 'dep1' {
  exclude module: 'dep2' 
}
compile 'dep2' {
  exclude module: 'dep1' 
}

If it is not straightforward, follow the logic. It is also an option to remove dependency, since it is a subdependency.

Iliiaz Akhmedov
  • 867
  • 7
  • 17
0

You are putting the library reference in the build.gradle that is in the library. You should be putting it in the app build.gradle file.

The settings.gradle will try to get the library's build.gradle. It will then attempt to compile it, but it includes a reference to itself. So when it tries to compile that reference, it grabs the same build.gradle file, tries to compile and finds a reference to itself... and so forth.

Move this line:

compile project(':Libs:easypost-java-master')

to your app build.gradle and you should be fine.

Jim
  • 10,172
  • 1
  • 27
  • 36