Reputation: 157
I have a simple AIDL definition and I want to use in Kotlin code but when it builds shows Unresolved reference error for all variables that uses the interface. but the same AIDL has no problem in Java code. does Kotlin support it? how to solve here my AIDL in src/main/aidl/
// ServiceInterface.aidl
package com.example.test;
interface ServiceInterface {
void test(String arg1);
}
and activity code is
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.os.RemoteException
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.swiftytime.clientappcommunication.R
import com.example.test.ServiceInterface
class MainActivity : AppCompatActivity() {
var mServiceAidl: ServiceInterface? = null
var mIsBound = false
private val mConnection: ServiceConnection = object : ServiceConnection {
override fun onServiceConnected(className: ComponentName, service: IBinder) {
try {
mServiceAidl = ServiceInterface.Stub.asInterface(service)
Log.e("app", "Attached")
} catch (e: RemoteException) {
}
}
override fun onServiceDisconnected(className: ComponentName) {
mServiceAidl = null
Log.e("app", "Disconnected.")
}
}
private fun doBindService() {
val intent = Intent().apply {
component = ComponentName(
"com.example.test", "com.example.test.MyService"
)
}
bindService(
intent,
mConnection, Context.BIND_AUTO_CREATE
)
mIsBound = true
Log.e("app", "Binding.")
}
private fun doUnbindService() {
if (mIsBound) {
unbindService(mConnection)
mIsBound = false
Log.e("app", "Unbinding.")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
doBindService()
}
}
this is error
[ERROR] [org.gradle.api.Task] e: /Volumes/Projects/AndroidProject/ClientAppCommunication/app/src/main/java/com/example/test/MainActivity.kt: (16, 23): Unresolved reference: ServiceInterface
Upvotes: 4
Views: 3811
Reputation: 157
After many hours, I found the problem which is buildToolsVersion 29.0.0 generate wrong path for generated java files, I submitted a bug
Just changing to buildToolsVersion 28.0.3 solve the issue.
Update:
Problem Solved and now it works in buildToolsVersion 29.0.1
Upvotes: 3
Reputation: 556
I'm using AIDL with Kotlin and what I have is interfaces written in Java and all model classes that are used by defined interfaces are written in Kotlin and it is working perfectly. For example. I have I*Subscriber.aidl with method
void onSomeEventHappened(in AidlEvent event);
and also I have the .aidl file and .kt file for AidlEvent class.
AidlEvent.aidl file
// AidlEvent.aidl
parcelable AidlEvent;
AidlEvent.kt
data class AidlEvent(
val eventType: Int,
val eventMessage: String):
Parcelable {
// add parcelable methods
}
I'm not sure that you will be able to write .aidl interface in Kotlin, haven't managed to do that. It should not be an issue if you need to write few methods in Java, as you don't need to implement them in Java, you will just declare them.
Upvotes: 0