Mobile Development 17 min read

Introduction to Yoga Layout Engine, Flexbox Basics, and Android Integration

Yoga, Facebook’s cross‑platform C‑based Flexbox layout engine with Java, C#, and Objective‑C bindings, lets developers define UI hierarchies using Flexbox properties, compile it via Buck or import a Gradle module, calculate node positions in Android, and manually apply them to Views, offering cross‑platform consistency but requiring extra integration effort.

Tencent Music Tech Team
Tencent Music Tech Team
Tencent Music Tech Team
Introduction to Yoga Layout Engine, Flexbox Basics, and Android Integration

Yoga is a cross‑platform layout engine originally introduced by Facebook in React Native. It implements the Flexbox specification and is written in C, offering bindings for Java, C#, Objective‑C and C. Yoga can be used with popular frameworks such as React Native.

Key Features of Yoga

Fully compatible with the Flexbox layout model and follows the W3C specification.

Supports Java, C#, Objective‑C and C languages.

Core is written in C, providing high performance and easy integration with other platforms.

Works with frameworks like React Native.

Flexbox Overview

Since 2009 the W3C has defined Flexbox, a responsive layout model now supported by all browsers. A container set to display: flex enables its children to be laid out using Flexbox properties; however, float , clear and vertical-align no longer apply.

Example of declaring a flex container:

.box { display: flex; }

Flexbox Basic Syntax

Yoga uses the same property names as Flexbox; the API differs but the concepts are identical. Detailed Flexbox documentation can be found on the Yoga website.

Yoga Extensions

Aspect Ratio

Yoga adds an aspectRatio property (a positive float) useful for media elements where one dimension and the ratio are known.

Depends on the item’s minimum and maximum size.

Has higher priority than flexGrow .

If aspectRatio , width and height are all defined, the cross‑axis size is overridden.

RTL (Right‑to‑Left) Layout

Yoga supports RTL by providing start and end values for margin, padding and border, which reverse when the layout direction is RTL.

Setting Up Yoga for Android Development

Method 1 – Build with Buck

Install Buck following the instructions on buckbuild.com .

Clone the Yoga repository from GitHub and compile the Java sources with Buck: buck build //java:java && buck build //java:jni

After compilation, the buck‑out directory contains JAR files but not the native .so libraries. A workaround described at jianshu.com suggests building with: buck build //java:jni#android-armv7,shared – however this flavor is not recognized by Buck, leading to a build failure.

Method 2 – Import an Existing Yoga Module

Clone the sample project AndYogaSample and import its yoga module into a new Android project via “Import Gradle Project”.

Add the module as a dependency (e.g., compile project(':yoga') ) and sync Gradle.

Use Yoga’s Java API in the new project.

The imported module contains two JARs ( jsr305.jar and soloader-0.1.0.jar ) and the native .so libraries generated from the original repository.

Using Yoga in Android

Creating Nodes and Setting Properties

YogaNode root = new YogaNode();
YogaNode image1 = new YogaNode();
YogaNode image2 = new YogaNode();

Typical property settings:

root.setWidth(getWindowManager().getDefaultDisplay().getWidth() / 2);
root.setHeight(getWindowManager().getDefaultDisplay().getWidth() / 2);
root.setJustifyContent(YogaJustify.SPACE_BETWEEN);

image1.setHeight(60 * getResources().getDisplayMetrics().density);
image1.setWidth(60 * getResources().getDisplayMetrics().density);
image1.setMargin(YogaEdge.ALL, 15 * getResources().getDisplayMetrics().density);

image2.setHeight(60 * getResources().getDisplayMetrics().density);
image2.setWidth(60 * getResources().getDisplayMetrics().density);
image2.setMargin(YogaEdge.ALL, 15 * getResources().getDisplayMetrics().density);
image2.setAlignSelf(YogaAlign.FLEX_END);

After building the node tree, call:

root.calculateLayout();

Layout results (position and size) are retrieved via getLayoutX() , getLayoutY() , etc., and then applied to Android Views manually:

ImageView imageView1 = new ImageView(this);
imageView1.setImageResource(R.drawable.little_point);
imageView1.setX(image1.getLayoutX());
imageView1.setY(image1.getLayoutY());
layout.addView(imageView1);

ImageView imageView2 = new ImageView(this);
imageView2.setImageResource(R.drawable.little_point);
imageView2.setX(image2.getLayoutX());
imageView2.setY(image2.getLayoutY());
layout.addView(imageView2);

Full Demo Example

The article provides a complete demo that builds a hierarchy of Yoga nodes (rows, images) and then creates corresponding ImageView objects positioned according to the calculated layout. The source code is available at GitHub .

Summary

Advantages : Write layout once and let Yoga compute positions for multiple platforms.

Disadvantages : Yoga’s Android API only returns positional data; developers must manually apply it to native views. Integrating Yoga requires replacing existing XML layouts, which is invasive. Some layouts achievable with XML cannot be expressed in Yoga.

Overall, Yoga offers a promising cross‑platform layout concept but still lacks mature Android integration.

FAQ (Compilation Issues)

Missing files in buck‑out : Remove test‑related Buck files from the Java directory before building.

Undefined NDK_HOME or SDK_HOME : Create a local.properties file with sdk.dir or set ANDROID_HOME / ANDROID_SDK environment variables.

“No Android platform target specified” error: Install the required Android platform via the SDK manager or specify an existing add‑on target.

JavaCross-PlatformlayoutAndroidBuckflexboxYoga
Tencent Music Tech Team
Written by

Tencent Music Tech Team

Public account of Tencent Music's development team, focusing on technology sharing and communication.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.