⌘K

Nativeblocks Compiler and Gradle Plugin

The Nativeblocks Compiler and Gradle Plugin are powerful tools designed to streamline the development of server-driven, reusable blocks and actions for Android applications. By leveraging structured annotations and Gradle tasks, these tools simplify complex workflows, reduce boilerplate, and enhance developer productivity.


Key Features

Nativeblocks Compiler

  • Annotations for Blocks and Actions:
    • Use annotations like @NativeBlock and @NativeAction to define reusable components with minimal effort.
  • Generated Code and JSON:
    • Automatically generates Kotlin classes and JSON files for seamless integration.
  • Integration with Nativeblocks Studio:
    • Synchronize your blocks and actions directly with Nativeblocks Studio for centralized management.

Nativeblocks Gradle Plugin

  • Sync JSON to Studio:
    • Upload generated JSON files to Nativeblocks Studio with a single command.
  • Prepare JSON Schemas:
    • Generate schemas for use with Nativeblocks DSL and CLI.
  • Automation:
    • Integrate tasks into your build process for efficient development.

Getting Started

1. Install the Nativeblocks Compiler

Add the Nativeblocks Compiler to your build.gradle file:

implementation("io.nativeblocks:nativeblocks-compiler-android:1.1.0")
ksp("io.nativeblocks:nativeblocks-compiler-android:1.1.0")

Configure the KSP arguments:

ksp {
    arg("basePackageName", "com.example.app")
    arg("moduleName", "MyModule")
}

2. Install the Nativeblocks Gradle Plugin

Add the plugin dependency to your build.gradle file:

plugins {
    id("io.nativeblocks.nativeblocks-gradle-plugin").version("1.1.0")
}

Configure the plugin using a nativeblocks.properties file for your project:

endpoint=
authToken=
organizationId=

Load these properties in your Gradle configuration:

def nativeblocksProps = new Properties()
file("nativeblocks.properties").withInputStream { props.load(it) }

nativeblocks {
    endpoint = nativeblocksProps.getProperty("endpoint").toString()
    authToken = nativeblocksProps.getProperty("authToken").toString()
    organizationId = nativeblocksProps.getProperty("organizationId").toString()
    basePackageName = "com.example.app"
    moduleName = "MyModule"
}

Define Blocks and Actions

Define a Block

Use the @NativeBlock annotation to create a reusable block:

@NativeBlock(name = "Custom Button", keyType = "CUSTOM_BUTTON", description = "A reusable button block")
@Composable
fun CustomButton(
    @NativeBlockData(description = "Button text") text: String,
    @NativeBlockProp(
        description = "Button size",
        valuePicker = NativeBlockValuePicker.DROPDOWN,
        valuePickerOptions = [
            NativeBlockValuePickerOption("S", "Small"),
            NativeBlockValuePickerOption("M", "Medium"),
            NativeBlockValuePickerOption("L", "Large")
        ]
    ) size: String = "M",
    @NativeBlockEvent(description = "Triggered when the button is clicked") onClick: () -> Unit
) {
    Button(onClick = onClick) {
        Text(text)
    }
}

Define an Action

Use the @NativeAction annotation to create a reusable action:

@NativeAction(name = "Show Alert", keyType = "SHOW_ALERT", description = "Displays an alert with a message")
class ShowAlert {

    @NativeActionParameter
    data class Parameters(
        @NativeActionData(description = "Alert message") val message: String
    )

    @NativeActionFunction
    suspend fun execute(parameters: Parameters) {
        println("Alert: ${parameters.message}")
    }
}

Use the Gradle Plugin Tasks

1. Sync Task

Upload JSON files to Nativeblocks Studio:

./gradlew :app:nativeblocksSyncDebug

2. PrepareSchema Task

Generate JSON schemas for blocks and actions:

./gradlew :app:nativeblocksPrepareSchemaDebug

Workflow Integration

1. Define Components

Use the @NativeBlock and @NativeAction annotations to define your blocks and actions.

2. Generate and Upload

Run Gradle tasks to generate files and synchronize them with Nativeblocks Studio.

3. Automate

Incorporate the Gradle plugin tasks into your CI/CD pipeline for a streamlined workflow.