Kibi
Kibi

Reputation: 2060

How to pass a complex data structure in a callback from an AAR (Kotlin)

I have written an AAR in Kotlin, which has a function that:

fun writeToDevicesList(context : Context,
                           bleDevicesList: ArrayList<BleDevice>,
                           sendingMap: MutableMap<String, ArrayList<Pair<String, ByteArray>>>,
                           writingCallback: (MutableMap<String, ArrayList<Pair<MyResultClass, ByteArray?>>>) -> Unit) {
    var resultsMap : MutableMap<String, ArrayList<Pair<MyResultClass, ByteArray?>>> = LinkedHashMap()
    // do a lot of stuff filling up the results map
    writingCallback?.let {
        it(resultsMap)
    }
}

So anyway, in the project which has this aar code, I have a test app which calls this function and all the data comes through fine...It's a whole load of replies, success, fails, and all the byteArrays of the results that came in - all fine

Then I build it into an AAR and use the same function in the same way in another project

There's loads of logging, so I can see that the messages are sent, and replies come into the aar code filling up my map with useful information - the callback is called and my app receives back a Map which contains the right number of entries (as the aar produced). The keys are all there (they match the MAC ids of the devices I tried to send to) but for some reason the lists (while they have the right number of entries...it can vary per device) always show the result is a fail and the byte arrays are null

So I am worried that passing such a complex datatype doesn't work through an AAR, although i have not seen that in the documentation I read - maybe someone can point me to it.

I read some questions like this which say that if passing from activity to activity one should use serialization - would this work better?

Any suggestions might help

Thanks

Upvotes: 0

Views: 47

Answers (1)

Kaung Khant Kyaw
Kaung Khant Kyaw

Reputation: 540

If the code works in your app but not in the AAR file, it might be caused by ProGuard rules. Add required proguard rules in yourLibraryPath/consumer-rules.pro and rebuild AAR file.

Also make sure comsumerProguardFiles is defined in build.gradle.kts file of your library.

android {
    ...
    defaultConfig {
        ...
        consumerProguardFiles("consumer-rules.pro")
    }
    ...
}

After rebuilding the AAR file, it should include proguard.txt inside it. This ProGuard rules in your Android library will be automatically added to the ProGuard configuration of any project that uses your AAR file, so there is no need to rewrite rules.

Upvotes: 0

Related Questions