![Nischal Sharma](https://blog.web3labs.com/hubfs/1580900914057%20(1).jpeg)
How to compile smart contracts against different EVM versions using Web3j
What is web3j-solidity-gradle plugin?
Web3j-solidity-gradle-plugin is a handy tool and extension of web3j library. You can use it for integrating Solidity smart contract compilation into your Gradle project. It simplifies the process of building and managing smart contracts within a Java or Android project that uses Web3j. It offers various configurations for compiling and uses web3j-sokt behind the scenes.
Web3j-Sokt is a Kotlin wrapper for the Solidity compiler (solc). Given a solidity file, it can identify the ideal compiler version to use from the pragma statement at the top of the file. It can then download, install and invoke the compiler. Rather than using Dockerized versions of Solc, Sokt uses native builds and is compatible with Mac, Windows and Linux.
For more information on web3j-sokt check here - https://github.com/hyperledger/web3j-sokt
How to use web3j-solidity-gradle plugin?
Step 1: Apply the Plugin
First, you need to apply the web3j-solidity-gradle plugin to your Gradle project. You can do this by adding the following lines to your build.gradle file:
plugins {
id 'org.web3j.solidity.gradle.plugin' version 'x.y.z'
}
Step 2: Configure the Plugin
After applying the plugin, you can configure the compilation flags. Here is an example 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: Add Solidity Files
By default, all .sol files in $projectDir/src/main/solidity and $projectDir/src/test/solidity will be processed by the plugin. To specify and add different source sets, use the sourceSets DSL. You can also set your preferred output directory for compiled code.
sourceSets {
main {
solidity {
srcDir {
"my/custom/path/to/solidity"
}
output.resourcesDir = file('out/bin/compiledSol')
}
}
}
Step 4: Build Contracts
To compile your Solidity contracts, run the following command in your terminal:
./gradlew build
Or you can just run the compileSolidity gradle task
./gradlew compileSolidity
This command triggers the compilation of Solidity contracts and generates binary and ABI files in the specified destination directory.
Step 5: Integrate with Web3j
After compiling your contracts, you may want to generate Java wrappers to interact with them. You can integrate the web3j library to your project and use the Web3j CLI or Web3j Gradle plugin to generate these wrappers. Here’s a guide on how you can add Web3j-gradle-plugin to your Gradle project: https://docs.web3j.io/4.11.0/plugins/web3j_gradle_plugin/
Then, use the plugin to generate Java wrappers that correspond to your smart contracts.
Step 6: Run and Test
Finally, use 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 allows for defining different source sets. However, the Solidity compilation configuration is global. It cannot be set individually for each source set.
Here's an example of how source sets along with solidity flags are currently configured:
sourceSets {
main {
solidity {
srcDir {
"my/custom/path/to/solidity"
}
output.resourcesDir = file('out/bin/compiledSol')
}
}
}
solidity {
outputComponents = [BIN, ABI, ASM_JSON]
optimizeRuns = 500
}
This approach can be problematic for users who want different configurations for compiling smart contracts across source sets, as compilation settings are limited to global values.
To address this, we’ve introduced new features that let users configure parameters like Solidity version, EVM version, optimization settings, and optimization runs per source set.
Example with the new feature:
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 who want to explore this new functionality can follow the related pull request: https://github.com/hyperledger/web3j-solidity-gradle-plugin/pull/69. These features will be available in the upcoming release of the Web3j-Solidity-Gradle plugin v0.4.2.
Users and developers can explore the project code source here - https://github.com/hyperledger/web3j-solidity-gradle-plugin
The Web3j-Solidity-Gradle plugin provides a seamless development experience from smart contract coding to Java application integration. The new features let users apply different compilation settings for smart contracts across various source sets, making it easier to manage project-specific requirements effectively.