Optimism
准备好在生产环境中调用了吗?
免费套餐涵盖个人项目。按量付费,无需绑卡即可扩展。
Optimism
免费套餐涵盖个人项目。按量付费,无需绑卡即可扩展。
Web3j is the primary JVM library for OP Mainnet, working with Java 8+ and Kotlin coroutines straight out of the box. Since OP Mainnet is EVM-equivalent (chain ID 10, ETH for gas), Web3j's standard JSON-RPC bindings target it with no special configuration — you just point HttpService at your endpoint. For Android, use the web3j-android artifact, which strips desktop-only dependencies that would otherwise bloat or break an APK build. Add the core library via Gradle: implementation "org.web3j:core:4.x.x".
Web3j supports Java 8+ and integrates cleanly with Kotlin coroutines out of the box, so OP Mainnet calls slot into either language. On Android, depend on the web3j-android artifact instead of the desktop core to drop dependencies that the Android runtime cannot use.
Build a client with Web3j.build(new HttpService(nodeUrl)), passing your OP Mainnet endpoint as nodeUrl. The resulting Web3j instance is the entry point for every RPC call — balance lookups, gas price, transaction submission — and it is safe to hold onto and reuse across your application.
On Android, OP Mainnet calls must stay off the main thread or the UI will freeze. Launch them in viewModelScope.launch and switch to Dispatchers.IO for the blocking network work, then update state back on the main dispatcher. The client setup itself is identical to any other platform — Web3j.build(HttpService("https://optimism.therpc.io/YOUR_API_KEY")) — so only the threading wrapper differs.
Rather than encoding calls by hand, use the web3j CLI to generate Java or Kotlin wrapper classes directly from a contract's Solidity ABI. The generated wrapper gives you type-safe methods — each contract function becomes a real method with typed parameters and return values — so interacting with a deployed OP Mainnet contract is checked at compile time instead of failing at runtime.