What is the web3j-solidity-gradle plugin?
The web3j-solidity-gradle-plugin is a useful extension of the web3j library, designed to facilitate the integration of Solidity smart contract compilation into your Gradle project. It streamlines the process of constructing and managing smart contracts within a Java or Android project utilizing Web3j. The plugin provides various configurations for compilation and uses web3j-sokt in the background.
Web3j-Sokt is a Kotlin wrapper for the Solidity compiler (solc). It analyzes Solidity files, determining the appropriate compiler version based on the pragma statement at the file’s beginning. It can then download, install, and invoke the compiler. Unlike Dockerized versions of Solc, Sokt relies on native builds, supporting compatibility across Mac, Windows, and Linux platforms.
For additional details on web3j-sokt, visit: https://github.com/hyperledger/web3j-sokt
How to utilize the web3j-solidity-gradle plugin?
Step 1: Apply the Plugin
Begin by applying the web3j-solidity-gradle plugin to your Gradle project. You can accomplish this by incorporating the following lines into your build.gradle file:
plugins {
id 'org.web3j.solidity.gradle.plugin' version 'x.y.z'
}
Step 2: Configure the Plugin
Once the plugin is applied, you can configure the compilation parameters. Below is a sample configuration:
solidity {
// Optional: Specify the Solidity compiler version
solcVersion = 'v0.5.16'
// Optional: Configure the optimizer
outputComponents = [BIN, ABI, ASM_JSON]
optimizeRuns = 500
}
Step 3: Include Solidity Files
Naturally, all .sol files located in $projectDir/src/main/solidity and $projectDir/src/test/solidity will be handled by the plugin. To define and add alternative source sets, utilize the sourceSets DSL. It’s also possible to establish your desired output directory for the compiled code.
sourceSets {
main {
solidity {
srcDir {
"my/custom/path/to/solidity"
}
output.resourcesDir = file('out/bin/compiledSol')
}
}
}
Step 4: Compile Contracts
To compile your Solidity contracts, execute the following command in your terminal:
./gradlew build
Alternatively, you may run the compileSolidity gradle task directly:
./gradlew compileSolidity
This command initiates the compilation of Solidity contracts, generating binary and ABI files in the specified output directory.
Step 5: Integrate with Web3j
Once your contracts are compiled, you may wish to create Java wrappers for interaction. You can include the web3j library in your project and leverage the Web3j CLI or Web3j Gradle plugin to generate these wrappers. Here’s a guide on how to add the Web3j-gradle-plugin to your Gradle project: https://docs.web3j.io/4.11.0/plugins/web3j_gradle_plugin/
You can then utilize the plugin to generate Java wrappers corresponding to your smart contracts.
Step 6: Execute and Test
Ultimately, employ the generated Java wrappers in your Java or Android project to deploy, interact with, and test your smart contracts.
Introducing New Features in the Web3j-Solidity-Gradle Plugin
The Web3j Solidity Gradle plugin supports defining multiple source sets. However, the Solidity compilation configuration remains global, meaning it cannot be uniquely set per source set.
Below is an example of how source sets along with Solidity flags are currently set up:
sourceSets {
main {
solidity {
srcDir {
"my/custom/path/to/solidity"
}
output.resourcesDir = file('out/bin/compiledSol')
}
}
}
solidity {
outputComponents = [BIN, ABI, ASM_JSON]
optimizeRuns = 500
}
This setup can present challenges for users seeking distinct configurations for compiling smart contracts across multiple source sets since compilation settings are restricted to global values.
To tackle this issue, we’ve rolled out new features that allow users to configure parameters such as Solidity version, EVM version, optimization settings, and optimization runs on a per-source-set basis.
An example utilizing the new functionality:
sourceSets {
main {
solidity {
srcDir {
"my/custom/path/to/solidity"
}
output.resourcesDir = file('out/bin/compiledSol')
setEvmVersion('ISTANBUL')
setOptimize(true)
setOptimizeRuns(200)
setVersion('0.8.12')
}
}
}
Developers interested in exploring this new feature set can follow the related pull request: https://github.com/hyperledger/web3j-solidity-gradle-plugin/pull/69. These capabilities will be presented in the anticipated release of the Web3j-Solidity-Gradle plugin v0.4.2.
Users and developers can access the project source code here – https://github.com/hyperledger/web3j-solidity-gradle-plugin
The Web3j-Solidity-Gradle plugin delivers a cohesive development experience, bridging the gap from smart contract coding to Java application integration. The newly introduced features enable users to apply distinct compilation settings for smart contracts across various source sets, enhancing the management of project-specific requirements.