可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a sample project, with the following setup:
/root
+ Pure Java Lib
+ Android Test Lib
+ Android Test Project
Where the \'Test Project\' is dependent on the \'Test Lib\', and the last depends on the \'Pure Java Lib\'
Compiling the project and launching this setup works just fine.
I\'m now thinking about importing my previous Eclipse workspace and work with Android studio, the problem is the project setup is different, and I would like to keep it this way.
for example if to use the previous example:
/root
+ Android Test Lib
+ Android Test Project
/Some Other folder (another repository for example)
+ Pure Java Lib
I\'ve tried many configurations, but I didn\'t find a way to reference a project outside the scope of the parent folder (\'root\' in the example case).
In many platforms/modules you can use the \'..\' to move up in the folders but this didn\'t work for me, perhaps I\'ve used it wrong.
Does anyone know how this can be achieved with Gradle?
UPDATE
I\'ll try to be more generic:
/C:/
/Project A
+ Module 1 - Pure Java
+ Module 2 - Android Test Lib
+ Module 3 - Android Test Project
/Project B
+ Module 1 - Pure Java
+ Module 2 - Pure Java
+ Module 3 - Pure Java
I would like to use Module 1 of project B, in project A.
回答1:
Assuming that Some Other Folder is a gradle project you could add something like the following to your settings.gradle file:
include \':module1\'
project(\':module1\').projectDir = new File(settingsDir, \'../Project B/Module 1\')
回答2:
You have to put in your file settings.gradle this lines:
include \':module2\'
project(\':module2\').projectDir = new File(settingsDir, \'../Project 2/Module2\')
Then you have to add in your builde.gradle (Module: app) in the dependencies tree, this line:
compile project(\':module2\')
or go into the Project Structure > app > Dependencies, click on Add, choose 3 Module Dependencies and select your module
回答3:
With Gradle 1.10 (don\'t know what other versions this will be valid for) this is what I came up with based on a response given here http://forums.gradle.org/gradle/topics/reference_external_project_as_dependancy
I have an api library project, a common library project and the main app project. Each is a stand-alone development project and the two libraries are meant to be shared between multiple apps.
In settings.gradle for the common project:
def apiLibDir = file(\'../android-api/android-api-lib\')
def rootProjectDescriptor = settings.rootProject
settings.createProjectDescriptor(rootProjectDescriptor, \'android-api-lib\', apiLibDir)
include \':android-api-lib\'
Then in the main app project settings.gradle:
def apiLibDir = file(\'../android-libs/android-api/android-api-lib\')
def rootProjectDescriptor = settings.rootProject
settings.createProjectDescriptor(rootProjectDescriptor, \'android-api-lib\', apiLibDir)
include \':android-api-lib\'
def commonLibDir = file(\'../android-libs/android-common/android-common-lib\')
settings.createProjectDescriptor(rootProjectDescriptor, \'android-common-lib\', commonLibDir)
include \':android-common-lib\'
In each of the respective build.gradle files you just reference them by the name you gave them in the settings.createProjectDescriptor like you would any other project dependancy:
dependencies {
compile fileTree(dir: \'libs\', include: [\'*.jar\'])
compile project(\':android-api-lib\')
compile project(\':android-common-lib\')
}
This seems to work. It didn\'t even throw an error for multiple DEX files defining the api library, I suspect because it was all part of the same build process and Gradle was smart enough to figure it all out.
回答4:
Right click on project - Select \"Open Module Settings\" - Select \"Modules\" in left pane - Click on \"+\" symbol on top - Choose \"Import Module\".
After importing Module. You need to add it as a dependency for your current project.
Keep \"Modules\" Selected in left pane and click on your project - Now Go in dependencies tab and click on \"+\" symbol that is located at bottom - Choose third option \"Module Dependencies\" and if you have imported your project correctly, it will show you the all available module that can be added as a dependency to your current project.
回答5:
I re-ask this question in a way which entails the original posters\' intentions at How do we reference custom Android and Java Libraries living outside the directory of our Main Android Project?
There I answer my own question. At core my answer uses @Ethan\'s (the author of the chosen answer in the current thread) gradle coding insight. But my answer also navigates some other gotchas and provides a detailed step by step example.
回答6:
As Ethan said, if you add this to your settings.gradle, it will add an external project to Android Studio (in this example, it\'s in the parent folder):
project(\':../ProjectB/:module1\')
Then, to add it as a dependency of one of your projects, just add it in the build.gradle of that project as another dependency like this (you can also do it graphically as in here):
compile project(\':../ProjectB/:module1\')