I've been trying to debug my code all this afterno...
# compose
l
I've been trying to debug my code all this afternoon, but without success. Meanwhile, I've updated compose to the
1.0.0-beta02
Does not run on my device any more
Also I've updated my Studio IDE, and deleted the application from my device, cleared the project, and retried. The preview works in interactive mode (but is a bit slow because of my host computer).
s
This might not fix the issue but there's a new version of activity-compose
πŸ™ 1
l
Thank you, but I still get the same crash. Meanwhile, I keep the version of alpha04 πŸ™‚
l
Are you using a dependency that might rely on an older Compose version?
t
It would be easier to help if you try to identify the code parts which lead to the problem. So try to simplify your code until it works. And than maybe you find it out yourself or you can post this short code that leads to the issue.
l
@louiscad I don't think so. I'll have a double check.
t
Fo me beta02 works πŸ˜„
l
@Timo Drick Strange for the fact it runs on your side. You're right for the analyze, even if I have an idea on the code which cause the issue :
Copy code
@Composable
private fun MovedPiece(cellsSize: Float, positionFen: String, dndData: DndData) {
    val boardLogic = Board.fromFen(positionFen)
    val square = getSquareFromCellCoordinates(dndData.startFile, dndData.startRank)
    val piece = boardLogic.getSquareOccupant(square)
    val imageRef = piece.getPieceImageID()
    val imageDescription =
        piece.getPieceImageDescriptionID()

    val x = with(LocalDensity.current) { dndData.movedPieceX.toDp() }
    val y = with(
        LocalDensity.current
    ) { dndData.movedPieceY.toDp() }
    val imageSize = with(LocalDensity.current) {
        cellsSize.toDp()
    }

    if (imageRef != null && imageDescription != null) {
        Image(
            painter = painterResource(id = imageRef),
            contentDescription = stringResource(imageDescription),
            modifier = Modifier
                .size(imageSize)
                .offset(x, y)
        )
    }
}
Here I am trying to render a component if I could get all data, and to not render anything otherwise.
l
Did you try this snippet in a project that works on beta-02?
πŸ‘ 1
t
It is still to complexe to try. To many dependencies.
l
Good idea : I'm gonna try from a fresh project. Thank you :)
πŸ‘ 1
Finally I found the issue πŸ™‚ It was indeed that composable which used an if clause in order to return either an
Image
or nothing. Using the if clause upwards, in a parent composable makes it work on my device.
Thank you all for your answers πŸ™‚
l
Why would it crash?
t
If you think it is an bug than try to create a simple reproducer and report the issue.
l
Yes, where should I report the bug ?
l
l
Thank you, gonna make it right now πŸ™‚
πŸ‘ 1
Done
l
Then link it?
l
Did you find a way to have a very small reproducer, like less than 20 LOC?
l
I'll try to reduce the amount of code in a example now.
l
If you have it, posting it in a comment (or original post next time) can help people that will look into your issue and attempt to fix it.
l
Strange, it try to set a short version of my issue. But this time no crash :
Copy code
package com.loloof64.chessexercisesorganizer.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import java.util.*

@Composable
fun BugPOC() {
    Box(modifier = Modifier
        .size(300.dp)
        .background(Color.Yellow)) {
        val date = Date()
        if (date.time%2 == 0L) {
            Box(modifier = Modifier
                .size(20.dp)
                .background(Color.Red)
                .offset(50.dp, 80.dp))
        }
    }
}

@Preview
@Composable
fun PreviewBugPOC() {
    BugPOC()
}
t
On my system this do not crash. But it will not work as you maybe expect.
l
On my side, it works : if I have a very good luck , I can see a red square.
t
What do you want to archive?
l
I wanted to know if it the conditionnal @Composable which made my application crash.
t
I use conditional render a lot. I can say you that is not the issue.
l
I understand, so the issue is deeper.
t
Maybe the interaction between some custom classes from you with compose. But hard to say because you sample is very complexe.
l
Unfortunately it's also very hard to simplify it.
t
Try build the composables that way that all necessary information came from input parameters not from any global data. Than you are able to check them individually with test data.
l
I think the code which crashes is already organized in such way : I am not using global data. You can see it at as well as project configuration at https://github.com/loloof64/ChessExercisesOrganizerJetpackCompose/tree/555dff15e0e1b987559391f11f1f3433d4469c18. I've tried to split up the code as much as possible : also with some extension functions.