<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Zeliot </title>
    <description>The latest articles on DEV Community by Zeliot  (@zeliotofficial).</description>
    <link>https://dev.to/zeliotofficial</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F2710%2Fcf648208-e23e-4b1c-a7ca-dc078303198a.png</url>
      <title>DEV Community: Zeliot </title>
      <link>https://dev.to/zeliotofficial</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zeliotofficial"/>
    <language>en</language>
    <item>
      <title>AIDL deep dive post does not exist! Part - I</title>
      <dc:creator>Prasanna</dc:creator>
      <pubDate>Sat, 18 Jul 2020 06:33:30 +0000</pubDate>
      <link>https://dev.to/zeliotofficial/aidl-deep-dive-post-does-not-exist-part-i-4po8</link>
      <guid>https://dev.to/zeliotofficial/aidl-deep-dive-post-does-not-exist-part-i-4po8</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Hello there! I am going to talk about Android's parliamentary conversations. What do I mean by that? I mean, the IPC(Inter Process Communication) that is happening inside Android system. We are going to use AIDL for the so called "conversation". I will explain better and deeper in this article.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;We all know it!&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Why is this a brand new post? Because, it's not about the IPC of components inside one Android application. It's about the communication of two different applications. Most people agree to disagree that it's pretty easy to understand AIDLs. But, there is saying(of mine), &lt;code&gt;"If you understand interfaces in OOP, you shall understand AIDL!"&lt;/code&gt;. Well, Let's see what that saying really means, in detail.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The AIDL unbolted.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Android Interface Definition Language(AIDL), is what you every time you Google AIDL. This doesn't explain what it really is. Let me explain! AIDL is an Android implementation to achieve Inter Process Communication(IPC) in between Android components. Diving deep, there is a program called &lt;code&gt;aidl&lt;/code&gt;, which compiles the AIDL source code and generates client(Proxy) and server(Stub) Java interfaces.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fprgype5k7uh08zi40kly.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fprgype5k7uh08zi40kly.png" alt="Alt Text" width="675" height="215"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Initial building blocks.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let us start with the basics. To create an AIDL, we need to open an Android Project and click &lt;code&gt;File -&amp;gt; New -&amp;gt; AIDL -&amp;gt; AIDL File&lt;/code&gt;. Write your first AIDL file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

interface IMyAidlInterface {
    String getMessage();
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then, do not forget to &lt;code&gt;Build -&amp;gt; Rebuild&lt;/code&gt;. The &lt;code&gt;IMyAidlInterface&lt;/code&gt; class will be generated. Create a class which extends the &lt;code&gt;IMyAidlInterface.Stub&lt;/code&gt;. Please remember to override &lt;code&gt;getMessage()&lt;/code&gt; method in this class.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

public class MyImplementor extends IMyAidlInterface.Stub {
    @Override
    public String getMessage() {
        return "Hello from AIDL Stub!";
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once we have extended the Stub class and implemented(overridden) the method, we must create a Service which is going to expose the APIs to the applications which need bind the AIDL functionalities.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

public class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyImplementor();
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Also, add the &lt;code&gt;&amp;lt;service&amp;gt;&lt;/code&gt; tag in &lt;code&gt;AndroidManifest.xml&lt;/code&gt; inside &lt;code&gt;&amp;lt;application&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;service
    android:name=".MyService"
    android:enabled="true"
    android:exported="true"
    android:process=":remote"&amp;gt;
    &amp;lt;intent-filter&amp;gt;
        &amp;lt;action android:name="MyService" /&amp;gt;
    &amp;lt;/intent-filter&amp;gt;
&amp;lt;/service&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Now that we have implemented the Service part, we should go ahead and create another Android project for the client part. Please note that the AIDLs are very &lt;strong&gt;case-sensitive&lt;/strong&gt;. Unfortunately, Android Studio is not mature enough to detect the syntax errors in an AIDL file. Any changes in an AIDL file would need a rebuild of the project.&lt;br&gt;
 &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Real River to cross!&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We are done with the server(Service) part. Now, we should create another application which will be our client part. So, create a new project in Android Studio. After creating the project, we have to do one of the most important things.&lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;In Android Studio, select the Project view in the left top menu. Inside &lt;code&gt;app/src/main/&lt;/code&gt;, create a folder called &lt;code&gt;aidl&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcpc4t7zilznasmno0eeh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcpc4t7zilznasmno0eeh.png" alt="Alt Text" width="800" height="320"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;After this, go to the AIDL service application, and copy the contents of &lt;code&gt;aidl&lt;/code&gt; folder.&lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fht0bxlgcj9a6o834a47y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fht0bxlgcj9a6o834a47y.png" alt="aidl2" width="397" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Switch back to the client application and paste the contents into &lt;code&gt;aidl&lt;/code&gt; folder. &lt;code&gt;Build -&amp;gt; Rebuild&lt;/code&gt;. The project should build without any errors.&lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;Go to MainActivity.java and create a &lt;code&gt;ServiceConnection&lt;/code&gt; object and &lt;code&gt;IMyAidlInterface&lt;/code&gt; instance.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

private IMyAidlInterface iMyAidlInterface;
private final ServiceConnection mServiceConnection =
    new ServiceConnection() {
        @Override
        public void onServiceConnected(
                    ComponentName name, IBinder service) {

            iMyAidlInterface =
                    IMyAidlInterface.Stub.asInterface(service);

            Log.d(TAG, "Service Connected.");
            Toast.makeText(MainActivity.this, "Service Connected.", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            iMyAidlInterface = null;
            Toast.makeText(MainActivity.this, "Service Disconnected.", Toast.LENGTH_SHORT).show();
        }
    };


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;In &lt;code&gt;onCreate()&lt;/code&gt; of &lt;code&gt;MainActivity.java&lt;/code&gt;, bind the service. Note that we need the action and the package name to successfully bind to the service.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = new Intent();
    intent.setPackage("com.zeliot.aidldemo");
    intent.setAction("MyService");
    bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, create a button and while clicking the button, call &lt;code&gt;iMyAidlInterface.getMessage()&lt;/code&gt; to receive the String from the AIDL service.&lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;We are done with client and server parts. As of now, the Service is connected from another application. We can do any type of operations using the same. We can have data trasnfer,aynchronous calls, callbacks. More detailed implementation will be released in the second part of this series.&lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/prasan29/aidl-blog" rel="noopener noreferrer"&gt;AIDL source code&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you, for reading. Happy interfacing!! 😀 🙂
&lt;/h2&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fprofile-counter.glitch.me%2Fprasan29%2Fcount.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fprofile-counter.glitch.me%2Fprasan29%2Fcount.svg" alt="Visits" width="224" height="30"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>aidl</category>
      <category>java</category>
      <category>kotlin</category>
    </item>
  </channel>
</rss>
