androidservicestubaidl

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


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?


Solution

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