0

I am trying to import my own library into one of my apps in Android Studio. I have seen instructions in several places that basically amount to :

1 Move the files into a library folder within your project diectory.
2 Add include 'projectName:folderName:libraryName to your settings.gradle file
3 Add compile project('folderName:libraryName') under dependencies to your project level build.gradle file

This works for me if I am importing a module... I think its a module. I'm still trying to figure out gradle. My library structure looks like this:

ProjectName
    Module1
        Classes
    Module2
        Classes2

If I import Module1 or Module2 separately in this fashion (where Module1 is my 'libraryName' in the instructions above), they will work. But if I try to import the whole project as one library I get errors.

What am I doing wrong? I would like to import the entire project as I would like it to have many modules. Is there another/better way to do this? What am I misunderstanding?

Update:
As per @ScottBarta 's instructions I merged settings.gradle files. This appears to have worked in my main project because the gradle sync completed properly but I'm not ready to run that project. So I made a tester project and imported the library using the same steps. This time I get the error:

Gradle 'Tester' project refresh failed:
Build script error, unsupported Gradle DSL method found. 'compile()'!

As near as I can tell, the files are equivalent in the two projects:

Main:

include ':Eed'
include ':Eed:libraries:Overt:Visible'
include ':Eed:libraries:Overt:Control'

Test:

include ':app'
include ':app:libraries:Overt:Visible'
include ':app:libraries:Overt:Control'

Main:

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project('libraries:Overt:Visible')
    compile project('libraries:Overt:Control')
}

Test

dependencies {
    compile 'com.android.support:appcompat-v7:19.+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    complie project('libraries:Overt:Visible')
    complie project('libraries:Overt:Control')

}

And here's a screenshot of the file structure in the Tester project. enter image description here

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
limeandcoconut
  • 415
  • 6
  • 21

1 Answers1

1

You can only have one settings.gradle file per project, so if you have a multi-module library with its own settings.gradle, you can't bring it in with a single include statement. You'll need to merge the library's settings.gradle into that of the project you're including it in.

When you do the merge, make sure that all the include statements in the merged settings.gradle have sensible paths that will locate their libraries relative to the project root. Also keep in mind that the paths specified in your settings.gradle file have to match what's in your dependencies block. So if you have this:

include ':app'
include ':app:libraries:Overt:Visible'
include ':app:libraries:Overt:Control'

You need this:

dependencies {
    compile 'com.android.support:appcompat-v7:19.+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':app:libraries:Overt:Visible')
    compile project(':app:libraries:Overt:Control')
}

In the build script you posted in your question, there's a typo: it says complie instead of compile. If that's a typo in your build script and not just a typo in your question here, that will be a problem.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Is that as simple as copy pasting them together? – limeandcoconut Apr 23 '14 at 16:44
  • 1
    You'll have to get the paths to each module correct, but yes, you pretty much just mash them together. Give it a shot, and if you run into problems you can't figure out, add more details to your question. – Scott Barta Apr 23 '14 at 16:46
  • I think you've solved my problem but I'm getting weird results. Updated the question. – limeandcoconut Apr 23 '14 at 18:41
  • Yep that typo was a problem. I'm sitting to far from the monitor to see the l vs i thing. Final question, why does the main project *seem* to run fine with `compile project('libraries:Overt:Control')` instead of `compile project('**:app:**libraries:Overt:Control')`? – limeandcoconut Apr 23 '14 at 18:55
  • 1
    Actually, I didn't think carefully about it. Your original statements `compile project('libraries:...')` would work fine. The leading colon in the spec acts like a leading delimiter in an OS path: it makes the path absolute instead of relative. You had set it up as a path relative to the "app" module root, which was okay. In my correction, I set it up as a path absolute to the project root, which is also okay. I often see problems where users have mismatches that don't work, so I reflexively suggested you correct it here. – Scott Barta Apr 23 '14 at 18:58
  • Did not know that. Obviously I need a crash course in gradle. :) – limeandcoconut Apr 23 '14 at 19:02
  • This has worked wonderfully but when I try to import the project now that its modules are interdependent I get an error. I made a new module dependent on another and now It won't import properly. I posted a full question [here](http://stackoverflow.com/questions/23399719/android-studio-library-project-dependencies) but I can't get any help. – limeandcoconut Apr 30 '14 at 23:49