在Eclipse编译aidl文件中出现couldn't find import for class原因


http://blog.csdn.net/ghd2000/article/details/6082339 


最近正在研究aidl,出现了以下错误: couldn't find import for class 无法导入类!

 

IMyService.aidl  如图:

工程目录:

Student.java

 private int age;
    private String name;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
//    public static final Parcelable.Creator<Student> CREATOR = new Creator<Student>() {
//
//        @Override
//        public Student[] newArray(int size) {
//            // TODO Auto-generated method stub
//            return new Student[size];
//        }
//
//        @Override
//        public Student createFromParcel(Parcel source) {
//            // TODO Auto-generated method stub
//            return new Student(source);
//        }
//
//		
//    };
    public Student() {
    }
    public Student(Parcel pl) {
        age = pl.readInt();
        name = pl.readString();
    }
    
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub
        dest.writeInt(age);
        dest.writeString(name);
    }

后来在网上查询了一下。问题终于解决了,其实还要感谢这位大哥!http://blog.csdn.net/jackyu613/archive/2010/11/16/6011564.aspx

具体解决办法如下:

 

 参考《Android/OPhone开发完全讲义》第8章里的一段叙述:
“AIDL服务只支持有限的数据类型,因此,如果用AIDL服务传递一些复杂的数据就需要做更一步处理。AIDL服务支持的数据类型如下:

Java的简单类型(int、char、boolean等)。不需要导入(import)。

String和CharSequence。不需要导入(import)。

List和Map。但要注意,List和Map对象的元素类型必须是AIDL服务支持的数据类型。不需要导入(import)。

AIDL自动生成的接口。需要导入(import)。

实现android.os.Parcelable接口的类。需要导入(import)。

其中后两种数据类型需要使用import进行导入,将在本章的后面详细介绍。

传递不需要import的数据类型的值的方式相同。传递一个需要import的数据类型的值(例如,实现android.os.Parcelable接口的类)的步骤略显复杂。除了要建立一个实现android.os.Parcelable接口的类外,还需要为这个类单独建立一个aidl文件,并使用parcelable关键字进行定义。”

其实我们只要在新建一下Student.aidl文件对 Student这个类进行定义,就可以了。代码如下:


/* //device/java/android/android/content/Intent.aidl
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/
package com.android.aidl;
parcelable Student;

在这里切记 parcelable必须为小写,到此问题得到解决,这时目录结构如下:

 

补充一句,网上有一种“办法”,说是在frameworks/base/Android.mk中的LOCAL_AIDL_INCLUDES := $(FRAMEWORKS_BASE_JAVA_SRC_DIRS)前加上LOCAL_AIDL_INCLUDES := 欲import的类所在位置。其实这种“办法”也是受上面所说的import规则限制的。一个没有实现Parcelable接口的类,这种“办法”也是无能为力的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值