Android Studio:如何从不同的 Android Studio 项目导入 AIDL 文件?

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

一个 Android Studio 项目包含一个 no-activity 服务,通过 IMyAidlInterface 进行通信(属于包 com.example.tutorialspoint7.noactivity) .

另一个 Android Studio 项目包含一个仅 activity 的应用程序,其唯一目的是测试上述服务(假设一个不同的包,例如 com.example.tutorialspoint7.aidlactivity)

没有任何特殊配置,aidlactivity 项目不知道 noactivity 项目,因此,在 aidlactivity 中对 IMyAidlInterface 的任何引用都会得到 "Cannot resolve symbol IMyAidlInterface".

我搜索了一种方法让 aidlactivity 项目了解该 AIDL 文件,但我发现的所有 "solutions" 实际上都是复制文件并添加它的解决方法,如本例所示:

因此,我通过向其中添加以下内容来手动调整应用程序的 build.gradle:

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'
    }
}

然后我同步了它,太棒了,它现在是 aidlactivity 项目的一部分:

但是...AIDLActivity 仍然得到 "Cannot resolve symbol IMyAidlInterface".

我现在需要做什么才能让它识别文件?

问题"worked around"。方法如下:

  1. 取消了手动添加到应用程序的build.gradle:

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

  1. 手动创建了文件夹..\AIDLActivity\app\src\main\aidl\com\example\tutorialspoint7\noactivity,然后复制到原来的IMyAidlInterface.aidl 来自 'NoActivity' 服务项目的文件。

  2. 在 AIDLActivity.java 中添加 import com.example.tutorialspoint7.noactivity; 不起作用!!! 所以...我通过简单地引用解决了这个问题IMyAidlInterface 使用其 完全限定的 名称:

  1. Gradle 同步项目

  2. AIDLActivity 项目现在可以毫无错误地构建。 :-)

鸣谢 2013 年 6 月 18 日 post by dominik2...@gmail.com:https://code.google.com/p/android/issues/detail?id=56755#c1


更新:我通过简单地导入特定接口而不是包(duh!)设法避免了显式完全限定名称的丑陋:

现在一切都很好但是...为什么我必须在原始服务中更改接口文件时一直复制它,而不是仅仅引用它?我希望有一种方法可以在 Android Studio(版本 2.2.3)中引用(而不是复制)AIDL 文件。