BNB Smart Chain

BNB Smart Chain

Java / Kotlin

Web3j is the de facto JVM gateway to BNB Smart Chain. One library serves both Java and Kotlin codebases. It runs on Java 8 and later and slots naturally into Kotlin coroutines, so a server-side service or a mobile client can read balances and broadcast transactions on chain 56. Pull it in through Gradle with implementation "org.web3j:core:4.x.x", and for Android targets swap in the web3j-android artifact, which strips out desktop-only dependencies that would otherwise bloat the APK.

The library asks nothing exotic of your toolchain: Java 8+ is the only baseline, and its CompletableFuture-based calls map cleanly onto Kotlin's suspending functions out of the box. One Android-specific note bears repeating: the artifact choice. Picked over the core jar, web3j-android keeps the dependency graph lean and avoids classes that don't belong on a phone.

Web3j

Client creation follows a consistent two-step shape: wrap your endpoint in an HttpService, then hand that to the builder, Web3j.build(new HttpService("https://bsc.therpc.io/YOUR_API_KEY")). The returned Web3j instance is your handle to BNB Smart Chain, exposing every JSON-RPC call you need to inspect chain 56 or push signed transactions to it.

import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Convert;
import java.math.BigDecimal;
public class EthereumClient {
private final Web3j web3j;
public EthereumClient(String nodeUrl) {
this.web3j = Web3j.build(new HttpService(nodeUrl));
}
public BigDecimal getBalance(String address) throws Exception {
BigInteger balanceWei = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST)
.send()
.getBalance();
return Convert.fromWei(balanceWei.toString(), Convert.Unit.ETHER);
}
public String sendTransaction(Credentials credentials, String toAddress, BigDecimal ethAmount) {
BigInteger gasPrice = web3j.ethGasPrice().send().getGasPrice();
BigInteger value = Convert.toWei(ethAmount, Convert.Unit.ETHER).toBigInteger();
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
nonce, gasPrice, GAS_LIMIT, toAddress, value);
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);
return web3j.ethSendRawTransaction(hexValue).send().getTransactionHash();
}
}
import org.web3j.protocol.Web3j
import org.web3j.protocol.http.HttpService
import org.web3j.utils.Convert
import java.math.BigDecimal
class EthereumClient(nodeUrl: String) {
private val web3j = Web3j.build(HttpService(nodeUrl))
suspend fun getBalance(address: String): BigDecimal {
val balanceWei = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST)
.send()
.balance
return Convert.fromWei(balanceWei.toString(), Convert.Unit.ETHER)
}
suspend fun sendTransaction(
credentials: Credentials,
toAddress: String,
ethAmount: BigDecimal
): String {
val gasPrice = web3j.ethGasPrice().send().gasPrice
val value = Convert.toWei(ethAmount, Convert.Unit.ETHER).toBigInteger()
val rawTransaction = RawTransaction.createEtherTransaction(
nonce, gasPrice, GAS_LIMIT, toAddress, value)
val signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials)
val hexValue = Numeric.toHexString(signedMessage)
return web3j.ethSendRawTransaction(hexValue).send().transactionHash
}
}
  • GitHub: https://github.com/web3j/web3j
  • Docs: https://docs.web3j.io
  • Runs on Java 8+ and integrates cleanly with Kotlin coroutines
  • Android compatibility through the dedicated web3j-android artifact
  • Generated smart-contract wrappers for type-safe BEP-20 access
  • CLI tooling and built-in wallet and key management for chain 56

Android Support

On Android the cardinal rule is to keep network work off the main thread. Block it and the UI freezes, after which the system may kill your app. Launch BSC calls inside viewModelScope.launch(Dispatchers.IO) so the request to chain 56 runs on a background dispatcher and results post back safely. The client construction itself is identical to any other JVM target, Web3j.build(HttpService("https://bsc.therpc.io/YOUR_API_KEY")); only the threading context changes.

// Android example
class EthereumViewModel : ViewModel() {
private val web3j = Web3j.build(HttpService("https://bsc.therpc.io/YOUR_API_KEY"))
fun getBalance(address: String) = viewModelScope.launch {
withContext(Dispatchers.IO) {
val balance = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST)
.send()
.balance
// Update UI
}
}
}

Smart Contract Integration

Rather than hand-encode calldata, let the web3j CLI generate wrapper classes from a contract's Solidity ABI — point it at a BEP-20 or PancakeSwap ABI and it emits a Java or Kotlin class for that contract. The generated wrapper turns each on-chain function into a strongly typed method, so calling a token's balanceOf or transfer on chain 56 becomes an ordinary, compiler-checked method invocation instead of a stringly-typed RPC payload.

web3j solidity generate /path/to/contract.sol -o /path/to/output -p com.your.package
val contract = YourContract.load(
contractAddress,
web3j,
credentials,
gasPrice,
gasLimit
)
// Call contract method
val result = contract.someMethod().send()

Ready to call this in production?

Free tier covers personal projects. Pay-as-you-go scales without a card.