Saturday, March 17, 2018

Include library module from external folder for android project

Usually the android library module is placed under the project root folder and can be easily reference from settings.gradle file using folder name. However, it requires some extra settings to use an library module from external folder.

Assuming we start with below folder structure which haves three android projects, each project has an app module and library module, the goal is to call method from mylib1 and mylib2 from the app module and mylib3 module in myapp3 project. Note mylib1 and mylib2 is not under the root folder of myapp3 project
myapp1
      app1
      mylib1 (mylib1 is a jar library, which contains a method calls myMethod1)
myapp2
      app2
      mylib2 (mylib2 is android aar library module, which contains a method calls myMethod2)
myapp3 
     app3
     mylib3 (Android library module)

In order to add modules from external folder, the app3's project settings.gradle file needs to be updated either with relative path or absolute path as shown below
include ':app', ':mylib3'
include ':mylib2'
    project(':mylib2').projectDir = new File('../myapp2/mylib2')
include ':mylib1'
    project(':mylib1').projectDir = new File('/Users/i826633/Documents/temp/app1/mylib1')

Once the external modules are added into the project app1, then to use the modules in app1's app module, the app1's module build.gradle needs to add the external modules in its dependencies section as below
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile project(path: ':mylib3')
    compile project(path: ':mylib2')
    compile project(path: ':mylib1')
}

Similarly, to call the methods in mylib1 and mylib2 from mylib3, the build.gradle of mylib3 needs to be updated to include the mylib1 and mylib2 as dependencies
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile project(path: ':mylib2')
    compile project(path: ':mylib1')
}

By the way, if a project includes many library modules, adding the modules one by one to another project's settings.gradle file would be too troublesome. In that case, all modules in the library project can be imported to another project by using includeBuild, which just imports all library modules included in the settings.gradle to the new project as shown below
include ':myapp1', ':mylib2'
includeBuild '/Users/i826633/Documents/temp/app1' 

When includeBuild is used, be sure the distributionUrl in project's gradle-wrapple.properties is gradle-4.5.1 or above
distributionUrl=https\://services.gradle.org/distributions/gradle-4.5.1-all.zip

No comments:

Post a Comment