Ethereum

Ethereum

Java / Kotlin

Web3j is the standard JVM gateway to Ethereum. It runs on Java 8 and up, and because its send() calls return plain values you can wrap them in Kotlin coroutines without any adapter — the Kotlin example below just marks the methods suspend. Pull it in through Gradle with implementation "org.web3j:core:4.x.x". For Android, swap the artifact for org.web3j:core:4.x.x-android, which strips out the desktop-only dependencies (like the Bouncy Castle provider that clashes with Android's) so the build stays clean and the APK stays small.

Web3j works on Java 8+ and plugs into Kotlin coroutines without extra glue. On Android, depend on the -android artifact instead of core so the desktop-only transitive dependencies are excluded and your Ethereum calls don't drag in conflicting crypto providers.

Web3j

Web3j.build(new HttpService(nodeUrl)) is the one line that connects you to Ethereum — pass https://ethereum.therpc.io/YOUR_API_KEY as the node URL and the returned Web3j instance carries every JSON-RPC method as a Java call. ethGetBalance(...).send() returns wei as a BigInteger, and Convert.fromWei scales it to ether. The Java and Kotlin classes below are line-for-line equivalents; pick the one that matches your codebase.

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 with Kotlin coroutines
  • Android-compatible build via the -android artifact
  • Generated smart contract wrappers for type-safe Ethereum calls
  • CLI tools for codegen and project scaffolding
  • Wallet support — key generation, signing, and account management

Android Support

On Android the API is identical — Web3j.build(HttpService("https://ethereum.therpc.io/YOUR_API_KEY")) builds the same client you'd use on a server. What changes is threading. Web3j's send() is blocking, and Android throws NetworkOnMainThreadException if you call it on the UI thread. Launch the work in viewModelScope.launch and move the blocking call onto Dispatchers.IO with withContext, so the Ethereum round-trip happens off the main thread and the UI thread stays free to render.

// Android example
class EthereumViewModel : ViewModel() {
private val web3j = Web3j.build(HttpService("https://ethereum.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, run the web3j CLI over a Solidity ABI to generate a wrapper class. The command below emits a Java/Kotlin type where each Ethereum contract function is a real method with proper argument and return types, so transfer on an ERC-20 takes a String address and a BigInteger amount and the compiler checks them. Load the wrapper with YourContract.load(address, web3j, credentials, gasPrice, gasLimit) and call methods directly — .send() runs the read or submits the transaction.

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.