Android Aidl Compile Error: couldn't find import for class
Asked Answered
P

7

14

(I know there're multiple questions on stackoverflow and elsewhere (like google group) about adding parcelable for NetworkInfo but this is not about that.)

My work is under $(AOSP_ROOT)/device/ and involves multiple aidl files. one of it is like,

package com.example;

parcelable SomeRequest;

And another aidl is like,

package com.example;

import com.example.SomeRequest;
interface SomeService {
    SomeRequest getRequest();
}

And I'll get compile errors like,

device/somedevice/sdk/libs/aidl/com/example/SomeService.aidl:9: couldn't find import for class com.example.SomeRequest

I'm wondering it is the order of processing aidl files. My Android.mk looks like,

LOCAL_SRC_FILES := $(call all-java-files-under, src) $(call all-Iaidl-files-under, aidl)
LOCAL_AIDL_INCLUDES := $(call all-Iaidl-files-under, aidl)

This build error is introduced after I moved aidl files from src/ folder to aidl/ folder (for some reason I have to do so). It worked before but now even if I moved it back to src/ folder it doesn't work anymore. I tried to clean up $(AOSP_ROOT)/out/device/target but it's not helping.

Ideas?

Piggy answered 25/11, 2014 at 23:47 Comment(0)
P
11

Finally I got it resolved.

If you dig into /build/core/base_rules.mk, you'll find that LOCAL_AIDL_INCLUDES is actually the folders to be included during AIDL compiling phase in addition to the default folders like framework or so.

$(aidl_java_sources): PRIVATE_AIDL_FLAGS := -b $(addprefix -p,$(aidl_preprocess_import)) -I$(LOCAL_PATH) -I$(LOCAL_PATH)/src $(addprefix -I,$(LOCAL_AIDL_INCLUDES))

In this specific case, what you want is actually,

LOCAL_SRC_FILES := $(call all-java-files-under, src) $(call all-Iaidl-files-under, aidl)
LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)/aidl
Piggy answered 10/12, 2014 at 18:39 Comment(6)
What if my *.aidl file define in library, is not in current local path, just like ../LibraryProject/aidl, how to fix?Snowshoe
This 'all-Iaidl-files-under' function returned with path relative to $(LOCAL_PATH). You can specify library path directly from your root project. That is: LOCAL_AIDL_INCLUDES := LibraryProject/aidlRepair
@m0skit0, it's referring to e.g., android.googlesource.com/platform/build/+/master/core/…Widmer
This seems no longer valid. Running Android Studio 3.4, there si no base_rules.mk file, and no AIDL include path in any configuration.Rebut
It's moved to build/make/core/java.mk in AOSP12+Indene
It still gives the same error. ERROR: lineage-sdk/sdk/src/java/lineageos/app/IProfileManager.aidl: Couldn't find import for clas s android.app.NotificationGroupIndene
R
5

So I looked around - the solution is to put the AIDL files only into their own directory called aidl, which I think some others have suggested.

It turns out that Android Studio looks there, but doesn't quite know what to do about .aidl files put in the src directory.

I don't get it. I just don't get it. AIDL imports worked perfectly in Eclipse. Android Studio is not production ready. This is a major step backwards.

I'm was having trouble getting the AIDL compiler to find imports as well, and I'm thinking there has to be a better way than modifying base_rules.mk, which the platform manages.

Arg!

Retrad answered 16/10, 2015 at 0:47 Comment(2)
I'm using Android Studio 2.1 and did pretty much what you suggested: Put 2 .aidl files in aidl folder. However, what happened to me is that Android Studio prevented me to create the second .aidl (i.e., SomeRequest.aidl based on OP). It turns out you need to create this file a little bit brute-forcely: Create an .aidl with a random name and then change to the correct name. Otherwise Android Studio is not happy.Gerkman
I already have aidl files in a separate aidl/ folder. I even tried with subfolder in src/ and a root folder. Still same problem.Fenn
I
2

Ok, so I was struggling for days trying to figure out this same problem. Clean/Rebuilds didn't work for me. Eventually, I realized a really simple mistake I was doing in Android Studio - When I right-clicked my mouse and created a new "AIDL", I wasn't clicking in the appropriate folder as my original model classes.

For example, my model was in a java package called com.example.model, but I wasn't right-clicking in that folder when I created my AIDL. So when Android Studio generates the AIDL folder for me, it would reference the wrong folder, and I would end up with a compile error. I wish the android documentation made it clear that we needed to be right-clicking in the appropriate folder before generating the AIDL files. I know, it sounds silly, but I hope this answer applies to some of you!

Igloo answered 22/6, 2016 at 15:41 Comment(0)
P
2

just need name the .aidl file to be same as the .java file!

Pelayo answered 13/7, 2016 at 7:5 Comment(0)
R
2

As of now, currently accepted answer is no longer an option. No base_rules.mk file in Android Studio 3.4, none that I could find anyway.

To solve this, I took an example from IPackageStatsObserver which uses a parcelable PackageStats.

All imports must be declared in a .aidl file, some kind of .h if we were in C.

Here is the content of the PackageStats.aidl file, reflecting the existence of a class with the same name (on the java side):

package android.content.pm;

parcelable PackageStats;

So I declared all my parcelable in .aidl, for every matching .java I need to use in my aidl interface, and voila, it compiles.

Got to see if it actually works now ;)

Rebut answered 7/6, 2019 at 9:24 Comment(1)
Is there a verbose debug.log file for aidl.exe ?Trainman
S
1

To resolve couldn't find import for class in parcelable class, put the aild file and java file in correct package and folder

// MyObject.aidl
package com.example.androidaidlserver.model;
parcelable MyObject;
// ILocalLogService.aidl
package com.example.androidaidlserver;

import com.example.androidaidlserver.model.MyObject;

interface ILocalLogService {

    void writeObject(in MyObject log);
}
// MyObject.java
package com.example.androidaidlserver.model;

import android.os.Parcel;
import android.os.Parcelable;

public class MyObject implements Parcelable {
...
Shows answered 26/1, 2021 at 10:16 Comment(1)
Had no idea I need to create package hierarchy for aidl files too. Thanks, man, your screenshot gave me a clue!Septa
R
1

For anyone who's still encountering this issue as of 2023, just put your AIDL files directly inside the aidl folder, without any package hierarchy, but do not change the package declaration inside the aidl file itself.

Make sure each AIDL file has its counterpart java file, for example, if you have a LicenseValidator.aidl, you have to have a LicenseValidator.java, because they work together.

If nothing worked, try to have your code compiled WITHOUT the aidl files, that is, anything depending on it or whatever. There is this weird bug in Android Studio Iguana that won't let you detect aidl files no matter what, until at least a successful build happens...?

Receptor answered 5/9, 2023 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.