Universal Robots Forum

Developing URCaps with Kotlin

We have been using Kotlin for development of our URCap since the spring of 2020, and have both been a lot more productive as well as really enjoying it. So I though I would add a post explaining how and why we did it.

Firstly, Kotlin is a language created by Jetbrains, an organization famous for its IDE’s. Kotlin is 100 % compatible with Java 1.6, which is the Java version used for developing URCaps. With Kotlin we can therefore develop with a modern language, while maintaining compatibility with old code. Being 100 % compatible also means that there is no entry threshold to start. We do not need to replace all our old code, but can incrementally add Kotlin code as all new code.

Since Kotlin is created by Jetbrains, it comes as no surprise that Kotlin is best supported in Jetbrains IntelliJ IDE. The community edition is all that is needed, which is open source and free for commercial use.

Getting started:
To get started, we just need to update our pom file. Here is a snippet of the changes needed:

  • In the properties tag add:

    <kotlin.version>1.3.61</kotlin.version>
    <kotlin.compiler.incremental>true</kotlin.compiler.incremental>
    
  • In the plugins tag add this plugin (Yes, the indentation is bad…):

    	<plugin>
          <groupId>org.jetbrains.kotlin</groupId>
          <artifactId>kotlin-maven-plugin</artifactId>
          <version>${kotlin.version}</version>
          <executions>
              <execution>
                  <id>compile</id>
                  <goals> <goal>compile</goal> </goals>
                  <configuration>
                      <sourceDirs>
                          <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                          <sourceDir>${project.basedir}/src/main/java</sourceDir>
                      </sourceDirs>
                  </configuration>
              </execution>
              <execution>
                  <id>test-compile</id>
                  <goals> <goal>test-compile</goal> </goals>
                  <configuration>
                      <sourceDirs>
                          <sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
                          <sourceDir>${project.basedir}/src/test/java</sourceDir>
                      </sourceDirs>
                  </configuration>
              </execution>
          </executions>
      </plugin>
    
  • In the org.apache.maven.plugins plugin, add

    <executions>
              <!-- Replacing default-compile as it is treated specially by maven -->
              <execution>
                  <id>default-compile</id>
                  <phase>none</phase>
              </execution>
              <!-- Replacing default-testCompile as it is treated specially by maven -->
              <execution>
                  <id>default-testCompile</id>
                  <phase>none</phase>
              </execution>
              <execution>
                  <id>java-compile</id>
                  <phase>compile</phase>
                  <goals> <goal>compile</goal> </goals>
              </execution>
              <execution>
                  <id>java-test-compile</id>
                  <phase>test-compile</phase>
                  <goals> <goal>testCompile</goal> </goals>
              </execution>
          </executions>
    
  • This is a snippet of the section for importing packages to our URCap, where the key is Embed-Dependency:

    <plugin>
      		<groupId>org.apache.felix</groupId>
      		<artifactId>maven-bundle-plugin</artifactId>
      		<version>2.4.0</version>
      		<extensions>true</extensions>
      		<executions>
      			<execution>
      				<id>bundle-manifest</id>
      				<phase>process-classes</phase>
      					<goals>
      						<goal>manifest</goal>
      					</goals>
      			</execution>
      		</executions>
      		<configuration>
      			<instructions>
      				<!--********** DO NOT MODIFY THE ENTRIES OF THIS SECTION **********-->
      				<Bundle-Category>URCap</Bundle-Category>
      				<Bundle-Activator>....Activator</Bundle-Activator>
      				<Bundle-Vendor>${urcap.vendor}</Bundle-Vendor>
      				<Bundle-ContactAddress>${urcap.contactAddress}</Bundle-ContactAddress>
      				<Bundle-Copyright>${urcap.copyright}</Bundle-Copyright>
      				<Bundle-LicenseType>${urcap.licenseType}</Bundle-LicenseType>
      				<Bundle-Description>${urcap.description}</Bundle-Description>
      				<!--***************************************************************-->
      				<Import-Package>
      					com.ur.urcap.api*,
      					*
      				</Import-Package>
      				<Embed-Dependency>kotlin-stdlib</Embed-Dependency>
      			</instructions>
      		</configuration>
      	</plugin>
    
  • Lastly, under dependencies, add this dependency:

    <dependency>
      	<groupId>org.jetbrains.kotlin</groupId>
      	<artifactId>kotlin-stdlib</artifactId>
      	<version>${kotlin.version}</version>
      </dependency>
    

That’s it!

Now go and download IntelliJ, read the Getting started docs and use ctrl+alt+shift+k to convert a Java file to Kotlin :slight_smile:

Good luck!

4 Likes