首页 > 解决方案 > 用于aidl的Android自定义类依赖

问题描述

所以我有以下项目结构:

.
├── Central
│   ├── app
│   │   ├── build.gradle
│   │   └── src
│   ├── build.gradle
│   ├── gradle.properties
│   └── settings.gradle
└── Client
    ├── app
    │   ├── build.gradle
    │   └── src
    ├── build.gradle
    ├── gradle.properties
    └── settings.gradle

在 Central 应用程序中,我定义了一个服务以及一个 AIDL 接口。在 AIDL 中,其中一个函数返回一个自定义对象(扩展 Parcelable)。在客户端应用程序中,我放置了完全相同的 AIDL 文件(在src/aidl目录中的同一包下)。我尝试通过声明对 Central 应用程序的 gradle 依赖项来导入自定义类。

这是客户端的settings.grade

rootProject.name='Client'
include ':app'

include ":Central"
project(":Central").projectDir = file('../Central/app')

客户的 app/build.grade:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.Patel.Cli3n5"
        minSdkVersion 28
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation  project(path: ':Central', configuration: 'default')
}

和aidl文件(存在于两个应用程序中):

package com.Patel.Central;

import android.graphics.Bitmap;
parcelable Info;

interface MusicCentral {
     List<Info> getAllSongsInfo();
     Bitmap getSongImage(int songNum);
}

请注意,Info 类是在包中定义的com.Patel.Central

当我尝试构建客户端应用程序时,我收到以下错误:

error: cannot find symbol
    @Override public java.util.List<com.Patel.Central.Info> getAllSongsInfo() throws android.os.RemoteException

总之,问题在于有一个自定义类,我需要从另一个目录中的应用程序导入。

标签: javaandroidgradleaidl

解决方案


您需要定义这两个文件

Info.java

Info.aidlInfo.javaaidl的单独文件

在客户端和远程应用程序中具有相同的包名称。

意味着包应该有

MusicCentral.aidl
Info.java
Info.aidl

com.Patel.Central包装中。


推荐阅读