Code Monkey home page Code Monkey logo

remoter's Introduction

Remoter

Remoter - An alternative to Android AIDL for Android Remote IPC services using plain java interfaces

Remoter makes developing android remote services intuitive without messing with AIDL.

Android IPC through AIDL

Android supports remote IPC using AIDL. This process of using "aidl" is painful and limited.

Some of the problems and limitations of AIDL are :

  • Unlike the intuitive way of defining an inteface defintions as an interface, AIDL forces you to define the interface in an ".aidl" file
  • The .aidl file is usually in a different folder than your normal source
  • You lose most of the IDE capability for the ".aidl"
  • You can't use an existing interface class and convert that to a remote interface -- it has to be defined seperately as ".aidl"
  • Only limited predefined data types are supported in aidl
  • Any custom Parcelable class that you want to pass through the interface has to be defined again as another ".aidl" file!
  • No overloaded methods!- Methods with same name fail
  • Can't extend an aidl with another
  • Can't throw custom exceptions

Remoter - An intuitive way for Android IPC

Remoter solves the above problems in AIDL by allowing you to define the remote interface using plain java interface, and implement it using plain java implementation of the interface.

All you have to do is annotate the interface using @Remoter

@Remoter
public interface ISampleService {
    ...
}
  • No messy .aidl, just plain simple interface
  • Implement the interface directly using intuitive normal java way, instead of extending Stub
  • Fully interoperable with AIDL. Remoter creates the same serialized data as created by AIDL, so it is fully interoperable with AIDL
  • Supports more data types than AIDL, everything supported by Parceler
  • Make an interface that extends other interfaces as @Remoter
  • Interface methods can throw any exceptions. Clients will get the same exception that is thrown.
  • Remoter interface can be templated
  • Remoter is an annotation processor that generates two helper classes during build time -- a client side Proxy and a service side Stub that allows you to wrap your interface and implementation
  • Support kotlin coroutines!

At the client side

  • Simply wrap the binder that you got from the ServiceConnection with the autogenerated Proxy for your interface
ISampleService sampleService = new ISampleService_Proxy( binder );

At the service side

  • Wrap the implementation with the autogenerated Stub to covert it as a remote Binder and return that from your service
Binder binder = new ISampleService_Stub( sampleServiceImpl );

That's it!

Annotations

  • @Remoter Annotate on an interface to make it a remote interface
    • You an also use a marker interface that can provide a list of interfaces for which to generate the Remoter Proxy/Stub classes. For this, annotate @Remoter on the marker interface and specify the list of classes for "classesToWrap"
  /**
   * Example of a marker remoter interface that specifies other interfaces that should generate remoter proxy stub
   * <p>
   * In this case no proxy/stub gets generate for Marker, but it gets generated for IBaseA and IBaseB
   */
	@Remoter(classesToWrap = {IBaseA.class, IBaseB.class})
	private interface Marker {
	}
  • @ParamIn Mark an array or Parcelable parameter as an input only parameter(in of aidl). By default they are input and output (inout of aidl)
  • @ParamOut Mark an array or Parcelable parameter as an output only parameter(out of aidl).
  • @Oneway Annotate on a method (in the @Remoter interface) with void return to make it an asynchronous method.
  • @NullableType Used to annotate a type parameter or suspend function return as nullable. See below for more details

Kotlin Support with suspend functions

Remoter supports Kotlin interfaces with suspend functions. If your interface (marked with @Remoter) has any suspend functions, then the generated Proxy and Stub will be in Kotlin, enabling to call your remoter service method from coroutines.

  • The suspend functions will be dispatched using the Dispatcher.IO context
  • Kotlin Proxy can be created using the optional constructor that accepts IServiceConnector which moves service connection to a suspendable coroutine
Kotlin Example
  • Define interface in kotlin as suspend
@Remoter
interface ISampleService {

    /**
     * A suspend function which will be implemented by a service
     */
    suspend fun authenticate(userName:String, password:String) : Boolean
}
  • Include the depednecy for RemoterBuilder to take advantage of suspended service connection
implementation 'com.josesamuel:remoter-builder:<VERSION>'
  • From your coroutine, call the remote service call as follows
//From your coroutine context - 

//create service using serviceintent
val service = ISampleService_Proxy(context, SERVICE_INTENT)

//call the suspend function
val authenticated = service.authenticate(userName, password)

//The above call will 
 - suspend the current context
 - connect to service, 
 - make the remote call, 

 all sequentially without blocking the calling thread!
 
  • No need to take care of service connection!
  • No need to move to background thread for service call and then to main thred to update UI!
Notes on Kotlin support
  • Add remoter-builder dependency to get support for suspendable service connection using IServiceConnector
  • vararg is not supported. Either use array or non suspend
  • If any return is nullable type on a suspend function, explicitly mark the method with @NullableType
  • If any types in a generic parameter is nullable, explicitly mnark those parameter with @NullableType optionally specifying which indexex of that type parameter are nullable

Getting Remoter

Gradle dependency

dependencies {

    implementation 'com.josesamuel:remoter-annotations:2.0.6'
    kapt 'com.josesamuel:remoter:2.0.5'
    
    
    //If using kotlin coroutines, include following 
    //to make even the service connection simpler - 
    
    implementation 'com.josesamuel:remoter-builder:2.0.6'
    
}

License

Copyright 2017 Joseph Samuel

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.

remoter's People

Contributors

f43nd1r avatar josesamuel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

remoter's Issues

Accessing AIDL file

Is it possible to access the AIDL file that remoter generates from my kotlin interface?

I would like to have access to it so that I can feed it into the Xamarin Android AIDL compiler so that I can have the service be implemented in C#.

Communication between 2 apps

Hi, thank you for great work. I was wondering how one would go about having 2 apps communicating with your library.
You samples seems to show only within communication within app on a different process.

I am trying to have 2 apps, signed with same signature one as server and another as client communication with remoter.
Is it possible at all?

package org.parceler does not exist

Hi! Great library.

Just encountered this issue when using remoter for a kotlin interface. I included the library using a kapt annotation processor:

implementation 'com.josesamuel:remoter-annotations:1.2.4'
kapt 'com.josesamuel:remoter:1.2.4'

The annotation processor runs correctly and generates my proxy and stub, but at compile time throws the error package org.parceler does not exist whenever the generated stub tries to call org.parceler.Parcels.wrap().

Adding the following to my build.gradle works around the issue, but shouldn't Parceler be a transitive dependency?

implementation 'org.parceler:parceler-api:1.1.12'
kapt 'org.parceler:parceler:1.1.12'

Does not work w/ Parcelize

Parcelize itself does not generate an accessible CREATOR or writeToParcel in kotlin. For the CREATOR, technically you can create a java helper method to access the CREATOR accessible from the java side, however, I need to look further into the writeToParcel side.

Interface mismatch between Proxy and Stub on Huawei Mate 20

Even though I'm employing the same interface for both client and server, I encounter the following error on Huawei phones:
java.lang.RuntimeException: Interface mismatch between Proxy and Stub 64 [63]. Use the same interface for both client and server.
Remarkably, this error is exclusive to Huawei devices; the code runs smoothly on other devices.

Unchecked Exception is swallowed on oneway communication

Issue description

Lets assume we two apps, one acts as Server and the other acts as Client for a IPC Oneway call.
Now the Client performs a Oneway IPC call which includes a callback to the Server. The Server tries to process the request and suddenly produces a unchecked exception (OutOfMemory...). The Remoter Framework is now swallowing the unchecked Exception and the Client still waits for the callback to be called.

Steps to reproduce the issue

  1. Client calls Server via Oneway call
  2. Server produces unchecked exception
  3. Remoter swallows exception and Server continues

What's the expected result?

Unchecked exception in case of a IPC oneway call should be thrown which leads to the death of the App.

What's the actual result?

We had a OutOfMemory on the Server side and remoter was the cause that Android wasn't restarting our App because the OutOfMemory wasn't propagated any further. Instead the App got stuck in a unusable state.

issue with generic interface inheritance

Dear @josesamuel,

Thank you for this great library !

I've faced to some build issue with remoter code generation while trying to create some remote interface inheriting some generic interface.

Here is a trivial code example bringing out the issue:

package example;

import java.util.List;

public interface GenericInterface<T extends List<?>> {

    void onNext(T list);
}
package example;

import java.util.ArrayList;

import remoter.annotations.Remoter;

@Remoter
public interface RemoteInterface extends GenericInterface<ArrayList<?>> {
}

Trying to compile those to files results in following error in generated RemoteInterface_Proxy.java:

error: cannot find symbol
  public void onNext(T list_0) {
                     ^
  symbol:   class T
  location: class RemoteInterface_Proxy

It looks like the T generic parameter is not replaced with ArrayList<?> in Proxy class.

I've specified an upper bounded-type List<?> to simplify the example but actually in my real code the upper bounded-type is a remoter interface. This means that I would expect the generated proxy code to deal with this and work with binder in the remote procedure onNext rather than trying to serialize the input parameter.

I'm not familiar with java annotation processor but I can try to give a hand to fix this, if needed.

Best Regards,

How to call setStubProxyCheck on a listener.

Hi came across an issues. I am testing your library for communication between 2 different apps and at one point I have added extra methods to my listener on de server app and notice the crash because client app had the old version of the listener.

I cannot seem to find how to get instance of each listener as _stub and set setStubProxyCheck(false) on them.

As in your examples I have registerListener(listener: IRemoteServiceListener) Remoter interface implementation.

Where would get access to instance of IRemoteServiceListener_Stub?

Allow code generation for foreign interfaces.

I have a multiplatform project (desktop and android) with a hierarchy of modules.

at the base is a plain Java library written in Kotlin. (:businesslogic)
It contains an interface definition. (IBusinessLogic)

I then have an android library module that depends on this .jar, (:businesslogic:android) where I need to create a remoter host service and a client for this interface.

I can add com.josesamuel:remoter-annotations to the Java Library module (:businesslogic) and annotate the interface, but since it is a pure java library with no Android dependency (it produces a .jar, not an .aar) I cannot add com.josesamuel:remoter-builder and have a remoter stub or proxy generated in that module.

I would like to keep the interface defined in the Java module (:businesslogic) so as to have all the business logic separate from the android platform.

would it be possible to add some kind of mechanism to have the remoter-builder generate an implementation for the interface (IBusinessLogic) in the android module? (:businesslogic:android) while keeping the interface in the java module? Perhaps this could apply to any interface, not just ones that are marked with @Remoter?

perhaps something like the marker you have in your suspender library?

@GenerateRemoterFor(classesToWrap = [
    IBusinessLogic::class, 
    IThirdPartyLibraryInterface::class
])
interface XXX

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.