Kotlin multiplatform inside NX workspace
Published: 2023-06-26 13:08:18

Hi, I am [nx-gradle](https://github.com/khalilou88/jnxplus) creator and I want in this article to show how you can use it with Kotlin multiplatform.

## Repo 
We will use in this article the official repo-template from JetBrains : `https://github.com/JetBrains/compose-multiplatform-template.git`

So clone this repo and let's meet in the next step.

## Install NX
Run this command to install nx : `npx nx@latest init` and choose same answers as me:
```
PS D:\Workspace\Projects\nx-compose-multiplatform> npx nx@latest init
Need to install the following packages:
  nx@16.4.0
Ok to proceed? (y) y
√ Where should your workspace be created? · false

 >  NX   Let's create a new workspace [https://nx.dev/getting-started/intro]

? Which stack do you want to use? ... 
√ Which stack do you want to use? · none
√ Package-based or integrated? · integrated
√ Enable distributed caching to make your CI faster · No

 >  NX   Creating your v16.4.0 workspace.

   To make sure the command works reliably in all environments, and that the preset is applied correctly,
   Nx will run "npm install" several times. Please wait.

√ Installing dependencies with npm
√ Successfully created the workspace: init.

 >  NX   Directory is already under version control. Skipping initialization of git.


 ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 


 >  NX   First time using Nx? Check out this interactive Nx tutorial.

   https://nx.dev/tutorials/package-based-repo-tutorial

PS D:\Workspace\Projects\nx-compose-multiplatform> 
```
## Move files from `init` folder to the root folder

By default Nx will create a `init` folder where the files will be created. Lets move this files to the root folder and merge the two `.gitignore` files


## Install nx-gradle

Now install `nx-gradle` by running this command : `npm install --save-dev @jnxplus/nx-gradle`

We will not use the init generator for `nx-gradle` to add gradle wrapper because it's already exist so we will add other required steps manually :

### Add "io.github.khalilou88.jnxplus" gradle plugin
Add "io.github.khalilou88.jnxplus" gradle plugin to the `build.gradle.kts` file

```
plugins {
    // this is necessary to avoid the plugins to be loaded multiple times
    // in each subproject's classloader
    kotlin("multiplatform").apply(false)
    id("com.android.application").apply(false)
    id("com.android.library").apply(false)
    id("org.jetbrains.compose").apply(false)

    id("io.github.khalilou88.jnxplus") version "0.2.0"
}

```

### Add plugin to nx.json
Add `nx-gradle` to nx.json so we cann run `nx graph`

```
  },
  "plugins": ["@jnxplus/nx-gradle"]
}
```

## nx graph

Now we are ready to run `nx graph` and see the connection between projects

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pehoe7bg0imnuwlbqwej.png)


## Next step 
You can now move projects to apps and libs folders and add `project.json` files so you can override executors.
You can also add linting files to the tools folder if you want to use `checkstyle` and `pmd`.


You can find code here for this article:
https://github.com/khalilou88/nx-compose-multiplatform