ythy / blog

Give everything a shot
6 stars 0 forks source link

AIDL #116

Open ythy opened 6 years ago

ythy commented 6 years ago

reference 实例

While building Android apps you might sometimes need to expose functionality which other processes can use. As these calls are between separate processes, they cannot be a simple function, as one Android process cannot access the data in another.

To pass data between processes, it needs to be marshaled and unmarshaled accordingly. This marshaling and unmarshmaling of data to primitive types is so the OS can understand it passing over inter process communication (IPC) can be tedious and error prone if done manually. Android interface definition language (AIDL) helps solve this problem. AIDL generally has three components which are:

The interface library

Defines the interface using AIDL between the client and service. This library will be a dependency of both the client and the service implementing the interface.

The Service

This will be a separate application (apk) that implements the interface defined by the interface library.

The client

The client can be a separate application (apk) which will make calls to the service using the interface library.

Defining an AIDL

To define an AIDL, we need a file with the .aidl extension. In this tutorial we will place the AIDL in a separate library project . The Android SDK takes the AIDL and generates a Binder, implemented by the service.

The AIDL syntax is Java-like. You can use all primitive types in your AIDL definition and objects like ‘Strings’, ‘List’ and ‘Maps’. If you want to define your own objects, then you can, but you need to provide a class definition that implements the Parcelable interface.

aidl interface文件中定义的方法 参数不能包含自定义类型,返回值可以,否则报错

// FavorApp.aidl
package com.mx.aidl.easytouch;

parcelable FavorApp;

-----------------------------------------------------------------------------------------------

// IRemoteFavService.aidl
package com.mx.aidl.easytouch;

// Declare any non-default types here with import statements
import com.mx.aidl.easytouch.FavorApp;

interface IRemoteFavService {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);

    void addFavor(String name);

    List<FavorApp> getFavor();

}

Implementing the AIDL interface.

package com.mx.easytouch.service;

/**
 * Created by maoxin on 2018/6/28.
 */

public class FavorAppService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private final IRemoteFavService.Stub mBinder = new IRemoteFavService.Stub() {

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
        }

        @Override
        public void addFavor(String name) throws RemoteException {
        }

        @Override
        public List<FavorApp> getFavor() throws RemoteException {
            return null;
        }

    };

}
ythy commented 6 years ago

Android Studio 生成及引用 aar 文件的方法:

  1. 通过gradle的 assembleRelease 命令在output文件夹生成 XXX-release.aar
  2. 项目中 new Moudle后 选择 import .jar/,aar Package 选择上一步生成的 aar文件
  3. 在Service或Client项目的Moudle中 通过add Module Dependency 添加 XXX-release 依赖。

完成!