Transmission parameters in AIDL include in, out and inout

Posted by monkey_05_06 on Fri, 04 Mar 2022 21:17:05 +0100

preface

Today, we developed AIDL, created a new interface and wrote a function. As a result, we reported an error when build ing

Process 'command 'xxx/aidl'' finished with non-zero exit value 1

After a long time of trying, I finally got it. During this period, I reviewed a lot of knowledge and summarized it together.

Data types supported by AIDL by default

Take a look at IData Source code of Aidl:

package jun.server;

// Declare any non-default types here with import statements

interface IBaseData {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    // short type is not supported
    void basicTypes(byte aByte, int anInt, long aLong,
            boolean aBoolean,
            float aFloat, double aDouble,
            char aChar,
            String aString, CharSequence aCharSequence,
            inout List<String> aList);
}

In addition to short, there are eight basic data types in Java: String, CharSequence, and List. When the parameters we pass in are not of these types, an error will be reported. There are also some cases:

  • The element type of list cannot be a basic type, such as list < long >, list < byte >
  • An error will be reported if import is missing. aidl will not be imported automatically, so we need to add it manually. Because there is no prompt when adding, we must pay attention to the correct

in\out\inout

Some types are missing in\out\inout tags and will also report errors. For example:

  • Parameter type of < list >
  • Array of basic types, such as byte []
  • Implementation class of Parcelable, such as Bundle

So what do these three labels do?

  • in indicates that data can only flow from the client to the server
  • out means that data can only flow from the server to the client
  • inout means that data can flow in both directions between the server and the client.

Among them, the data flow is for the object passing in the method in the client.

  • If in is a directional tag, it means that the server will receive the complete data of that object, but the object of the client will not change due to the modification of the transmission parameters by the server;
  • out means that the server will receive the empty object of that object, but the client will change synchronously after the server makes any modification to the received empty object;
  • When inout is a directional tag, the server will receive the complete information of the object sent by the client, and the client will synchronize any changes made by the server to the object.

For example:

If a parameter is set to out, the client calls the function and passes in an object data, but the server receives an empty object, but the server can assign a value to this object, and the object on the client side will change after the assignment.

Custom class

After customizing the class, you need to add the corresponding file in aidl, otherwise an error will be reported.

Sometimes we need to transfer some complex data and customize a class, which must implement Parcelable. This class must have a copy on both the client and server.

Then we use this type of parameter in the aidl function, but the error is still reported, and the error is also reported when we have added import.

Because we need to add a corresponding file in aidl

such as

package cn.xxx.xxx;
public class Product implements Parcelable {
    ...
}

In the aidl interface

package com.xxx.xxx;

//Note: there is a huge pit here. aidl cannot be added to the package name. Otherwise, it will report an error
import com.xxx.xxxx.Product;

interface IMyAidlInterface {
    void do(in String country, in Product product);
}

Not only import is added here, but also the in tag is added for this type, but the error is still reported.

This requires adding a corresponding file in the aidl directory, as follows

package com.xxx.xxxx;

parcelable Product;

Note that the package name should be the same.

The parameter passed is a callback

The parameter transfer can be a callback, which is actually an aidl interface, such as

package cn.xxx.xxx;

import cn.xxx.xxx.Callback;
interface IMyInterface {
    void setCallback(Callback callback);
}

CallBack file

package cn.xxx.xxx;

interface Callback {
    void onDone(inout byte[] data, int length, int width, int height, int code);
}

This callback is also an aidl and cannot be an ordinary interface, so its parameters need to follow the above constraints

Pay attention to the official account number: BennuCTech, get more dry cargo!

Topics: Android