What is " Stub " and "AIDL" for in java?
In an AIDL file, an interface can be defined with the method signatures of the remote service. The AIDL parser generates a Java class from the interface, that can be used for two different purposes.
- It generates a
Proxy
class to give the client access to the service, It generates a
abstract
Stub
class, that can be used by the service implementation to extend it to an anonymous class with the implementation of the remote methods.
In other words,
When the AIDL android project is compiled, then java class
ISampleSevice.java
shall be generated forISampleSevice.aidl
file.It will have abstract Stub class and a Proxy class.
The remote service has to create an Stub class object, and the same has to be returned to the client when the client calls bindService().
The
onBind()
of remote service shall return an Stub class object.At the client's
onServiceConnected()
, user can get the proxy object of the stub defined at the remote service(theISampleService.Stub.asInterface()
returns the proxy class).The proxy object can be used to call the remote methods of the Stub class implementation at the service process.
'Stub' is a class that implements the remote interface in a way that you can use it as if it were a local one. It handles data marashalling/unmarshalling and sending/receiving to/from the remote service. The term 'stub' is generally used to describe this functionality in other RPC methods (COM, Java remoting, etc.), but it can mean slightly different things.
The IDL (Interface Definition Language) is generally language independent, and you could theoretically generate C++ or Python stub code from it. The Android one is Java-based though, so the distinction is subtle. One difference is that you can only have a single interface in an .aidl file, while Java allows multiple classes/interfaces per .java file. There are also some rules for which types are supported, so it is not exactly the same as a Java interface, and you cannot use one instead of AIDL.