androidaidl

Android AIDL IPC (Unable to call service)


I followed online articles to try and create my own example of IPC using AIDL. Below are my code snippets

ipc_library

// IIPCInterface.aidl
package com.zzzz.ipc_library;

// Declare any non-default types here with import statements

interface IIPCInterface
{
    int returnValues();
}

ipc_server

package com.zzzz.ipc_server

import com.zzzz.ipc_library.IIPCInterface

class IPCServiceImpl : IIPCInterface.Stub()
{
    override fun returnValues(): Int
    {
        val numberValues = 1

        return numberValues
    }
}
package com.zzzz.ipc_server

import android.app.Service
import android.content.Intent
import android.os.IBinder

class IPCService : Service()
{
    override fun onBind(p0: Intent): IBinder?
    {
        return IPCServiceImpl()
    }
}

ipc_server AndroidManifest.xml

        <service android:name=".IPCService"
            android:exported="true"
            android:enabled="true">
            <intent-filter>
                <action android:name="com.zzzz.ipc_server.IPCService"/>
            </intent-filter>
        </service>

ipc_client

class MainActivity : AppCompatActivity(), ServiceConnection
{
    override fun onStart() {
        super.onStart()

        Log.v("AIDL", "onStart calling bindService")

bindService(Intent("com.zzzz.ipc_server.IPCService").setPackage("com.zzzz.ipc_server"),this,BIND_AUTO_CREATE)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        ....
        
        Log.v("AIDL", "returnValues " + ipcservice?.returnValues())

        ....
    }
    
    private var ipcservice: IIPCInterface? = null

    override fun onServiceConnected(p0: ComponentName?, p1: IBinder?){

        Log.v("AIDL", "onServiceConnected called")
        Log.v("AIDL", "")

        ipcservice = IIPCInterface.Stub.asInterface(p1)
    }

    override fun onServiceDisconnected(p0: ComponentName?) {
        TODO("Not yet implemented")
    }
}

I bind the service in onStart.

I never saw the logging in onServiceConnected being called so it seems that function was not executed.

The logging for returnValues shows null instead of 1.

I followed mainly the steps in this article (https://budhdisharma.medium.com/aidl-and-its-uses-in-android-e7a2520093e).

Are there steps I missed?


Solution

  • It may look not quite obvious, but before binding the service you need to add the service package to your manifest's queries section:

    <manifest>
    
        <queries>
            <package android:name="com.zzzz.ipc_server" />
        </queries>
    </manifest>