Use AIDL interfaces across module/subprojects in Gradle
Asked Answered
P

3

8

I have a project consisting of multiple gradle modules (plugin com.android.library) referencing each other (tree, not flat). I use AIDL intensively and thus it happens that I reference AIDL interfaces from one module (modA) in an AIDL interface in another module (modB).

modA/src/main/aidl/modA/foo.aidl:

package modA; 
interface foo {
    void modAcall();
}

modB/src/main/aidl/modB/bar.aidl:

package modB;
import modA.foo;
interface bar {
    foo modBcall();
}

modB/build.gradle:

dependencies {
    compile project(':modA')
}

I can reference parcelables of modA in AIDL files in modB and I can reference interfaces of modA in java code in modB, but not in AIDL in modB. Instead the error message couldn't find import for class is shown by the aidl tool.

I noticed that .aar-files also do not contain information on the aidl interfaces of the library, only the parcelables are listed. I guess this has the same reason as the problem described above.

As I already have the aidl files in one place I don't want to copy them over to another subproject, because changes have to be manually copied over in that case. I also don't like the idea of using symlinks for this, because it feels wrong to modify the src directory of modB to include something from modA.

  • Is there a way to include aidl interfaces in aar so it can be used by other modules?
  • Is it possible to extend the aidl include path in Gradle to use the aidl files of the dependency module (for subprojects, it should be possible, because the aidl files are present on filesystem) without "compiling" them again (the class files of the generated java interface/classes are already included in the aar/module import)?
Piddling answered 4/8, 2016 at 10:45 Comment(0)
P
7

I found a solution. There is an apparently undocumented feature in Android gradle build tools to include arbitrary aidl files into the output .aar file. This makes it possible to reference them in other subprojects.

// In modA build.gradle
android {
    aidlPackageWhiteList "src/main/aidl/foo.aidl"
}

This is far from ideal, but at least usable to solve my problem.

EDIT: Clarify that this would be in modA (the library), not modB (which is a library according to the question, but not in all possible cases).

Piddling answered 4/8, 2016 at 12:3 Comment(2)
I tried this but I get that aidlPackageWhiteList is not supported :(Jannelle
@Jannelle It appears to have been renamed to aidlPackagedList.Circumrotate
B
2

I try 'aidlPackageWhiteList' , it shows me "aidlPackageWhiteList is not supported" , so I try another way below, it works for me.

  // in modB build.gradle
  android {
        sourceSets {
            main {
                aidl.srcDirs += '../src/main/aidl'    // locate your modA aidl directory 
            }
        }
    }
Benoite answered 13/6, 2019 at 12:39 Comment(2)
I am still using aidlPackageWhiteList in my Android build (using build tools 28.0.3). Important to note is that aidlPackageWhiteList has to be added to the library (modA). Android application packages (build to .apk, not libraries build to .aar) do not support aidlPackageWhiteList. The advantage of aidlPackageWhiteList is that is also works when libraries are shipped through maven, where your approach won't work, as the source is not available.Piddling
I really like this answer. Taking it a step further, you can define a top level AIDL source folder at the project root, outside either of the modules so that no module is the owner. Then each module's build.gradle file can have this: sourceSets { main { aidl.srcDirs += '../aidl' } } I verified that both library modules AAR files contain the generated java files from the AIDL file(s).Uhf
C
0

my answer is same as @Marvin W but with diff path parms

// In modA build.gradle

aidlPackagedList "com/xxx/xxx/xxx.aidl"
Cocksure answered 30/1, 2023 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.