java - Kotlin 是否支持 AIDL?

标签 java android kotlin aidl

我有一个简单的 AIDL 定义,我想在 Kotlin 代码中使用,但是当它构建时,对使用该接口(interface)的所有变量显示 Unresolved 引用错误。但是同样的AIDL在Java代码中没有问题。 Kotlin 支持吗?怎么解决 这是我的 AIDL 在 src/main/aidl/

// ServiceInterface.aidl
package com.example.test;

interface ServiceInterface {
    void test(String arg1);
}

Activity 代码是

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()
    }
}

这是错误

[ERROR] [org.gradle.api.Task] e: /Volumes/Projects/AndroidProject/ClientAppCommunication/app/src/main/java/com/example/test/MainActivity.kt: (16, 23): Unresolved reference: ServiceInterface

最佳答案

几个小时后,我发现问题是 buildToolsVersion 29.0.0 generate wrong path for generated java files,我提交了一个 bug

只需更改为 buildToolsVersion 28.0.3 即可解决问题。

更新: 问题已解决,现在可以在 buildToolsVersion 29.0.1

中使用

关于java - Kotlin 是否支持 AIDL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59156304/

相关文章:

android - 如何在我的应用程序中使用 CMU Flite TTS 引擎?

java - 如何在登录后重定向处于不同 Activity 的 2 个不同用户?

Kotlin 作为协程之间的消息队列流动

java - 将支持 bean 实例作为参数传递给另一个支持 bean 方法

java - 更新类中的类

java - 尝试在空对象引用上调用虚拟方法(JAVA、AS)

java - JSONArray在android中抛出内存异常

android - 如何使用回调对挂起函数进行单元测试

android - @Parcelize 在多平台项目中

java - 闯关游戏中只有一 block 砖 block 消失