Android studio add external project to build.gradle

后端 未结 6 2009
半阙折子戏
半阙折子戏 2020-11-22 16:16

I have a sample project, with the following setup:

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

Where the \'Te

6条回答
  •  臣服心动
    2020-11-22 16:52

    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.

提交回复
热议问题