Android\Sdk\build-tools\28.0.2\aidl.exe'' finished with non-zero exit value 1(或者27.0.3等)关于AIDL初学的坑

本文是参照《Android开发艺术探索》

一、.建实体类Book

1.新建一个文件夹aidl(名字自己决定其他也行),注意:实体类的包名和aidl文件包名一定要相同,直接新建aidl文件Android Studio会生成一个包名和主目录相同的文件,直接看图

Android\Sdk\build-tools\28.0.2\aidl.exe'' finished with non-zero exit value 1(或者27.0.3等)关于AIDL初学的坑_第1张图片

2.比如我要新建一个名为IBookManager的aidl文件,直接新建的会生成没有绿色部分(后来可以自己修改)的样子,我是直接新建没有修改包名猜出错的(可以直接新建,然后修改包名)

3.Book实体类(没什么东西,Parcelable 可以用插件生成

public class Book implements Parcelable {
    private int bookId;
    private String bookName;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.bookId);
        dest.writeString(this.bookName);
    }

    public Book() {
    }

    public Book(int bookId, String bookName) {
        this.bookId = bookId;
        this.bookName = bookName;
    }

    protected Book(Parcel in) {
        this.bookId = in.readInt();
        this.bookName = in.readString();
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        @Override
        public Book createFromParcel(Parcel source) {
            return new Book(source);
        }

        @Override
        public Book[] newArray(int size) {
            return new Book[size];
        }
    };

    @Override
    public String toString() {
        return "Book{" +
                "bookId=" + bookId +
                ", bookName='" + bookName + '\'' +
                '}';
    }
}

二、建一个和实体类同名的aidl(为了能够让真正的aidl文件能够导入实体类Book)

// Book.aidl
package com.example.administrator.myapplication.aidl;
parcelable Book;

注意一下包名应该和截图部分一样

三、建真正的aidl文件IBookManager

// IBookManager.aidl
package com.example.administrator.myapplication.aidl;

// Declare any non-default types here with import statements
import com.example.administrator.myapplication.aidl.Book;
interface IBookManager {

    List getBookList();
    void addBook(in Book book);
}

注意包名和import,然后make project;

 

你可能感兴趣的:(Android)