Skip to content

Generating Optional Bindings

The GIR bindings generator allows you to generate Kotlin/Native bindings for a list of default libraries. However, you can also generate bindings for additional libraries by editing the gtkkn.json file.

Editing the gtkkn.json File

The gtkkn.json file serves as a configuration file for both the GIR bindings generator and the Gradle project. It specifies which libraries should have bindings generated for them. Additionally, the file is also used to configure and enable additional modules in the Gradle build. Below is an example of the default gtkkn.json file:”

Click to expand/collapse
gtkkn.json
{
  "girBaseDir": "/usr/share/gir-1.0",
  "outputDir": "bindings",
  "logLevel": "INFO",
  "skipFormat": false,
  "bindingLicense": "LGPL",
  "libraries": [
    {
      "name": "cairo",
      "module": "core:cairo",
      "girPrefix": "cairo-"
    },
    {
      "name": "gdkpixbuf",
      "module": "core:gdkpixbuf",
      "girPrefix": "GdkPixbuf-"
    },
    {
      "name": "gio",
      "module": "core:gio",
      "girPrefix": "Gio-"
    },
    {
      "name": "glib",
      "module": "core:glib",
      "girPrefix": "GLib-"
    },
    {
      "name": "gmodule",
      "module": "core:gmodule",
      "girPrefix": "GModule-"
    },
    {
      "name": "gobject",
      "module": "core:gobject",
      "girPrefix": "GObject-"
    },
    {
      "name": "graphene",
      "module": "core:graphene",
      "girPrefix": "Graphene-"
    },
    {
      "name": "harfbuzz",
      "module": "core:harfbuzz",
      "girPrefix": "HarfBuzz-"
    },
    {
      "name": "pango",
      "module": "core:pango",
      "girPrefix": "Pango-"
    },
    {
      "name": "pangocairo",
      "module": "core:pangocairo",
      "girPrefix": "PangoCairo-"
    },
    {
      "name": "gdk",
      "module": "gtk:gdk4",
      "girPrefix": "Gdk-4"
    },
    {
      "name": "gsk",
      "module": "gtk:gsk4",
      "girPrefix": "Gsk-"
    },
    {
      "name": "gtk",
      "module": "gtk:gtk4",
      "girPrefix": "Gtk-4"
    },
    {
      "name": "adw",
      "module": "adwaita",
      "girPrefix": "Adw-1"
    }
  ],
  "ignoredLibraries": [
    {
      "name": "gtksource",
      "module": "extra:gtksource",
      "girPrefix": "GtkSource-5"
    },
    {
      "name": "he",
      "module": "extra:libhelium",
      "girPrefix": "He-"
    }
  ]
}

The libraries list contains the names of the libraries that you want to generate bindings for, along with some additional metadata:

  • name: The name of the library (e.g. glib).
  • module: The module that the library belongs to, in Gradle format, that will be generated inside outputDir (e.g. core:glib).
  • girPrefix: The prefix used in the GIR file for the library (e.g. GLib-).

The ignoredLibraries list contains optional libraries that the GIR generator supports but that are not generated by default.

To enable an optional library, you can move it from the ignoredLibraries list to the libraries list.

Using a Custom Configuration File

You can also use a custom configuration file instead of the default gtkkn.json file. To do this, define a new Gradle property called org.gtkkn.configFile containing the path to your custom config file. You can pass this property via the command line directly to Gradle using -Dorg.gtkkn.configFile=<path to config file>. Alternatively, you can permanently set it in your ~/.gradle/gradle.properties file.

For example, to use a custom configuration file located at /path/to/my/config.json, you can run the following command:

./gradlew gir:run -Dorg.gtkkn.configFile=/path/to/my/config.json

or, for the ~/.gradle/gradle.properties:

org.gtkkn.configFile=/path/to/my/config.json