简体   繁体   English

包括整个项目作为库android studio

[英]Including entire project as library android studio

I am trying to import my own library into one of my apps in Android Studio. 我正在尝试将自己的库导入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. 1将文件移到项目目录内的库文件夹中。
2 Add include 'projectName:folderName:libraryName to your settings.gradle file 2include 'projectName:folderName:libraryName添加到settings.gradle文件中
3 Add compile project('folderName:libraryName') under dependencies to your project level build.gradle file 3将依赖项下的compile project('folderName:libraryName')添加到项目级别的build.gradle文件中

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. 如果我以这种方式分别导入Module1或Module2(在上述说明中,其中Module1是我的“ libraryName”,则它们将起作用)。 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. 按照@ScottBarta的说明,我合并了settings.gradle文件。 This appears to have worked in my main project because the gradle sync completed properly but I'm not ready to run that project. 这似乎在我的主项目中起作用了,因为gradle同步已正确完成,但是我还没有准备好运行该项目。 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. 这是Tester项目中文件结构的屏幕截图 在此处输入图片说明

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. 每个项目只能有一个settings.gradle文件,因此,如果您有一个具有自己的settings.gradle的多模块库,则不能使用单个include语句将其引入。 You'll need to merge the library's settings.gradle into that of the project you're including it in. 您需要将库的settings.gradle合并到包含它的项目中。

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. 合并时,请确保合并后settings.gradle中的所有include语句都具有明智的路径,这些路径将相对于项目根目录定位其库。 Also keep in mind that the paths specified in your settings.gradle file have to match what's in your dependencies block. 还请记住,在settings.gradle文件中指定的路径必须与dependencies块中的路径匹配。 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 . 您在问题中发布的构建脚本中有一个错字:它说complie而不是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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM