https://kotlinlang.org logo
#getting-started
Title
# getting-started
z

Zokivasilic8

12/05/2023, 12:36 PM
Hi everyone, this is my first time writing in the group, so hopefully everything will be OK. Can you help me what can be the root cause of the following exception in the Kotlin language:
Copy code
org.jetbrains.kotlin.util.KotlinFrontEndException: Front-end Internal error: Failed to analyze declaration
File being compiled: (68,5) in Kotlin file[0m
[10:27:02]: ▸ [35mThe root cause java.io.FileNotFoundException was thrown at: java.base/java.io.FileInputStream.open0(Native Method)
Thanks to all who can help out on this issue. 🙂
j

Jose Ramon (JR)

12/05/2023, 1:22 PM
Where is the kotlin code ?
🤔 1
z

Zokivasilic8

12/05/2023, 1:49 PM
This is the class that the Kotlin compiler is complaining about, but this is happening on the CI/CD tool side and not locally
Copy code
package com.usemenu.sdk.brandresources.assets.svg

import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.RectF
import androidx.core.graphics.createBitmap
import androidx.core.graphics.drawable.toDrawable
import coil.ImageLoader
import coil.decode.*
import coil.fetch.SourceResult
import coil.request.Options
import coil.request.css
import com.caverock.androidsvg.RenderOptions
import com.caverock.androidsvg.SVG
import kotlinx.coroutines.runInterruptible

class CustomCoilSvgDecoder @JvmOverloads constructor(
    private val source: ImageSource,
    private val options: Options,
    private val useViewBoundsAsIntrinsicSize: Boolean = true
) : Decoder {

    override suspend fun decode(): DecodeResult =
        runInterruptible {
            val svg = source.source().use { SVG.getFromInputStream(it.inputStream()) }

            val svgWidth: Float
            val svgHeight: Float
            val viewBox: RectF? = svg.documentViewBox
            if (useViewBoundsAsIntrinsicSize && viewBox != null) {
                svgWidth = viewBox.width()
                svgHeight = viewBox.height()
            } else {
                svgWidth = svg.documentWidth
                svgHeight = svg.documentHeight
            }

            val bitmapWidth: Int
            val bitmapHeight: Int
            val resScalingFactor = Resources.getSystem().displayMetrics.density
            if (svgWidth > 0 && svgHeight > 0) {
                bitmapWidth = (resScalingFactor * svgWidth).toInt()
                bitmapHeight = (resScalingFactor * svgHeight).toInt()
            } else {
                bitmapWidth = DEFAULT_SIZE
                bitmapHeight = DEFAULT_SIZE
            }

            // Set the SVG's view box to enable scaling if it is not set.
            if (viewBox == null && svgWidth > 0 && svgHeight > 0) {
                svg.setDocumentViewBox(0f, 0f, svgWidth, svgHeight)
            }

            svg.setDocumentWidth("100%")
            svg.setDocumentHeight("100%")

            val bitmap = createBitmap(bitmapWidth, bitmapHeight)
            val renderOptions = options.parameters.css()?.let { RenderOptions().css(it) }
            svg.renderToCanvas(Canvas(bitmap), renderOptions)

            DecodeResult(
                drawable = bitmap.toDrawable(options.context.resources),
                isSampled = true // SVGs can always be re-decoded at a higher resolution.
            )
        }


    class Factory @JvmOverloads constructor(
        val useViewBoundsAsIntrinsicSize: Boolean = true
    ) : Decoder.Factory {

        override fun create(
            result: SourceResult,
            options: Options,
            imageLoader: ImageLoader
        ): Decoder? {
            if (!isApplicable(result)) return null
            return CustomCoilSvgDecoder(result.source, options, useViewBoundsAsIntrinsicSize)
        }

        private fun isApplicable(result: SourceResult): Boolean {
            return result.mimeType == MIME_TYPE_SVG || DecodeUtils.isSvg(result.source.source())
        }

        override fun equals(other: Any?): Boolean {
            if (this === other) return true
            return other is Factory &&
                    useViewBoundsAsIntrinsicSize == other.useViewBoundsAsIntrinsicSize
        }

        override fun hashCode() = useViewBoundsAsIntrinsicSize.hashCode()
    }

    companion object {
        private const val MIME_TYPE_SVG = "image/svg+xml"
        private const val DEFAULT_SIZE = 512
    }
}
a

AndreyVanDenHaag

12/05/2023, 7:17 PM
I think this code fails because source does not exist (FileNotFoundException): val svg = source.source().use { SVG.getFromInputStream(it.inputStream()) }
z

Zokivasilic8

12/06/2023, 9:03 AM
The weird thing is that this only occasionally happens, not all the time. Could also be some multithread issue?
Also, the weird thing is that it crashes on this class, but for some reason it says that there is no Huawei class file that we need to remove because of Google policies.
Caused by: java.io.FileNotFoundException: HmsLocationProvider.java (No such file or directory)