开源了一款Android的IPC库AndLinker,结合了AIDL和Retrofit的特性,支持RxJava

2,744 阅读4分钟
原文链接: blog.csdn.net

背景

工作中经常会遇到进程间通信的需求,但每次通过AIDL实现又觉得步骤十分的繁琐。因此用业余时间自己设计并开源了这款IPC库。设计思路来源于Retrofit通过动态代理生成实现类的这种方式,把AIDL与Retrofit的特性完美的结合起来。

这是你们没有体验过的全新方式,只需体验三分钟,你就会像我一样,爱上这款开源库~

项目地址:github.com/codezjx/And…

简介

AndLinker是一款Android上的IPC (进程间通信) 库,结合了AIDLRetrofit的诸多特性,且可以与RxJavaRxJava2的Call Adapters无缝结合使用。项目的设计与部分代码参考了伟大的Retrofit项目。

配置

在项目根目录的build.gradle中添加jcenter()仓库

allprojects {
    repositories {
        jcenter()
    }
}

在App的build.gradle中添加如下依赖

dependencies {
    implementation 'com.codezjx.library:andlinker:0.7.0'
}

功能特性

  • 以普通Java接口代替AIDL接口
  • Retrofit一样生成远程服务接口的IPC实现
  • 支持的Call Adapters:CallRxJava ObservableRxJava2 Observable & Flowable
  • 支持远程服务回调机制
  • 支持AIDL的所有数据类型
  • 支持AIDL的所有数据定向tag:inoutinout
  • 支持AIDL的oneway关键字

快速开始

使用注解@ClassName@MethodName修饰远程服务接口IRemoteService,并实现它

@ClassName("com.example.andlinker.IRemoteService")
public interface IRemoteService {

    @MethodName("getPid")
    int getPid();

    @MethodName("basicTypes")
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}

private final IRemoteService mRemoteService = new IRemoteService() {

    @Override
    public int getPid() {
        return android.os.Process.myPid();
    }

    @Override
    public void basicTypes(int anInt, long aLong, boolean aBoolean,
        float aFloat, double aDouble, String aString) {
        // Does something
    }
};

在服务端App中,创建AndLinkerBinder对象,并注册上面的接口实现。然后在onBind()方法中返回,暴露给客户端

private AndLinkerBinder mLinkerBinder;

public class RemoteService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        mLinkerBinder = AndLinkerBinder.Factory.newBinder();
        mLinkerBinder.registerObject(mRemoteService);
    }

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

在客户端App中,通过Builder创建AndLinker对象,并通过create()方法生成一个IRemoteService远程接口的IPC实现

public class BindingActivity extends Activity {

    private AndLinker mLinker;
    private IRemoteService mRemoteService;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLinker = new AndLinker.Builder(this)
                .packageName("com.example.andlinker")
                .action("com.example.andlinker.REMOTE_SERVICE_ACTION")
                .build();
        mLinker.bind();

        mRemoteService = mLinker.create(IRemoteService.class);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLinker.unbind();
    }
}

一切就绪,现在mRemoteService对象中的所有方法都是IPC方法,直接调用即可

int pid = mRemoteService.getPid();
mRemoteService.basicTypes(1, 2L, true, 3.0f, 4.0d, "str");

支持数据类型

AndLinker支持AIDL所有数据类型:
- Java语言中的所有原始类型 (如:intlongcharboolean,等等)
- String
- CharSequence
- Parcelable
- List (List中的所有元素必须是此列表中支持的数据类型)
- Map (Map中的所有元素必须是此列表中支持的数据类型)

进阶使用

Call Adapters

在客户端App中,你可以copy并修改远程服务接口,包装方法的返回值

@ClassName("com.example.andlinker.IRemoteService")
public interface IRemoteService {

    @MethodName("getPid")
    Observable<Integer> getPid();

    @MethodName("basicTypes")
    Call<Void> basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, 
        double aDouble, String aString);
}

AndLinker.Builder中注册对应的Call Adapter Factory,剩下的步骤基本和Retrofit一致,不再赘述

new AndLinker.Builder(this)
        ...
        .addCallAdapterFactory(OriginalCallAdapterFactory.create()) // Basic
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())  // RxJava2
        .build();

处理远程服务接口回调

使用@ClassName@MethodName注解修饰远程服务回调接口IRemoteCallback

@ClassName("com.example.andlinker.IRemoteCallback")
public interface IRemoteCallback {

    @MethodName("onValueChange")
    void onValueChange(int value);
}

在远程方法中使用@Callback注解修饰callback参数

@MethodName("registerCallback")
void registerCallback(@Callback IRemoteCallback callback);

在客户端App中,实现上面定义的远程服务回调接口IRemoteCallback,并且注册到AndLinker中,就是这么简单

private final IRemoteCallback mRemoteCallback = new IRemoteCallback() {
    @Override
    public void onValueChange(int value) {
        // Invoke when server side callback
    }
};
mLinker.registerObject(mRemoteCallback);

指定数据定向tag

你可以为远程方法的参数指定@In@Out,或者@Inout注解,它标记了数据在底层Binder中的流向,跟AIDL中的用法一致

@MethodName("directionalParamMethod")
void directionalParamMethod(@In KeyEvent event, @Out int[] arr, @Inout Rect rect);

注意:
- 所有非原始类型必须指定数据定向tag:@In@Out,或者@Inout,用来标记数据的流向。原始类型默认是@In类型,并且不能指定其他值。
- 使用@Out或者@Inout修饰的Parcelable参数必须实现SuperParcelable接口,否则你必须手动添加此方法public void readFromParcel(Parcel in)

使用@OneWay注解

你可以在远程方法上使用@OneWay注解,这会修改远程方法调用的行为。当使用它时,远程方法调用不会堵塞,它只是简单的发送数据并立即返回,跟AIDL中的用法一致

@MethodName("onewayMethod")
@OneWay
void onewayMethod(String msg);

反馈

欢迎各位提issues和PRs!

License

Copyright 2018 codezjx <code.zjx@gmail.com>

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.