Akash Adhkary
Akash Adhkary

Reputation: 1

AIDL service is not working in Android 13

I am learning about AIDL services so I have created two apps. AIDL Server and AIDL Client.

In AIDL Server app I have created a .aidl file which you can see below.

// IAIDLColorInterface.aidl
package com.example.aidlserver;

interface IAIDLColorInterface {
    
    int getColor();
}

After creating this file I have rebuilt the project which created interface java file. Then I created the service class as below.

package com.example.aidlserver

import android.app.Service
import android.content.Intent
import android.graphics.Color
import android.os.IBinder
import android.os.RemoteException
import android.util.Log
import java.util.Random


class AIDLColorService : Service() {

    override fun onBind(intent: Intent): IBinder {
        return binder
    }

    private val binder: IAIDLColorInterface.Stub = object : IAIDLColorInterface.Stub() {
        @Throws(RemoteException::class)
        override fun getColor(): Int {
            val rnd = Random()
            val color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))
            Log.d("color", "getColor: $color")
            return color
        }
    }

    override fun onCreate() {
        super.onCreate()
        Log.d("AIDLColorService", "Service created")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("AIDLColorService", "Service destroyed")
    }
}

Manifest file is as follows

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AIDLServer"
        tools:targetApi="31">
        <service
            android:name=".AIDLColorService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="AIDLColorService" />
            </intent-filter>
        </service>
    </application>

</manifest>

Now I have created another app AIDL Client which has button in main activity. On button click I am calling the service which should return the random color. But it doesn't seem to work. When I opened the app I got the log "Failed to bind service" which you can see below for the reference. Following is the code for MainActivity.kt in AIDL Client app.

package com.example.aidlclient;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import com.example.aidlclient.databinding.ActivityMainBinding;
import com.example.aidlserver.IAIDLColorInterface;


class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    lateinit var iAIDLColorService: IAIDLColorInterface
    var TAG = "MAIN ACTIVITY"

    private var mConnection: ServiceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            iAIDLColorService = IAIDLColorInterface.Stub.asInterface(service)
            Log.d(TAG, "Remote config Service Connected!!")

            // Once the service is connected and initialized, set the click listener here.

        }

        override fun onServiceDisconnected(name: ComponentName?) {
            Log.e(TAG, "onServiceDisconnected: $name", )
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val intent = Intent("AIDLColorService")
        intent.setPackage("com.example.aidlserver")

        val isServiceBound = bindService(intent, mConnection, Context.BIND_AUTO_CREATE)
        if (isServiceBound) {
            Log.d(TAG, "Service bound successfully")
        } else {
            Log.e(TAG, "Failed to bind service")
        }

        binding.btn.setOnClickListener {
            if(!this::iAIDLColorService.isInitialized)
                return@setOnClickListener
            val color: Int = iAIDLColorService.getColor()
            Log.i(TAG, "onServiceConnected: $color")
            binding.btn.setBackgroundColor(color)
        }
    }
}

I have added the same .aidl file from AIDL Server app to this AIDL Client app.

Is there anything I am missing in android 13? This seems to work perfectly in android 10 device. After Android 10 it doesn't work.

Thanks

I am new to this so didn't know what to search. I read full document from Android on AIDL and services but couldn't find the solution.

Upvotes: 0

Views: 879

Answers (1)

iroiroys
iroiroys

Reputation: 963

As @Pawel commented, You need to add <queries> element in AndroidManifest.xml

<queries>
    <package android:name="com.example.aidlserver" />
</queries>

Upvotes: 0

Related Questions