Skip to content

Build with Gradle

Volker Berlin edited this page Oct 21, 2023 · 9 revisions

Build with Gradle

Load the compiler Gradle plugin

Until there is a stable version of JWebAssembly we recommended the snapshot version of the compiler to compile your project. Declare the repository and classpath of the plugin first in your file build.gradle.

buildscript {
    repositories {
        maven { url uri('https://jitpack.io') }
    }
    dependencies {
        classpath 'com.github.i-net-software:jwebassembly-gradle:master-SNAPSHOT'
    }
}

And apply the plugin to load it.

apply plugin: 'de.inetsoftware.jwebassembly'

This implicit apply the Java plugin. We want compile Java sources first and the compile it to the WebAssembly files.

Library Dependencies

Add the library dependencies. Here also the snapshot version of the JWebAssembly API is recommended currently. You can also add other Java libraries that you need. If there calls into libraries then the needed Java code will be compiled.

repositories {
    maven { url 'https://jitpack.io' }
}
dependencies {
    implementation 'com.github.i-net-software:jwebassembly-api:master-SNAPSHOT'
}

Wasm file name

Like in Java you can optional define a custom file name for the output:

archivesBaseName = 'HelloWorld'

Source files

Like in Java you can optional define the source files that want compile if you does not use the defaults. Resources are not supported yet.

sourceSets {
    main {
        java {
            srcDir 'src/main/java'
        }
    }
}

wasm task

The wasm files will be build from the wasm task. A complete list of all properties can you find in the source code of the wasm task. It is an extension of AbstractArchiveTask and also these properties can be used.

wasm {
  //Get more debug information if there are problems.
  //logging.level = LogLevel.DEBUG
  //logging.levelInternal = LogLevel.DEBUG

  //Change the output format to the text format *.wat. 
  //This can be helpful to understand the problems if you see the generated code as text format.
  //format = 'Text'

  //The used JWebAssembly compiler version. The default is the latest release '+'. 
  //You can set any valid Gradle version string or a full dependency string. 
  //The snapshot version is recommended until a stable release.
  //compilerVersion = 0.2
  compilerVersion = 'com.github.i-net-software:jwebassembly:master-SNAPSHOT'

  //Write method and parameter names into the output *.wasm file. 
  //The file will be approximate 10% larger. And it generate a source map. 
  //With a source map you can see in the debugger of the browser your source code if available.
  debugNames = true

  //Set an absolute or relative path between the final wasm file location and the source files location.
  ///This is needed for debugging in the browser. It work only if debugNames = true.
  //sourceMapBase = '../../src/main/java/'
  //sourceMapBase = '/src/main/java/'

  //set the destination dir
  //destinationDir = file( 'targetDir' )

  //ignore all referenced native methods without declared replacement in a library and replace them with a stub that throws an exception at runtime
  //properties[ 'IgnoreNative' ] = 'true'
}