Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout 2.0 rendering problems

After updating to ConstraintLayout 2.0 I realized that even the simplest layouts can not be previewed properly in Android Studio, both on OS X and Ubuntu.

First, my setup looks like this: I'm using "androidx.constraintlayout:constraintlayout:2.0.0", the version of Android Studio is 4.0.1 (the latest stable release) on both of my computers, the version of Android Gradle plugin is 4.0.1, the version of Gradle is 6.1.1.

Here's an example:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

I would expect it to draw the TextView aligned to the top of the screen.

What I end up seeing instead is this:

enter image description here

I tried turning off the experimental rendering engine as AS warned me there might be some issues with it but to no avail.

Is there something obvious I might be missing? Since 2.0.0 was in release for 2-3 days (and the release candidate was available for a wide audience for longer than that), I assume all obvious glitches should've been resolved at this point.

Upd. AS states there is a following rendering problem:

java.lang.NoClassDefFoundError: Could not initialize class androidx.constraintlayout.widget.ConstraintLayout$LayoutParams$Table
    at androidx.constraintlayout.widget.ConstraintLayout$LayoutParams.<init>(ConstraintLayout.java:2603)
    at androidx.constraintlayout.widget.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:1823)
    at androidx.constraintlayout.widget.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:481)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1125)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1097)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:501)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:348)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:404)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:678)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$8(RenderTask.java:809)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1604)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

Upd. 2 I've submitted a bug report, let's see if this is a bug with Android Studio / Constraint Layout indeed (which I think is not the case).

like image 692
aga Avatar asked Aug 24 '20 10:08

aga


People also ask

Is ConstraintLayout better than RelativeLayout?

ConstraintLayout has flat view hierarchy unlike other layouts, so does a better performance than relative layout. Yes, this is the biggest advantage of Constraint Layout, the only single layout can handle your UI. Where in the Relative layout you needed multiple nested layouts (LinearLayout + RelativeLayout).

What is the difference between ConstraintLayout and RelativeLayout in Android?

Unlike RelativeLayout , ConstraintLayout offers a bias value that is used to position a view in terms of 0% and 100% horizontal and vertical offset relative to the handles (marked with a red circle). These percentages (and fractions) offer seamless positioning of the view across different screen densities and sizes.

Is ConstraintLayout a ViewGroup?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread).


2 Answers

Ok, in my case this definitely looks like an issue with a project cache. Once I've cleaned it up (Build -> Clean Project), the layout preview started working just fine.

like image 53
aga Avatar answered Oct 14 '22 00:10

aga


I am having the same issue after updating to 2.0.0, I believe this is an AS/Constraint Layout issue. Using version 1.1.3, for the time being, works just fine.

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
like image 22
Alejandro Avatar answered Oct 13 '22 22:10

Alejandro