简体   繁体   English

Android Studio Gradle外部库项目

[英]Android Studio Gradle External Library Project

Ok, I realize that Gradle and Android Studio seem to think that all Library Applications are built for one project and one project only, but that is not the case. 好吧,我意识到Gradle和Android Studio似乎认为所有的图书馆应用程序都是为一个项目和一个项目而构建的,但事实并非如此。 I have many shared Library Applications with common purposes that are shared throughout the organization. 我有许多共享的库应用程序,其共同目的是在整个组织中共享。 Gradle does not seem to be very accomodating to this desired solution. Gradle似乎不太适应这种理想的解决方案。 Can someone offer any insight? 有人可以提供任何见解吗?

My current Structure at a very rudimentary level is like this: 我目前的结构处于一个非常基本的层面是这样的:

|--Directory
|  |--PROJECT A
|     |---Module 1
|  |--Project B
|     |---Module 2
|  |--Project c
|     |--Module 3

///////////////////////////////////////////// My Current dependency structure is like this: ///////////////////////////////////////////// /////////////////////////////////////////////我当前的依赖结构是像这样: /////////////////////////////////////////////

Project A : (FYI, Builds Just Fine) 项目A :(仅供参考,建造得很好)

Project A's settings.gradle 项目A的settings.gradle

include ':Module 1', ':Module 2'
project(':Module 2').projectDir = new File('../Project B/Module 2')

Module 1's build.gradle 第1单元的build.gradle

dependencies {
    compile project(':Module 2')
}

Project C: (FYI, BROKEN) 项目C:(仅供参考,BROKEN)

Project C's settings.gradle Project C的settings.gradle

include ':Module 3', ':Module 1'
project(':Module 1').projectDir = new File('../Project A/Module 1')

Module 3's build.gradle 模块3的build.gradle

dependencies {
    compile project(':Module 1')
}

Breaks: Cannot resolve Module 2 inside of Module 1's build.gradle file. 中断:无法解析模块1的build.gradle文件中的模块2。

This is because the directory structure for Module 2 is established inside Project A's settings.gradle so Project B has no idea where to render this from. 这是因为模块2的目录结构是在Project A的settings.gradle中建立的,因此Project B不知道从哪里渲染它。

I understand that I can add 我明白我可以添加

project(':Module 2').projectDir = new File('../Project B/Module 2')

to Project C and everything will work just fine. 到项目C,一切都会正常工作。 However Project C doesn't use or know about Module 2. I want other developers to have the freedom to use my common shared library project without having to dig in and see what library projects I used and include those in their settings as well. 但是,Project C不使用或不了解模块2.我希望其他开发人员可以自由使用我的公共共享库项目,而无需深入了解我使用的库项目,并将其包含在他们的设置中。 How can I specify my own dependency directory structure in the build.gradle instead of the settings.gradle to make it accessible to all that use it? 如何在build.gradle中指定我自己的依赖项目录结构而不是settings.gradle,以使所有使用它的人都可以访问它?

On a second note, but similar topic. 在第二个注释,但类似的主题。 I'm having the exact same issue with JAR files. 我对JAR文件有完全相同的问题。 If i specify a REPO in a Library Project's build.gradle like: myRepo1 and have a myJar1. 如果我在库项目的build.gradle中指定一个REPO,如:myRepo1并且有一个myJar1。 Then when that library project is used in a parent project that doesn't define the repo that contains the jar in the library projects dependeny section, it fails to resolve the jar file from the library project when compile project(':libproject') is used. 然后,当该库项目用于未在库项目依赖项部分中定义包含jar的repo的父项目时,当编译项目(':libproject')时,它无法从库项目中解析jar文件用过的。 I have to duplicate the repo pointers in the parent's build.gradle file as well so that the libproject will build from the parent app. 我必须在父的build.gradle文件中复制repo指针,以便libproject将从父应用程序构建。 Any help on this one would be appreciated as well. 对此的任何帮助也将受到赞赏。 As not every repo is used in every app so this can become redundant. 因为不是每个应用程序都在每个应用程序中使用,所以这可能变得多余。

Ok this is a really old post, but still gets traction so let me update 3 years later since I originally wrote it lol. 好吧这是一个非常古老的帖子,但仍然有牵引力所以让我更新3年后,因为我最初写它大声笑。

Shout out to CommonWare who had the right best practice idea right from the start, but didn't provide an answer to mark up. 向一位从一开始就拥有正确最佳实践想法的CommonWare致敬,但没有给出标记的答案。

Let me start by saying that using project references like I was doing above should be limited to development stages only and should only be if the library project is also in development stage at the same time as the main project. 首先让我说,像我上面那样使用项目引用应该仅限于开发阶段,并且只应该在库项目与主项目同时处于开发阶段时才应该这样做。 Otherwise a dependency management server like Nexus, Apache Archiva, or S3 with Maven directory structure or equivalent would be preferred. 否则,首选依赖管理服务器,如Nexus,Apache Archiva或具有Maven目录结构或等效的S3。 I have learned many ways to manage dependencies since this, including transitive dependency management. 我已经学习了许多管理依赖关系的方法,包括传递依赖关系管理。

My preferred method is to deploy artifacts with POM files to Apache Archiva and then use these dependencies within the parent project instead of using relative paths to reference code projects now. 我首选的方法是将带有POM文件的工件部署到Apache Archiva,然后在父项目中使用这些依赖项,而不是使用相对路径来引用代码项目。 This is the first choice. 这是第一选择。

However, if you are too new to dependency management and choose not to have a server for this purpose, you may package your AAR files or JAR files and put them in one centralized repo like artifact_repo and have everyone include that repo at the same folder structure and reference them relatively, but this is not good practice so I would steer clear if you can. 但是,如果您对依赖项管理过于陌生并选择不为此目的安装服务器,则可以将AAR文件或JAR文件打包并将它们放在一个集中的repo中,例如artifact_repo,并让每个人都在同一个文件夹结构中包含该repo。并且相对地引用它们,但这不是好的做法,所以如果可以,我会引导清楚。

You can also take the artifacts and nest them in you libs directory and bring them in that way if you would like, but it becomes more of a manual update process which some people like and others do not. 您也可以将这些工件并将它们嵌套在libs目录中,如果您愿意,可以将它们以这种方式引入,但它更像是一个人们喜欢的手动更新过程,而其他人则不喜欢。

Now this opens a whole different set of issues that you need to handle. 现在,这将打开您需要处理的一整套不同的问题。

Transitive Dependencies and Child Repo pointers. 传递依赖和儿童回购指针。 For example, if you wrapped your own Crash Reporting Library around Fabric or Hockey or other hoping to make it easy to trade libraries later, then you have found that the repo pointer has to live in the parent build.gradle files or the transitive dependencies are not found. 例如,如果您围绕Fabric或Hockey包装自己的Crash Reporting Library或希望以后可以轻松交易库,那么您发现repo指针必须存在于父build.gradle文件或传递依赖项中。未找到。

You could of course use one of those hacky Fat_AAR or Fat_JAR scripts that works "sometimes" until updated gradle then they break again until someone hacks it back together, but this is also poor practice as you are creating potential mismatch dependencies on support or other important child libraries and the "exclude transitives" only works if you are using pom files to control the transitives and not making the AAR or JAR file fat. 你当然可以使用那些hacky Fat_AAR或Fat_JAR脚本中的一个“有时”工作,直到更新的gradle然后它们再次中断,直到有人将它重新组合在一起,但这也是一种糟糕的做法,因为你在支持或其他重要方面创建了潜在的不匹配依赖性子库和“exclude transitives”仅在您使用pom文件来控制传递而不使AAR或JAR文件变胖时才有效。 So you are limiting your ability to control the dependencies. 所以你限制了你控制依赖的能力。

So what i have finally come to terms with is that transitive dependencies should be managed through POM files to allow excluding or including without nesting into children libraries. 所以我最终接受的是,传递依赖应该通过POM文件来管理,以允许排除或包括不嵌套到子库中。 Also libraries that require repo pointers inside of them, should probably not exist as they require parent boiler plate, introduce room for human error and typically don't save much time on wrapping analytics or crash libraries for example or you start getting into json configs that need to live in parent files for PUSH or other reasons. 此外,在它们内部需要repo指针的库可能不存在,因为它们需要父样板,为人为错误引入空间,并且通常不会节省很多时间来包装分析或崩溃库,或者你开始进入json配置需要为PUSH或其他原因生活在父文件中。 Just avoid it. 避免它。

So long story short lol. 这么长的故事很短暂的哈哈。 Stick to dependency management tools they way they were intended to be used and you will be fine. 坚持使用他们想要使用的依赖管理工具,你会没事的。 It is when you are new to it or start getting hacky that you run into ugly code and ugly problems. 当你刚接触它或者开始变得hacky时,你会遇到丑陋的代码和丑陋的问题。 Hope this encourages someone to do it the right way :) 希望这能鼓励某人以正确的方式去做:)

One last thing :). 最后一件事 :)。 I have recently started writing Gradle Plugins to manage my versions and dependencies as a separate file so that I can use intellisense to pull in dependencies and make sure all support, gms, and tool versions are the same across all projects. 我最近开始编写Gradle插件来管理我的版本和依赖项作为一个单独的文件,这样我就可以使用intellisense来提取依赖关系,并确保所有项目中的所有支持,gms和工具版本都相同。 You can even copy down live templates with your plugin to enable intellisense for Gradle to work with your stuff. 您甚至可以使用插件复制实时模板,以便为Gradle启用intellisense以处理您的内容。 It's not too bad to do. 做起来并不算太糟糕。 Best of luck and happy Gradling :). 祝你好运和快乐的Gradling :)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有Git的Android Studio Gradle外部目录/库 - Android Studio Gradle external directory / library with Git 如何通过Gradle / Android Studio中的外部库项目修改导入的模块并修改代码 - How to update imported modules with code modification from the their external library project in Gradle/Android Studio Android Gradle将外部库和嵌套外部库添加到项目中 - Android Gradle adding external library and nested external libraries to a project H如何将外部库添加到Android Studio项目中? - HHow to add an external library to an Android Studio project? 将外部库添加到android studio 0.5.1项目中 - Add external library into android studio 0.5.1 project 无法将外部库添加到android studio项目 - Cannot add external library to android studio project 如何使用 Android Studio + Gradle + NDK 构建外部 C++ 库? - How to build external C++ library with Android Studio + Gradle + NDK? 通过gradle下载的外部库在哪里存储在Android项目中? - Where does the external library, downloaded by gradle, get stored in an Android project? 如何在我的Android Studio项目的SVN中存储Gradle的外部依赖关系? - How to store the external dependencies of Gradle in SVN for my Android Studio Project? Android studio 添加外部项目到 build.gradle - Android studio add external project to build.gradle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM