简体   繁体   English

Android 自定义 class 依赖于aidl

[英]Android custom class dependency for aidl

So I have the following project structure:所以我有以下项目结构:

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

In the Central app, I define a service and along with it an AIDL interface.在 Central 应用程序中,我定义了一个服务以及一个 AIDL 接口。 In the AIDL, one of the functions returns a custom object (which extends Parcelable).在 AIDL 中,其中一个函数返回自定义 object(扩展 Parcelable)。 In the Client app, I put the exact same AIDL file (under the same package in the src/aidl directory).在客户端应用程序中,我放置了完全相同的 AIDL 文件(在src/aidl目录中的相同 package 下)。 I try to import the custom class by declaring a gradle dependency to the Central app.我尝试通过向 Central 应用程序声明 gradle 依赖项来导入自定义 class。

Here is the Client's settings.gralde这是客户端的settings.grade

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

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

The Client's app/build.gralde:客户的 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')
}

And the aidl file (present in both apps):和aidl文件(存在于两个应用程序中):

package com.Patel.Central;

import android.graphics.Bitmap;
parcelable Info;

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

And note that the Info class is defined in the package com.Patel.Central .请注意,信息 class 在 package com.Patel.Central中定义。

When I try to build the Client app, I get the following error:当我尝试构建客户端应用程序时,我收到以下错误:

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

In summary, the problem is that there is a custom class and I need to import from an app that is in another directory.总之,问题是有一个自定义 class ,我需要从另一个目录中的应用程序导入。

You need to define both files您需要定义这两个文件

Info.java

and

Info.aidl seprate aidl file for Info.java Info.aidl用于aidl的单独辅助文件

with same package name at both client and remote application.在客户端和远程应用程序中具有相同的 package 名称。

means package should have意味着 package 应该有

MusicCentral.aidl
Info.java
Info.aidl

in com.Patel.Central package.com.Patel.Central package 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM