Constraint Layout. Render problems. Layout Editor

1.3k views Asked by At

I am everyday facing with very frustrating issue of ConstraintLayout in my project.

Project gradle file:

buildscript {

    ext.support_library_version = '27.0.2'
    ext.play_services_version = '11.6.2'
    ext.firebase_version = '11.6.2'

    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'io.realm:realm-gradle-plugin:4.2.0'
        classpath 'com.google.gms:google-services:3.1.2'

        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        google()
        maven { url "https://maven.google.com" }
        maven { url 'https://jitpack.io' }
    }
}

This is my short version of app gradle:

apply plugin: 'com.android.library'
apply plugin: 'realm-android'

// set to 'cmake', 'none'
def nativeBuildSystem = 'none'//'cmake'

realm {
    syncEnabled = false
}

android {
    defaultConfig {
        targetSdkVersion 27
        compileSdkVersion 27
        minSdkVersion 21
        // for using vectors for less than 21 API
        vectorDrawables.useSupportLibrary = true

        versionCode 56
        dexOptions {
            preDexLibraries true
            javaMaxHeapSize "4g"
        }

        // Enabling multidex support.
        multiDexEnabled true
        versionName "1.6.5"

        renderscriptTargetApi 2
        renderscriptSupportModeEnabled true    // Enable RS support

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

configurations {
    all*.exclude group: 'commons-logging', module: 'commons-logging'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    /*********************************  Deep Linking Library *********************************************/
    api 'com.airbnb:deeplinkdispatch:3.1.1'
    annotationProcessor 'com.airbnb:deeplinkdispatch-processor:3.1.1'

    /*********************************    Android SDK  *********************************************/
    api "com.android.support:appcompat-v7:$support_library_version"
    api 'com.android.support.constraint:constraint-layout:1.0.2'
    api "com.android.support:percent:$support_library_version"
    api "com.android.support:recyclerview-v7:$support_library_version"
    api "com.android.support:gridlayout-v7:$support_library_version"
    api "com.android.support:cardview-v7:$support_library_version"
    api "com.android.support:design:$support_library_version"
    api "com.android.support:support-vector-drawable:$support_library_version"
    api "com.android.support:support-core-utils:$support_library_version"
    api 'com.android.support:multidex:1.0.2'

}

Below what i see in Layout Editor:

enter image description here

Below my xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.constraint.ConstraintLayout>

Layout Editor in this case says:

enter image description here

And if i click on Show Exception i will see:

java.lang.NegativeArraySizeException
at android.support.constraint.R$styleable.<clinit>(Unknown Source)
at android.support.constraint.ConstraintLayout.init(ConstraintLayout.java:440)
at android.support.constraint.ConstraintLayout.<init>(ConstraintLayout.java:420)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:481)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:264)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:222)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:211)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:337)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:348)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:248)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:325)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:384)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:193)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:547)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$3(RenderTask.java:681)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

I tried a lot in order to solve it but without success. I doesn't solved using simple approaches like invalidate cache/clean project/rebuild project and etc...

2

There are 2 answers

1
chrisonline On BEST ANSWER

Problem:
It seems the Layout Editor has some troubles if you have too much attributes in one <declare-stylable> tag defined in your attr.xml!

Solution:
I have 49 attributes in one <declare-stylable> tag.
If I remove 4 attributes, so I have 45, the Layout Editor is working.

I have also added an Issue on the Google Issue Tracker:
https://issuetracker.google.com/issues/71641021

UPDATE from Google Issue:
This has been fixed for the Android Studio 3.1 release.

1
Ashish On

Do not use ConstraintLayout but rather go for Relative... or Linear.... That should work.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical">
    <EditText
        android:layout_width="match_parent" />
</LinearLayout>