Has anyone encountered issues rendering `@Preview`...
# compose
k
Has anyone encountered issues rendering
@Preview
composables in AS (currently Canary 15) with an error similar to this?
Copy code
java.lang.NoClassDefFoundError: androidx/lifecycle/runtime/R$id
	at androidx.lifecycle.ViewTreeLifecycleOwner.set(ViewTreeLifecycleOwner.java:49)
	at androidx.compose.ui.tooling.preview.ComposeViewAdapter.init(ComposeViewAdapter.kt:612)
	at androidx.compose.ui.tooling.preview.ComposeViewAdapter.<init>(ComposeViewAdapter.kt:206)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
This is only happening for some members on our team but once it does its persistent and the only way to get it to compile is add the following dependencies which doesnt seem necessary:
Copy code
androidx.savedstate:savedstate-ktx
androidx.lifecycle:lifecycle-viewmodel-compose
androidx.lifecycle:lifecycle-runtime-ktx
androidx-core-ktx
👀 2
🧵 2
👍 1
c
Do you have the full
idea.log
for this? That would be very helpful for the team. cc @nosuid
k
Paging @eschrag, can you pull your
idea.log
after you encounter this error?
e
where do i pull that from, exactly?
found where/how
k
Help -> Show Log in Finder
c
At least for macOS it’s ~/Library/Logs/Google/AndroidStudio<version>
Yeah that shortcut works really well also 🙂
e
is there any good way to sanitize these
lotta class names in here that i’d prefer not to leak
k
sed 's/voldemort/he-who-must-not-be-named/g' idea.log
?
e
when i try to refresh the preview this is the entirety of what goes into `idea.log`:
Copy code
2021-05-17 12:35:13,262 [2621923]   INFO - ild.invoker.GradleBuildInvoker - About to execute Gradle tasks: [:lib:buttons:compileDebugKotlin] 
2021-05-17 12:35:13,309 [2621970]   INFO - s.plugins.gradle.GradleManager - Instructing gradle to use java from /Users/eschra000/.sdkman/candidates/java/current 
2021-05-17 12:35:13,310 [2621971]   INFO - ild.invoker.GradleBuildInvoker - Build command line options: [--continue, -Pandroid.injected.invoked.from.ide=true, -Pandroid.injected.studio.version=2020.3.1 Canary 15, -Pandroid.injected.attribution.file.location=/Users/eschra000/dev-work/android-soft-remote/.gradle] 
2021-05-17 12:35:13,312 [2621973]   INFO - xecution.GradleExecutionHelper - Passing command-line args to Gradle Tooling API: --continue -Pandroid.injected.invoked.from.ide=true -Pandroid.injected.studio.version=2020.3.1 Canary 15 -Pandroid.injected.attribution.file.location=/Users/eschra000/dev-work/android-soft-remote/.gradle 
2021-05-17 12:35:13,344 [2622005]   INFO - s.literals.LiveLiteralsService - Editor is view only, no literal tracking will be used. 
2021-05-17 12:35:13,349 [2622010]   INFO - s.literals.LiveLiteralsService - Editor is view only, no literal tracking will be used. 
2021-05-17 12:35:15,002 [2623663]   INFO - ild.invoker.GradleBuildInvoker - Gradle build finished in 1 s 673 ms
c
If it helps, preview and stuff is all kinds of buggy for me. Try turning off live literals maybe? That crashes things a lot.
Also, if you use buildSrc I'd also tread carefully in the last canary. The last time things worked fine with preview and such for me was like canary 13
n
The lifecycle dependencies should not be needed at all. Maybe is a caching issue? Does removing them again make the preview stop working again?
e
yes
g
I can verify this in Compose 1.0.0 and AS 2020.3.1. I had to add
Copy code
implementation("androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version")
implementation("androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version")
to a *library module*'s
build.gradle
for the previews to work. Otherwise, AS reported rendering problems with stacktrace:
Copy code
java.lang.NoClassDefFoundError: androidx/lifecycle/runtime/R$id
            at androidx.lifecycle.ViewTreeLifecycleOwner.set(ViewTreeLifecycleOwner.java:49)
            at androidx.compose.ui.tooling.ComposeViewAdapter.init(ComposeViewAdapter.kt:620)
            at androidx.compose.ui.tooling.ComposeViewAdapter.<init>(ComposeViewAdapter.kt:214)
            at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
            at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
            at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
            at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:399)
            at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:189)
            at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:147)
            at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:303)
            at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:417)
            at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:428)
            at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:332)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:657)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
            at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:353)
            at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:431)
            at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
            at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:714)
            at com.android.tools.idea.rendering.RenderTask.lambda$inflate$7(RenderTask.java:870)
            at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$2.run(RenderExecutor.kt:187)
            at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
            at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
            at java.base/java.lang.Thread.run(Thread.java:834)
2
c
I’m not sure if @Preview works in library modules. @nosuid might have more insight
g
@Chris Sinco [G] Well, in my library module previews do work (after I did what I describe above). What doesn't work is "deploy preview to device", but that's a known issue (https://issuetracker.google.com/issues/160491871).
👍 2
d
Just came up against this as well; adding the two dependencies as stated by @George Kylafas fixed the issue. I'm also
@Preview
'ing in a library module - this seems a completely reasonable thing to do, I would expect Compose documentation to make it very clear if it were unsupported from Library Modules?
@George Kylafas just wondering, did you ever raise a bug ticket for this?
This is nearly a month on from the previous messages and I'm using Arctic Fox 2020.3.1 patch 1. In case it's significant this is a multiplatform project; is yours @George Kylafas?
g
@darkmoon_uk No, I haven't opened any ticket. My project is an android one
a
I just ran into this issue as well. I was able to find an issue that someone filed back in June: https://issuetracker.google.com/issues/191070658 I added a comment to the issue with what I've discovered in case it's helpful to anyone.
👍 1