Jetpack Compose. Preview is not available - android

There is a project with compose.
There are a 2 modules in the project: core and othergames (core is depends on othergames).
I can successfully see a previews (annotated with #Preview) of compose views in core module. But when I try to see previews in othergames, preview can't be shown with error:
The following classes could not be instantiated:
- androidx.compose.ui.tooling.ComposeViewAdapter (Open Class, Show Exception, Clear Cache)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE. If this is an unexpected error you can also try to build the project, then manually refresh the layout.
There are details:
java.lang.NoSuchFieldError: view_tree_saved_state_registry_owner
at androidx.savedstate.ViewTreeSavedStateRegistryOwner.set(ViewTreeSavedStateRegistryOwner.java:53)
at androidx.compose.ui.tooling.ComposeViewAdapter.init(ComposeViewAdapter.kt:666)
at androidx.compose.ui.tooling.ComposeViewAdapter.<init>(ComposeViewAdapter.kt:217)
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:373)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:192)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:150)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:302)
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:965)
at android.view.LayoutInflater.inflate(LayoutInflater.java:663)
at android.view.LayoutInflater.inflate(LayoutInflater.java:505)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:361)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:436)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:121)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:727)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$7(RenderTask.java:883)
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:829)
Both of modules has a equal compose dependencies and settings in build.gradle files:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
useIR = true
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.2.0-alpha03'
}
And:
dependencies {
...
implementation "androidx.compose.ui:ui:1.2.0-alpha03"
implementation "androidx.compose.ui:ui-tooling:1.2.0-alpha03"
implementation "androidx.compose.ui:ui-tooling-preview:1.2.0-alpha03"
implementation "androidx.compose.foundation:foundation:1.2.0-alpha03"
implementation "androidx.compose.foundation:foundation-layout:1.2.0-alpha03"
implementation "androidx.compose.material:material:1.2.0-alpha03"
implementation "androidx.compose.material:material-icons-core:1.2.0-alpha03"
implementation "androidx.compose.material:material-icons-extended:1.2.0-alpha03"
implementation "androidx.compose.animation:animation:1.2.0-alpha03"
implementation "androidx.compose.animation:animation-core:1.2.0-alpha03"
implementation "androidx.compose.animation:animation-graphics:1.2.0-alpha03"
implementation "androidx.compose.runtime:runtime-livedata:1.2.0-alpha03"
implementation "androidx.compose.compiler:compiler:1.2.0-alpha03"
...
Why I can't see previews in othergames module? How to solve it?

I found a solution to fix it. In module where previews broke I added library which I removed recently:
implementation 'androidx.appcompat:appcompat:1.4.1'
Once I did it, previews appeared again. I not sure this library is real reason, because core module doesn't contains this library, but still show preview.
UPDATE:
Time passed and I ran into this problem again. Now it is not solved by adding the library I mentioned above. I still don't know what to do.

Came across the exact issue earlier. Make sure everything is up to date, this includes:
Android Studio ( Help > Check for updates)
Jetpack Compose (project level build)
Kotlin (project level build)
Other than that, the compose-tooling dependency should have the same version as that of Compose itself.
Here's what solved my issue
#Preview(showSystemUi = true, showBackground = true) // Apparently, adding these two lines seems to do the magic here
#Composable
fun MyComposable(){
...
}
Well, that should be pretty much it.
Have a look at Studio BumbleBee "Render Problem" For Compose Preview

Whenever you see a "try to build the project" error, always check the gradle implementations first.
Then, move on to whether your version numbers are correct.
Alpha versions aren't always the best choices.
So, if you can't find an error in the implementations, roll back any error associated implementation to it's previous stable release.
Then check again.
Also, maybe try and create a new android studio project and check if the core implementations in both the new and your current are similar or, at least look ok for the common ones.
If they look ok, add a comment besides all the implementations that look ok. Then focus on the other implementations.
Now, assuming your error is the same, i.e. a combination of try to build the project and is associated with compose, check all your ui based implementations and, your jetpack compose implementations if you are using those. Hope it helps.

Related

Realm error when adding Compose library to the project

I am trying to add Jetpack Compose but the app crashes at launch when installed over a version without Compose with the following error logs:
E/[Koin]: Instance creation error : could not create instance for [Factory:'AppWidgetRealmDataSource']: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Property 'MyFirstRealmTable.$stable' has been removed.
- Property 'MySecondRealmTable.$stable' has been removed.
- ...
- Property 'MyLastRealmTable.$stable' has been removed.
The app works just fine when it is installed for the first time.
The exception is thrown only if a previous version of the application without Compose was already installed.
Of course there were no changes in Realm tables in the version with Compose. So no Realm migration should be expected here.
The only thing I did is adding the Compose dependencies as stated in the Android docs :
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.3.0'
}
dependencies {
implementation 'androidx.activity:activity-compose:1.5.1'
implementation 'androidx.compose.material:material:1.2.1'
implementation 'androidx.compose.animation:animation:1.2.1'
implementation 'androidx.compose.ui:ui-tooling:1.2.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1'
androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.2.1'
}
There is no error at compile time, only at run time.
Furthermore, when I remove compose true the app does not crash.
I really do not know what I am doing wrong and why a Realm error is thrown when I add Compose as the 2 libraries are not related at all.
I am using this for Realm: io.realm:realm-gradle-plugin:10.10.1.
Thanks

Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?

I'm trying to migrate some Java library from 'normal' JVM to android and stuck with some Java11 APIs used in the code.
The first thing I already got - Java11 language features seems to work only with Canary build of Android Studio, see answer here
Now I need to get understanding about which APIs can be really used. Here are two use-cases which do not work for me and I can't get if I'm doing something wrong or it never should work:
List.copyOf() - introduced in Java11, method copyOf is not available on android. Methods 'List.of()', introduced with Java 9, work OK.
class java.lang.invoke.LambdaMetafactory - introduced with Java 1.8 - to be used for programmatic creation of lambdas for usage instead for reflection, is not visible on Android.
I see both of them in sources of desugar_jdk_libs here:
https://github.com/google/desugar_jdk_libs/blob/master/jdk11/src/java.base/share/classes/java/lang/invoke/LambdaMetafactory.java
https://github.com/google/desugar_jdk_libs/blob/master/src/share/classes/java/util/List.java
So - the question is: how can I identify if some Java API is supposed to be available in 'desugared' android build or no? What really can be expected from 'desugaring'?
Steps to reproduce:
Using Android Studio Canary generate a dummy "Basic Activity" project
Make sure following is provided in build.gradle
android {
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}
dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}
Add following lines somewhere in code
List<Integer> ints1 = List.of(1, 2, 3);
Supplier<List<Object>> listSupplier = () -> new ArrayList<>();
List<Object> alist = listSupplier.get();
List<Integer> ints2 = List.copyOf(ints1);
LambdaMetafactory.metafactory(null,null,null,null,null,null);
Last 2 lines fail to compile for me.
PS: final application is supposed to work on Android 10+.
Contrary to the other answer, desugaring is totally possible.
The dependency to add is
dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}
You can find more information at the official Android Java 8 desugaring documentation.
Desugaring lib is considered unofficial. We can't expect an exact answer. We get the feature when it is ready. Now List.copyOf() method now working with the latest Gradle version.
About the LambdaMetafactory class, It is not included in Android Javadoc. This means we assume we don't have LambdaMetafactory at all. Google stripped down some java API for being lightweight.
In general, We should check android Javadoc first. If android Javadoc has no mention about some API. We can be sure we won't get that feature anytime soon.

Upgrading to Jetpack Compose Alpha 12 causes errors on setContent

I upgraded to Jetpack Compose 1.0.0-alpha12 and started to run into issues.
Firstly, the setContent method I was using showed as deprecated.
From the Alpha 12 release notes, I noticed that it said:
ComponentActivity.setContent has moved to androidx.activity.compose.setContent in the androidx.activity:activity-compose module. (Icf416)
So I removed my import androidx.compose.ui.platform.setContent and switched it to import androidx.activity.compose.setContent, which removed the deprecation.
However, I then got an error that says:
w: Flag is not supported by this version of the compiler: -Xallow-jvm-ir-dependencies
w: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+NonParenthesizedAnnotationsOnFunctionalTypes
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
e: Classes compiled by an unstable version of the Kotlin compiler were found in dependencies.
Remove them from the classpath or use '-Xallow-unstable-dependencies' to suppress errors
e: /[my path]/MainActivity.kt: (39, 9): Class 'androidx.activity.compose.ComponentActivityKt' is
compiled by an unstable version of the Kotlin compiler and cannot be loaded by this compiler
And again, I was able to work around that by changing my build.gradle file to have:
kotlinOptions {
jvmTarget = '1.8'
useIR = true
// I added this line
freeCompilerArgs += "-Xallow-unstable-dependencies"
}
While that let me compile my app, I now get the following exception at runtime:
java.lang.NoSuchMethodError: No static method setContent(
Landroidx/activity/ComponentActivity;Landroidx/compose/runtime/Com
positionContext;Lkotlin/jvm/functions/Function2;)V in class
Landroidx/activity/compose/ComponentActivityKt; or its super classes
(declaration of 'androidx.activity.compose.ComponentActivityKt' appears in [my apk]
How can I fix this and upgrade my app to Jetpack Compose 1.0.0-alpha12?
As per this issue, this issue is related to the new androidx.activity:activity-compose:1.3.0-alpha01 artifact.
From that issue:
Activity 1.3.0-alpha02 has been released and fixes this issue.
Apps using Compose alpha12 and specifically artifacts like androidx.compose.ui:ui-test-junit4:1.0.0-alpha12 that internally use setContent should add the activity-compose:1.3.0-alpha02 dependency to their dependencies block to ensure that the 1.3.0-alpha01 artifact is not used
So to fix your app, you should:
Remove the freeCompilerArgs += "-Xallow-unstable-dependencies" line from the build.gradle file (as it is no longer needed)
Add a specific dependency on Activity Compose 1.3.0-alpha02:
implementation 'androidx.activity:activity-compose:1.3.0-alpha02'
By adding that dependency, any direct usages of setContent as well as internal usages by androidx.compose.ui:ui-tooling:1.0.0-alpha12 or androidx.compose.ui:ui-test-junit4:1.0.0-alpha12 will use the fixed Activity Compose 1.3.0-alpha02 release.
With Activity 1.3.0-alpha02, setContent is working, but got another error.
Execution failed for task ':app:mergeDebugJavaResource'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
> 2 files found with path 'META-INF/AL2.0' from inputs:
Had to use the workaround to make it built
packagingOptions {
exclude 'META-INF/AL2.0'
exclude 'META-INF/LGPL2.1'
}
And still have the warning
Flag is not supported by this version of the compiler: -Xallow-jvm-ir-dependencies

How to get lifecycle.coroutineScope with new androidx.lifecycle:*:2.2.0-alpha01

On 7th May, 2019 androidx.lifecycle:*:2.2.0-alpha01 was released announcing:
This release adds new features that adds support for Kotlin coroutines for Lifecycle and LiveData. Detailed documentation on them can be found here.
On documentation it's mentioned that I can get the LifecycleScope:
either via lifecycle.coroutineScope or lifecycleOwner.lifecycleScope properties
But it seems that I'm not able to find none of them. My current dependencises are:
def lifecycle_ver = "2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_ver"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_ver"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_ver"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1'
What may be the cause and how do get these apis?
I actually spent a couple hours trying to figure this out myself and it turns out it is in a new package that only exists as of the alpha. Add this and you should be good to go.
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_ver"
The accepted answers is working, but I'm misused for the first time, so I'm trying to make it clear, the current version of lifecycle is "2.1.0" and lifecycleScope, and ViewModelScope is not available in this version, to get them use
For ViewModelScope,
use androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-beta01 or higher.
For LifecycleScope,
use androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha01 or higher.
at this time 2.4.1 is available.
I've just added this two lines of code to my build.gradle (App) and that worked like a charm. I can import lifecycleScope without any problems.
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.2'

Room annotation processor with Data binding

I have used Data binding in my existing code and now I am migrating to Room for persistence.
I have followed the steps mentioned in Florina's Blog for room
My Code builds fine without java code error or BR related error when I remove room dependency
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
and its runs too, but gives Runtime exception saying database_Impl does not exists. As it couldn't generate that's file.
But after I put Annotation processor back, it give me
Error:(29, 37) error: cannot find symbol class BR
My gradle plugin used is com.android.tools.build:gradle:3.0.1
They both don't seem to work together
Steps taken so far:
Changed BaseObservable to Observable As suggested here
Updated Android Studio to 3.0.1
Tried using gradle latest plugin canary 6
Clear, Clear Cache also done
Has anyone used Room and Data binding together ?
After 4 days of efforts I finally made my code run properly.
Steps to solve the
Data binding error like
error: package com.packagename.databinding does not exist
error: cannot find symbol class CustomMainActivityBinding
The app gradle must have below code added in order to view more than 100 errors that come by default
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xmaxerrs" << "4000"
options.compilerArgs << "-Xmaxwarns" << "4000"
}
}
}
Gradle dependencies for data binding and Room arch components
annotationProcessor 'com.android.databinding:compiler:3.0.1'
implementation 'android.arch.lifecycle:extensions:1.0.0'
implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
Note: Gradle plugin version is 3.0.1
I changed my all VMs to implement Observable and call
registry.notifyChange(this, BR.bar);
in case of notify change and also implement overridden methods
#Override
public void addOnPropertyChangedCallback(OnPropertyChangedCallback
callback) {
registry.add(callback);
}
#Override
public void removeOnPropertyChangedCallback(
OnPropertyChangedCallback callback) {
registry.remove(callback);
}
These things made my code Build, but it run without exceptions when I solved the Room query related errors. Which was the main reason, code was building but not running. These errors I could see when I Rebuid my project again.
UPDATE:
After Android studio 3.1.3, Message window is gone and now all build error appears under Build view. Although there is toggle available to get textview response of error, for data-binding errors it isn't sufficient.
Solution that helped me:
In Command promt/Terminal navigate to project root.
Run this command "./gradlew build --stacktrace" if Mac or ".\gradlew
build --stacktrace" if Windows.
Now search for "error:" tag and the compile time errors will show up.
I couldn't get these errors in IDE.
In my experience, the following reasons may cause the error:
Incorrect getter/setter (missing or incorrect name)
Incorrect return type in Dao
annotationProcessor or kapt issues
The default constructor is overridden (you need to keep the empty constructor for Room)
I faced this issue while adding room dependencies. Add this in the below way to resolve the error.
def roomVersion = "2.0.0-rc01"
implementation "android.arch.persistence.room:runtime:$roomVersion"
annotationProcessor "android.arch.persistence.room:runtime:$roomVersion"
annotationProcessor "android.arch.persistence.room:compiler:$roomVersion"

Categories

Resources