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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top