AndLinker is a IPC library for Android, which combines the features of AIDL and Retrofit. Allows IPC call seamlessly compose with RxJava and RxJava2 call adapters.
AndLinker is a IPC(Inter-Process Communication) library for Android, which combines the features of AIDL and Retrofit. Allows IPC call seamlessly compose with RxJava and RxJava2 call adapters. Project design and partial code refer to the great project Retrofit.
Add the
jcenter()repository in your root
build.gradle.
groovy allprojects { repositories { jcenter() } }
Add the dependencies in your app level
build.gradle.
groovy dependencies { implementation 'com.codezjx.library:andlinker:0.7.2' }
Call, RxJava
Observable, RxJava2
Observable&
Flowable
in,
out, or
inout
onewaykeyword
Define a normal java Interface with
@RemoteInterfaceannotation, and implements the interface.
@RemoteInterface public interface IRemoteService {int getPid(); 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 }
};
In your server app, create the
AndLinkerBinderobject and register interface implementation above, then expose the linker binder to clients.
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; }
}
In your client app, create the
AndLinkerobject and generates an implementation of the
IRemoteServiceinterface.
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(); }
}
Now your client app is ready, all methods from the created
IRemoteServiceare IPC methods, call it directly!
int pid = mRemoteService.getPid(); mRemoteService.basicTypes(1, 2L, true, 3.0f, 4.0d, "str");
AndLinker supports all AIDL data types: - All primitive types in the Java programming language (such as
int,
long,
char,
boolean, and so on) -
String-
CharSequence-
Parcelable-
List(All elements in the List must be one of the supported data types in this list) -
Map(All elements in the Map must be one of the supported data types in this list)
You can modify the client side app's remote service interface, wrap the return type of the method.
@RemoteInterface public interface IRemoteService {Observable<integer> getPid(); Call<void> basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString);
}
Register the call adapter factory in
AndLinker.Builder, the remaining steps are consistent with Retrofit.
new AndLinker.Builder(this) ... .addCallAdapterFactory(OriginalCallAdapterFactory.create()) // Basic .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // RxJava2 .build();
Define callback interface to receive callbacks from the remote service with
@RemoteInterfaceannotation.
@RemoteInterface public interface IRemoteCallback {void onValueChange(int value);
}
Use
@Callbackannotation for callback parameter.
void registerCallback(@Callback IRemoteCallback callback);
In your client app, implements the remote callback and register to
AndLinker, and that's it!
private final IRemoteCallback mRemoteCallback = new IRemoteCallback() { @Override public void onValueChange(int value) { // Invoke when server side callback } }; mLinker.registerObject(mRemoteCallback);
You can specify
@In,
@Out, or
@Inoutannotation for parameter, indicating which way the data goes, same as AIDL.
void directionalParamMethod(@In KeyEvent event, @Out int[] arr, @Inout Rect rect);
Caution: - All non-primitive parameters require a directional annotation indicating which way the data goes. Either
@In,@Out, or@Inout. Primitives are@Inby default, and cannot be otherwise. - Parcelable parameter with@Outor@Inoutannotation must implements fromSuperParcelable, or you must add methodpublic void readFromParcel(Parcel in)by yourself.
@OneWayannotation
You can use
@OneWayfor a method which modifies the behavior of remote calls. When used, a remote call does not block, it simply sends the transaction data and immediately returns, same as AIDL.
@OneWay void onewayMethod(String msg);
Add following rules to
proguard-rules.profile, keep classes that will be serialized/deserialized over AndLinker.
-keep class com.example.andlinker.model.** { public void readFromParcel(android.os.Parcel); }
Any issues or PRs are welcome!
Copyright 2018 codezjxLicensed 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. </code.zjx>