Search code examples
androidandroid-studioaidl

Android Studio: How to import AIDL file from a different Android Studio project?


One Android Studio project contains a no-activity service, to be communicated with via IMyAidlInterface (belonging to package com.example.tutorialspoint7.noactivity).

Another Android Studio project contains an activity-only application, for the sole purpose of testing the aforemention service (assume a different package, e.g. com.example.tutorialspoint7.aidlactivity)

Without any special configuration, the aidlactivity project does not know about the noactivity project and thus, any reference to IMyAidlInterface in aidlactivity gets "Cannot resolve symbol IMyAidlInterface".

I searched for a way to let the aidlactivity project to know about that AIDL file, but all "solutions" I found were actually workarounds duplicating the file and adding it as in this example: https://stackoverflow.com/a/16906355/5556250

So, I turned into manually tweaking the app's build.gradle by adding the following into it:

android {
    compileSdkVersion 24
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.tutorialspoint7.aidlactivity"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets {
        main.java.srcDirs += '../../NoActivity/app/src/main/aidl/com/example/tutorialspoint7/noactivity'
    }
}

I then synced it and, how wonderful, it is now part of the aidlactivity project:

enter image description here

But... AIDLActivity still gets "Cannot resolve symbol IMyAidlInterface".

What do I need to do now to get it to recognize the file?


Solution

  • Problem "worked around". Here is how:

    1. I undid the manual addition into the app's build.gradle:

    sourceSets { main.java.srcDirs += '../../NoActivity/app/src/main/aidl/com/example/tutorialspoint7/noactivity' }

    1. I manually created the folder ..\AIDLActivity\app\src\main\aidl\com\example\tutorialspoint7\noactivity, and then copied over to there the original IMyAidlInterface.aidl file from the 'NoActivity' service project.

    2. Adding import com.example.tutorialspoint7.noactivity; in AIDLActivity.java doesn't work!!! so... I worked around this by simply referencing IMyAidlInterface using its fully qualified name:

    enter image description here

    1. Gradle sync the projects

    2. The AIDLActivity project now builds without any errors. :-)

    Credits goes to this Jun 18, 2013 post by dominik2...@gmail.com: https://code.google.com/p/android/issues/detail?id=56755#c1


    Update: I managed to avoid the explicit fully qualified name ugliness by simply importing the specific interface, not the package (duh!):

    enter image description here

    All is well now but... Why do I have to keep copying the interface file whenever it changes in the original service, instead of just referencing it? I wish there were a way to reference (instead of copy) an AIDL file in Android Studio (version 2.2.3).