Programing

Android Studio에서 build.gradle에 외부 프로젝트 추가

crosscheck 2020. 6. 24. 07:39
반응형

Android Studio에서 build.gradle에 외부 프로젝트 추가


다음 설정으로 샘플 프로젝트가 있습니다.

/root
  + Pure Java Lib
  + Android Test Lib
  + Android Test Project

' Test Project '가 ' Test Lib '에 의존하고 마지막이 ' Pure Java Lib ' 에 의존하는 경우 프로젝트를 컴파일 하고이 설정을 시작하면 제대로 작동합니다.

이제 이전 Eclipse 작업 공간을 가져오고 Android 스튜디오에서 작업하는 것에 대해 생각하고 있습니다. 문제는 프로젝트 설정이 다르기 때문에이 방법으로 유지하고 싶습니다.

예를 들어 이전 예제를 사용하는 경우 :

/root
  + Android Test Lib
  + Android Test Project

/Some Other folder (another repository for example)
  + Pure Java Lib

많은 구성을 시도했지만 부모 폴더 범위 밖에서 프로젝트를 참조하는 방법을 찾지 못했습니다 ( 이 예에서는 ' root ').

많은 플랫폼 / 모듈에서 '..'을 사용하여 폴더로 이동할 수는 있지만 이것은 나에게 효과적이지 않습니다. 아마 잘못 사용했습니다.

누구나 Gradle을 사용하여 이것이 어떻게 달성 될 수 있는지 알고 있습니까?

최신 정보

좀 더 일반적으로 노력하겠습니다.

/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

나는의 모듈 1을 사용하고자하는 프로젝트 B , 프로젝트 A를에 .


업데이트 : 09-03-19

나는 이것을보고 지금 업데이트해야한다. 거의 6 년이 지난 지금, 나는 현명하다. 그리고 나는 문제가 "진리의 근원"이라는 개념을 오해하고 있다고 말할 수있다.

라이브러리에 대한 하나의 참조를 갖는 것은 개념을 갖는 것이 좋지만 "진리의 근원"처럼 보일 수 있지만, 실제 "진리의 근원"은 각 프로젝트가 해당 라이브러리에서 사용하는 코드의 버전 일 것입니다. 라이브러리 자체에는 버전이 있습니다. 많은 버전에서 "진실의 근원"은 라이브러리를 사용하는 프로젝트와 관련이 있습니다.

올바른 방법은 대부분의 개발자가 싫어하는 것을 사용하는 것이며 git 서브 모듈이며, 각 프로젝트의 소스를 복제하면 각 프로젝트가 다른 버전의 코드를 사용할 가능성이 높습니다.

그러나 모든 프로젝트가 모든 최신 라이브러리 버전을 사용하도록해야합니다. 이는 그 자체로 어려운 문제입니다.

이것이 라이브러리 소스로 프로젝트를 개발하는 올바른 방법 인 이유는이 스케일이기 때문입니다. 각각 자체 라이브러리 구성으로 수백 개의 프로젝트를 가질 수 있습니다.


일부 다른 폴더가 gradle 프로젝트라고 가정하면 settings.gradle 파일에 다음과 같은 것을 추가 할 수 있습니다.

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

파일 설정 을 넣어야 합니다.

include ':module2'
project(':module2').projectDir = new File(settingsDir, '../Project 2/Module2')

그런 다음 종속성 트리 builde.gradle (모듈 : app)다음 줄에 추가해야합니다.

compile project(':module2')

또는 프로젝트 구조 > > 종속성 으로 이동하여 추가를 클릭 하고 3 모듈 종속성 을 선택한 다음 모듈을 선택하십시오.


Gradle 1.10을 사용하면 (이것이 유효한 다른 버전을 모릅니다) 이것은 http://forums.gradle.org/gradle/topics/reference_external_project_as_dependancy에 제공된 응답을 기반으로 작성된 것입니다.

API 라이브러리 프로젝트, 공통 라이브러리 프로젝트 및 기본 앱 프로젝트가 있습니다. 각각은 독립형 개발 프로젝트이며 두 라이브러리는 여러 앱간에 공유되도록되어 있습니다.

공통 프로젝트의 settings.gradle에서 :

def apiLibDir = file('../android-api/android-api-lib')
def rootProjectDescriptor = settings.rootProject
settings.createProjectDescriptor(rootProjectDescriptor, 'android-api-lib', apiLibDir)
include ':android-api-lib'

그런 다음 기본 앱 프로젝트 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'

각각의 build.gradle 파일에서 다른 프로젝트 종속성과 마찬가지로 settings.createProjectDescriptor에서 지정한 이름으로 파일을 참조하십시오.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':android-api-lib')
    compile project(':android-common-lib')
}

이것은 작동하는 것 같습니다. API 라이브러리를 정의하는 여러 DEX 파일에 오류가 발생하지 않았습니다. 모두 동일한 빌드 프로세스의 일부 였고 Gradle이 모든 것을 알아낼만큼 똑똑했기 때문입니다.


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.


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.


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')

참고URL : https://stackoverflow.com/questions/17479076/android-studio-add-external-project-to-build-gradle

반응형