constraint-layout lib update from 1.0.2 to 1.1.0 g

2019-03-16 07:29发布

问题:

I currently using constraint-layout by the following lib

implementation 'com.android.support.constraint:constraint-layout:1.0.2'

By update warning in the android studio, I Update this to

implementation 'com.android.support.constraint:constraint-layout:1.1.0'

After that whenever I try to build or run the project I got the following issue (See stack trace)

java.lang.AssertionError: TOP
    at android.support.constraint.solver.widgets.Guideline.getAnchor(Guideline.java:159)
    at android.support.constraint.solver.widgets.ConstraintWidget.immediateConnect(ConstraintWidget.java:1579)
    at android.support.constraint.ConstraintLayout.setChildrenConstraints(ConstraintLayout.java:1012)
    at android.support.constraint.ConstraintLayout.updateHierarchy(ConstraintLayout.java:793)
    at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1540)
    at android.view.View.measure(View.java:18804)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.view.View.measure(View.java:18804)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
    at android.view.View.measure(View.java:18804)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
    at android.view.View.measure(View.java:18804)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2112)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1228)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1464)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
    at android.view.Choreographer.doCallbacks(Choreographer.java:670)
    at android.view.Choreographer.doFrame(Choreographer.java:606)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
    at android.os.Handler.handleCallback(Handler.java:746)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5443)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Also got the crashlytics issue as,

E/CrashlyticsCore: Failed to execute task.
java.util.concurrent.TimeoutException
    at java.util.concurrent.FutureTask.get(FutureTask.java:177)
    at com.crashlytics.android.core.CrashlyticsBackgroundWorker.submitAndWait(CrashlyticsBackgroundWorker.java:41)
    at com.crashlytics.android.core.CrashlyticsController.handleUncaughtException(CrashlyticsController.java:320)
    at com.crashlytics.android.core.CrashlyticsController$6.onUncaughtException(CrashlyticsController.java:300)
    at com.crashlytics.android.core.CrashlyticsUncaughtExceptionHandler.uncaughtException(CrashlyticsUncaughtExceptionHandler.java:42)
    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

If I revert this back to 1.0.2 version work fine. Is this a noticed bug or not?. How to fix this?

回答1:

We just got the same issue, the problem is that the new version is adding some extra assertions, thus old code might stop working, because of the way the constraint layout works, the stack trace is not intuitive at all.

    public ConstraintAnchor getAnchor(Type anchorType) {
    switch(anchorType) {
    case LEFT:
    case RIGHT:
        if (this.mOrientation == 1) {
            return this.mAnchor;
        }
        break;
    case TOP:
    case BOTTOM:
        if (this.mOrientation == 0) {
            return this.mAnchor;
        }
        break;
    case BASELINE:
    case CENTER:
    case CENTER_X:
    case CENTER_Y:
    case NONE:
        return null;
    }

    throw new AssertionError(anchorType.name());
}

This is the method that causes the exception. What is checking is that if the Guideline is a vertical one, any item that adds a constraint to it should do it horizontal (start or end) and the other way around for vertical guidelines.

In our case, we had a layout using

app:layout_constraintTop_toBottomOf="@+id/guideline"
app:layout_constraintStart_toStartOf="@+id/guideline"

Check your layouts and make sure you add the proper constraint to the guideline.



回答2:

Just add android:orientation="horizontal" or android:orientation="vertical" to your Guideline