Search code examples
androidandroid-studiogradleandroid-gradle-pluginandroid-studio-import

Libraries in Android Studio - how not to produce a full copy


I am in the process of migrating from Eclipse to Studio.

I have one Eclipse project which is a personal library of various bits and pieces that I use in other projects. I have successfully imported that library project into Studio.

When I import a project that uses that library, the importer seems to copy the whole library project into the app project, source and all.

This, obviously, is not what is required. I've tried many things and wasted a LOT of time trying to overcome this. So...

Q: Within Studio / Gradle, how do I replicate the Eclipse functionality Project / Properties / Android / Add... to just reference the library project and not produce a stand-alone copy?


Solution

  • You can specify in your settings.gradle a relative path using the project().projectDir property, something like this:

     include ':lib'
     project(':lib').projectDir = new File('xxxxxx') // Relative file path from your settings.gradle
    

    In this way you can use the lib module inside a project without cloning the code.

    Example:

        Project0
        |--library
        |----build.gradle
        |build.gradle
        |settings.gradle
    
        Project1
        |--app
        |----build.gradle
        |build.gradle
        |settings.gradle
    

    in Project1/settings.gradle

    include ':library'
    project(':library').projectDir = new File(settingsDir, '../Project0/library')
    

    in Project1/app/build.gradle

    dependencies {
       compile project(':library')
    }