What is " Stub " and "AIDL" for in java?

28,146

Solution 1

'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.

Solution 2

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.

  1. It generates a Proxy class to give the client access to the service,
  2. 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.

    enter image description here

In other words,

  • When the AIDL android project is compiled, then java class ISampleSevice.java shall be generated for ISampleSevice.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(the ISampleService.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.

Solution 3

Hey please check this http://developer.android.com/guide/components/aidl.html. It will help you to understand stub and AIDL.

Share:
28,146
AmyWuGo
Author by

AmyWuGo

Updated on May 02, 2020

Comments

  • AmyWuGo
    AmyWuGo about 4 years

    Question 1:

    I am studying Android service and often see code like this:

    private ISampleService.Stub sampleServiceIf = new ISampleService.Stub(){}
    

    What is .Stub ?

    Question 2:

    I checked "AIDL", but I want to know why we have to use that instead of the Java interface file?