Android java中的“存根”和“AIDL”是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10648280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:30:00  来源:igfitidea点击:

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

androidservicestubaidl

提问by AmyWuGo

Question 1:

问题 1:

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

我正在研究Android服务,经常看到这样的代码:

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

What is .Stub?

什么是.Stub

Question 2:

问题2:

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

我检查了“ AIDL”,但我想知道为什么我们必须使用它而不是 Java 接口文件?

回答by Nikolay Elenkov

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

'Stub' 是一个实现远程接口的类,您可以像使用本地接口一样使用它。它处理数据编组/解组以及向/从远程服务发送/接收数据。术语“存根”通常用于描述其他 RPC 方法(COM、Java 远程处理等)中的此功能,但它的含义可能略有不同。

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.

IDL(接口定义语言)通常与语言无关,理论上您可以从中生成 C++ 或 Python 存根代码。Android 是基于 Java 的,所以区别很微妙。一个区别是 .aidl 文件中只能有一个接口,而 Java 允许每个 .java 文件有多个类/接口。对于支持哪些类型也有一些规则,因此它与Java接口并不完全相同,您不能使用一个来代替AIDL。

回答by Hardian

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.

在 AIDL 文件中,可以使用远程服务的方法签名定义接口。AIDL 解析器从接口生成一个 Java 类,可用于两种不同的目的。

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

  1. 它生成一个Proxy类来让客户端访问服务,
  2. 它生成一个abstractStub类,服务实现可以使用该类将其扩展为具有远程方法实现的匿名类。

    在此处输入图片说明

In other words,

换句话说,

  • When the AIDL android project is compiled, then java class ISampleSevice.javashall be generated for ISampleSevice.aidlfile.

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

  • AIDL android项目编译完成后,ISampleSevice.java会为ISampleSevice.aidl文件生成java类。

  • 它将具有抽象 Stub 类和一个 Proxy 类。

  • 远程服务必须创建一个 Stub 类对象,并且在客户端调用 bindService() 时必须将其返回给客户端。

  • onBind()远程服务将返回一个stub类对象。

  • 在客户端onServiceConnected(),用户可以获得远程服务定义的存根的代理对象(ISampleService.Stub.asInterface()返回代理类)。

  • 代理对象可用于在服务进程中调用 Stub 类实现的远程方法。

回答by PravinK

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

嘿,请检查这个http://developer.android.com/guide/components/aidl.html。它将帮助您理解存根和 AIDL。