What is " Stub " and "AIDL" for in java?
Asked Answered
C

4

38

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?

Consalve answered 18/5, 2012 at 7:14 Comment(0)
G
38

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

Glooming answered 18/5, 2012 at 7:30 Comment(3)
I am still confused. Maybe I need more time. About implements of IPC in different computer language are the same? or they are different but all of them need to do that kind of thing. And in Java especially based on SDK, if we use IDL, it will be easier? Could I explain it like this? Thank you Nikolay.Consalve
Those are general concepts. While you could probably implement something other than AIDL in Android, it won't be standard, so you can just think of AIDL as the only standard RPC mechanism.Glooming
You can start here to get the basic idea: en.wikipedia.org/wiki/Remote_procedure_callGlooming
Y
6

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.

Yahrzeit answered 30/4, 2019 at 21:13 Comment(0)
H
1

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

Haversine answered 18/5, 2012 at 9:4 Comment(0)
S
0

So in other words you can eves drop on another person privacy, invasion of another person affairs.get information on them and then use there Identity and Money listen in on the conversation and Stealing from them. And doing it all from Afar.In Congeito..

Shaniqua answered 25/6, 2024 at 7:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.