Create a Project

This chapter explains the different ways to create a new project.

Note

The different project creation systems do not produce exactly the same project content and structure. Especially, the IntelliJ IDEA wizard produces a simple project whereas the Android Studio, Command Line Interface and Eclipse wizards create multi-projects builds. Both structures (single and multi projects) can be used, the recommended one depends on the context (components, size of the project, …). Refer to the official Gradle documentation for more information.

The creation of a project with Android Studio is done as follows:

  • Click on File > New > Project….
  • Select Generic > New MicroEJ project.
Project Creation in Android Studio

Project Creation in Android Studio

  • Click on the Next button.
  • Fill the name of the project in the Name field.
  • Fill the package name of the project in the Package name field.
  • Select the location of the project in the Save location field.
  • Keep the default Android SDK in the Minimum SDK field.
  • Select Kotlin for the Build configuration language field.

Note

Groovy build script DSL is not officially supported by the SDK, so the project created by the Wizard uses Kotlin regardless of the language selected by the user.

Project Creation in Android Studio

Project Creation in Android Studio

  • Click on Next button.
  • Fill the group of the artifact to publish in the Group field.
  • Fill the version of the artifact to publish in the Version field.
  • Select the module type among Application, Mock and Addon-Library in the drop-down list.
  • If you selected Application module type, you can check This is a kernel application checkbox if your Application is a Kernel.
  • Click on Finish button.
Project Creation in Android Studio

Project Creation in Android Studio

  • Change the view from Android to Project in the selectbox at the top of the project’s files tree:
Project View in Android Studio

Project View in Android Studio

Note

The newly created Gradle project uses Gradle Wrapper with Gradle version 8.2. Refer to the Gradle Wrapper section for more information.

The project created by the wizard is a multi-project with a single subproject (named app). This subproject is either an Application or an Add-On Library, depending on the module type that has been chosen.

Note

By default, Android Studio automatically saves any file change, but requires the user to explicitly trigger the reload of a Gradle project when its configuration has changed. Therefore, when the configuration of a Gradle project has been updated, you have to click on the Sync Now action which appears on the top-right of the editor:

Gradle Project reload in Android Studio

Gradle Project reload in Android Studio

You can also configure Android Studio to automatically reload a Gradle project after a change. Refer to the How To Automatically reload a Gradle project section for more information.

Warning

When reloading your Gradle project, the build can fail if the SDK EULA has not been accepted. In that case, you must set the ACCEPT_MICROEJ_SDK_EULA_V3_1B environment variable to YES and restart Android Studio. For more information about SDK EULA, refer to the Licenses chapter.

When the Gradle project has been reloaded, it should compile successfully, without any error. You can then learn how to launch the build of the project, or how to run it on the Simulator in the case of an Application.

Configure a Project

The SDK allows to build several types of modules. Each type has its own Gradle plugin and configuration options. Refer to the module type you want to build to configure your project:

Application Project

  • Add the com.microej.gradle.application plugin in the build.gradle.kts file:

    plugins {
        id("com.microej.gradle.application") version "0.15.0"
    }
    

    Note

    The java plugin must not be added since it is automatically applied by the MicroEJ plugin.

  • If your Application is a Standalone Application:

    • Create the Java main class in the src/main/java folder.

    • Define the property applicationMainClass in the microej configuration block of the build.gradle.kts file. It must be set to the Full Qualified Name of the Application main class, for example:

      microej {
        applicationMainClass = "com.mycompany.Main"
      }
      
  • If your Application is a Kernel Application:

    • Create the Java main class in the src/main/java folder.

    • Define the property applicationMainClass in the microej configuration block of the build.gradle.kts file. It must be set to the Full Qualified Name of the Application main class, for example:

      microej {
        applicationMainClass = "com.mycompany.Main"
      }
      
    • Create a file named kernel.kf in the src/main/resources folder. This file is a property file which must contain at least the version property, and optionally the name property (defaults to KERNEL), for example:

      version=1.0.0
      name=MY-KERNEL
      
  • If your Application is a Sandboxed Application:

    • Create the Java class of the Feature Entry Point in the src/main/java folder, for example:

      package com.mycompany;
      
      import ej.kf.FeatureEntryPoint;
      
      public class MyApplication implements FeatureEntryPoint {
      
        @Override
        public void start() {
          System.out.println("Feature MyApplication started!");
        }
      
        @Override
        public void stop() {
          System.out.println("Feature MyApplication stopped!");
        }
      }
      
    • Create a file with the extension .kf in the src/main/resources folder, for example MyApplication.kf. This file must at least contain the property entryPoint set to the Full Qualified Name of the Application Feature class, for example:

      entryPoint=com.mycompany.MyApplication
      version=0.1.0
      

Refer to the page Module Natures for a complete list of the available MicroEJ natures and their corresponding plugins.

Add-On Library Project

  • Add the com.microej.gradle.addon-library plugin in the build script:

    plugins {
        id("com.microej.gradle.addon-library") version "0.15.0"
    }
    

    Note

    The java plugin must not be added since it is automatically applied by the MicroEJ plugin.

Refer to the page Module Natures for a complete list of the available MicroEJ natures and their corresponding plugins.

Mock

  • Add the com.microej.gradle.mock plugin in the build script:

    plugins {
        id("com.microej.gradle.mock") version "0.15.0"
    }
    

    Note

    The java plugin must not be added since it is automatically applied by the MicroEJ plugin.

Refer to the page Module Natures for a complete list of the available MicroEJ natures and their corresponding plugins.

J2SE Library Project

  • Add the com.microej.gradle.j2se-library plugin in the build script:

    plugins {
        id("com.microej.gradle.j2se-library") version "0.15.0"
    }
    

    Note

    The java plugin must not be added since it is automatically applied by the MicroEJ plugin.

Refer to the page Module Natures for a complete list of the available MicroEJ natures and their corresponding plugins.

Create a subproject in an existing project

This section explains the different ways to add a module to an existing project.

Warning

If you want to add a MicroEJ module to a non MicroEJ project, for example an Android project, you must configure the repositories before creating the module. If the repositories used by your project are centralized in the settings.gradle.kts file of the project, the MicroEJ repositories defined in this file must be added to your settings.gradle.kts file.

The creation of a module with Android Studio is done as follows:

  • Click on File > New > New Module….
  • Select MicroEJ Module in Templates list on the left panel.
  • Fill the name of the module in the Name field.
  • Fill the group of the artifact to publish in the Group field.
  • Fill the version of the artifact to publish in the Version field.
  • Select the module type among Application and Addon-Library buttons.
  • If you selected Application module type, you can check This is a kernel application checkbox if your Application is a Kernel.
  • Click on Finish button.
Module Creation in Android Studio

Module Creation in Android Studio

Gradle Wrapper

It is recommended to use the Gradle Wrapper to execute a build. The Wrapper is a script that ensures that the required version of Gradle is downloaded and used during the build of a project.

When creating a project following one of the project creation systems described in the Create a Project section, the Wrapper files are automatically generated in the gradle/wrapper folder of the project. It is also possible to add the Wrapper to an existing project by executing the wrapper task:

gradle wrapper

The Gradle version used by the project can then be updated in the gradle/wrapper/gradle-wrapper.properties file. The SDK requires Gradle 8.0.2 or higher:

distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip

To use the Wrapper during a build, use gradlew or ./gradlew depending on your OS instead of gradle in the command line:

gradlew build

In the following chapters of the documentation, the Linux command ./gradlew is used in all examples to execute a build.

Refer to the official Gradle documentation for more information about the Wrapper.