user3239558
user3239558

Reputation: 1827

Android AIDL: Project crash after change code to kotlin from java

These are the link of my project AIDL-Client, AIDL-Server. Steps:

  1. Please install both apks.

  2. Then click "BIND Device button"

  3. "IBindDeviceCallback: deviceName: tpd deviceBrand: loop" is print in client project

  4. change "AIDLService.java" to "AIDLService.kt"in AIDL-Server project then install apk.

  5. Then click "BIND Device button"" of AIDL-server project

you will find the crash.

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter deviceCertifcate
                                                                at android.os.Parcel.readException(Parcel.java:1697)
                                                                at android.os.Parcel.readException(Parcel.java:1646)
                                                                at com.loop.ILoopService$Stub$Proxy.bindDevice(ILoopService.java:88)
                                                                at com.client.MainActivity$mServiceConnection$1.onServiceConnected(MainActivity.kt:53)
                                                                at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1516)
                                                                at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1544)
                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                at android.os.Looper.loop(Looper.java:154)
                                                                at android.app.ActivityThread.main(ActivityThread.java:6682)
                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

It looks like android/kotlin's problem rather than mine.

Any idea about this crash, how to fix it in kotlin?

Upvotes: 0

Views: 961

Answers (1)

emerssso
emerssso

Reputation: 2386

The reason the exception looks like it is coming from Android is because it is being passed between processes.

On MainActivity.kt line 53 in your client code, you pass null to bindDevice for the deviceCertificate byte[]. The exception is telling you that this cannot be null.

When you convert the service from Java to Kotlin, it treats the method parameter as non-null by default, deviceCertifcate: ByteArray. If you tell Kotlin that this parameter can be null, it will not crash. This is acheived by declaring the type with a ?, i.e. deviceCertifcate: ByteArray?.

For more information see the Kotlin docs on Null-Safety.

In a normal Kotlin app, this wouldn't even compile, but since this goes across process boundaries via AIDL, the compiler can't catch this issue.

Upvotes: 1

Related Questions