Lottie error: "java.lang.IllegalStateException: Unable to parse composition"
Asked Answered
H

5

5

I am trying to add LottieAnimationView to my xml layout in my project but getting this exception: "java.lang.IllegalStateException: Unable to parse composition".

java.lang.IllegalStateException: Unable to parse composition
    at com.airbnb.lottie.LottieAnimationView$1.onResult(LottieAnimationView.java:78)
    at com.airbnb.lottie.LottieAnimationView$1.onResult(LottieAnimationView.java:71)
    at com.airbnb.lottie.LottieAnimationView$3.onResult(LottieAnimationView.java:95)
    at com.airbnb.lottie.LottieAnimationView$3.onResult(LottieAnimationView.java:88)
    at com.airbnb.lottie.LottieTask.notifyFailureListeners(LottieTask.java:158)
    at com.airbnb.lottie.LottieTask.access$200(LottieTask.java:27)
    at com.airbnb.lottie.LottieTask$1.run(LottieTask.java:133)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7860)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
 Caused by: java.util.concurrent.ExecutionException: java.lang.NoClassDefFoundError: Failed resolution of: Lokio/Okio;
    at java.util.concurrent.FutureTask.report(FutureTask.java:123)
    at java.util.concurrent.FutureTask.get(FutureTask.java:193)
    at com.airbnb.lottie.LottieTask$LottieFutureTask.done(LottieTask.java:175)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
    at java.util.concurrent.FutureTask.run(FutureTask.java:271)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:919)
 Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lokio/Okio;
    at com.airbnb.lottie.LottieCompositionFactory.fromRawResSync(LottieCompositionFactory.java:265)
    at com.airbnb.lottie.LottieCompositionFactory$3.call(LottieCompositionFactory.java:234)
    at com.airbnb.lottie.LottieCompositionFactory$3.call(LottieCompositionFactory.java:229)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
    at java.lang.Thread.run(Thread.java:919) 
 Caused by: java.lang.ClassNotFoundException: Didn't find class "okio.Okio" on path: DexPathList[[zip file "/data/app/com.create.loop-D3ZAa0jgAPGefDD0MoGI3A==/base.apk"],nativeLibraryDirectories=[/data/app/com.create.loop-D3ZAa0jgAPGefDD0MoGI3A==/lib/arm, /system/lib, /vendor/lib, /system/product/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at com.airbnb.lottie.LottieCompositionFactory.fromRawResSync(LottieCompositionFactory.java:265) 
    at com.airbnb.lottie.LottieCompositionFactory$3.call(LottieCompositionFactory.java:234) 
    at com.airbnb.lottie.LottieCompositionFactory$3.call(LottieCompositionFactory.java:229) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
    at java.lang.Thread.run(Thread.java:919) 

Part of XML layout:

    <com.airbnb.lottie.LottieAnimationView
    android:id="@+id/animationView"
    android:layout_width="50dp"
    android:layout_height="50dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:lottie_autoPlay="true"
    app:lottie_loop="true"
    app:lottie_rawRes="@raw/heart_beat" />

What is interesting is that when I created a new test project and paste the above code to the newly created activity_main.xml everything works perfectly in the new project.

I tried to replase app:lottie_rawRes="@raw/heart_beat" with app:lottie_fileName="heart_beat.json" (from assets folder) but still no result. How can I resolve this issue? Thank you.

Heaver answered 18/12, 2020 at 16:11 Comment(0)
H
1

Got it. In build.gradle(Module:app) I excluded 'okio' and 'okhttp' modules for some reason. That leaded to the exception.

configurations {
    all*.exclude module: 'okhttp'
    all*.exclude module: 'okio'
}
Heaver answered 19/12, 2020 at 17:38 Comment(1)
how do you do the exclusion? I'm using Visual Studio and I'm programming under Xamarin Forms.Reversible
L
7

If you want to just avoid crash, you can set the lottie failure listener instead of default one before setting the animation.

lottieView.setFailureListener { t ->
    //do on error
}
lottieView.setAnimation(...)
Lanfri answered 24/6, 2021 at 8:15 Comment(0)
A
3

in my case this happened when the lottie json file was simply corrupted. worked fine with any other lottie files.

Antiquarian answered 3/11, 2021 at 17:28 Comment(0)
H
1

Got it. In build.gradle(Module:app) I excluded 'okio' and 'okhttp' modules for some reason. That leaded to the exception.

configurations {
    all*.exclude module: 'okhttp'
    all*.exclude module: 'okio'
}
Heaver answered 19/12, 2020 at 17:38 Comment(1)
how do you do the exclusion? I'm using Visual Studio and I'm programming under Xamarin Forms.Reversible
R
1

Look just Replace to fix it

app:lottie_rawRes="@raw/heart_beat"

with

app:lottie_fileName="heart_beat.json"

Lottie looks by default in the src/main/assets folder.

But due to that Error , maybe your code problem is about this

class "okio.Okio" 
Rojas answered 15/4, 2023 at 19:21 Comment(0)
C
0

Check what the heart_beat.json file's encoding is. Is it "UTF-8 with BOM"?

My project targets Xamarin.Android. There's an active issue since Lottie moved to AndroidX libraries. The app crashes when json file is UTF-8 with BOM encoded.

Crinose answered 18/12, 2020 at 17:2 Comment(1)
I have checked the file. The encoding is UTF-8.Heaver

© 2022 - 2024 — McMap. All rights reserved.