Android Gradle Dependency

2019-05-11 22:43发布

问题:

I am using local aar files for one of our projects and have below Query. I have 2 libraries and 1 application.

2 libraries are: 1. TestLib2 2. TestLib1

1 Application is: 1. Test

I use a aar file created for TestLib2 and refer it using flatDir in TestLib1. I can access the functions present in TestLib2 without any problems.

Now I use a aar file created for TestLib1 and refer it using flatDir in Test. I can access only the functions present in TestLib1. For accessing TestLib2 i have to add it to Test application as one more Library.

So the dependency is like below:

Test
|_ TestLib1
   |_ TestLib2

Is the above possible in case of aar files?

Also in settings.gradle file for TestLib1 i mention to include

include ':app', ':testlib2-debug'

Where app refers to the TestLib1

The build.gradle file doesnt really have any flavors as such and i dont even have any restriction of using them as jar's since its containing only the java piece of code.

Any help on the same is much appreciated.

BR, Jayshil

Update 1: I tried below as well in build.gradle of TestLib1 and Test. Still no luck.

dependencies {
    compile (name:'testlib2-debug', ext:'aar') {
        transitive = true;
    }
}

And for Test App

compile (name:'testlib1-debug', ext:'aar')  {
        transitive = true;
}

回答1:

So i finally figured out a solution for this. It works for 2 level dependency mentioned above.

Create a jar file for Test Lib 2.

task clearJar(type: Delete) {
    delete 'build/outputs/loggingSDK.jar'
}

task makeJar(type: Copy) {
    from('build/intermediates/bundles/release/')
    into('build/outputs/')
    include('classes.jar')
    rename ('classes.jar', 'testlib2.jar')
}

makeJar.dependsOn(clearJar, build)

By using a the command

gradle makeJar

You would have got a testlib2.jar

Copy this into your TestLib1

Use command

gradle assemble

This would create debug and release version

Take the debug version and copy it in Test you would be able to call functions of TestLib1 which in turn calls function of TestLib2

Hope this may help someone looking for such solution