PieBridge

An efficient, light, and easy-to-use framework for Android Inter-Process Communication (IPC).

An efficient, compact, and easy to use Bundle-based IPC framework for Android interprocess communication. Github.com/kuangfrank/…

These days, I have learned iQiyi’s cross-process communication framework — Andromeda, and studied The Android cross-process call solution Bifrost based on JSON PRC protocol. I feel inspired.

Andromeda is suitable for the overall solution of App multi-process architecture; Bifrost’s jSON-based communication protocol has room for improvement in call efficiency and support for complex types.

It wouldn’t be great if a framework borrowed the ideas from both frameworks to simplify the integration process and improve cross-process invocation efficiency and support for complex types.

This was the starting point for developing the PieBridge framework. Timed to coincide with qixi Festival, the name PieBridge implies smooth communication between Android processes.

The PieBridge framework communicates based on the Bundle provided natively by Android. The Bundle implements the Parcelable interface and internally maintains the Map<String,Object> data structure, enabling both efficient communication and conversion between complex types.

PieBridge GitHub PieBridge framework, Lib code contains only 4 Java files, but 300 lines of code, easy to learn and use.

Call way

Android cross-process calls use the AIDL approach, which usually requires a lot of code and complex operations; Cross-process invocation of different services is not easy to reuse. The PieBridge framework library can be used to reduce the difficulty.

Just define an interface and implementation class, just like a local call.

public interface IBookApi {
    Bundle insertBookListMethod(Bundle param);
    Bundle deleteBookListMethod(Bundle param); . }Copy the code
public class BookApiImpl implements IBookApi {
    @Override
    public Bundle insertBookListMethod(Bundle param) {
        ArrayList<Book> tmpList = BookApiUtil.fromBundle(param);
        sBookList.addAll(tmpList);

        Bundle result = BookApiUtil.toBundle(sBookList);
        return result;
    }

    @Override
    public Bundle deleteBookListMethod(Bundle param) {...returnresult; }... }Copy the code

Re-register interfaces and implementation classes and invoke them during Application initialization. For details, see Demo Application.

public class MainApp extends Application {...@Override
    public void onCreate(a) {
        super.onCreate();

        if (isMainProcess(this)) {
            PieBridge.getInstance().init(this);
        } else if (isPieBridgeProcess(this)) { PieBridge.getInstance().register(IBookApi.class, BookApiImpl.getInstance()); }}}Copy the code

The basic principle of

Define a generic ADIL interface

import android.os.Bundle;
interface IPieBridgeAidl {
    Bundle call(in Bundle args);
}
Copy the code

In cross-process communication, the caller passes in the name of the calling interface as a parameter through ADIL proxy, and the server invokes the actual interface in the way of dynamic proxy and returns data.

summary

advantages

  • Easy to learn and use, simplifying cross-process coding
  • Efficient invocation and support complex data transfer
  • Source code does not depend on any tripartite library, the number of files, less code, easy to integrate

disadvantages

  • A conversion is required between the Bundle and the original data type. Even simple data types need to communicate through bundles
  • Currently asynchronous code invocation is not implemented, only synchronous operations are supported

Contact information and related links

My E-mail: [email protected]

Github.com/kuangfrank/…

Github.com/iqiyi/Andro…

Github.com/LiushuiXiao…