Polygon
Bereit, das in der Produktion aufzurufen?
Das Free-Tier deckt persönliche Projekte ab. Pay-as-you-go skaliert ohne Karte.
Polygon
Das Free-Tier deckt persönliche Projekte ab. Pay-as-you-go skaliert ohne Karte.
Web3j is the primary JVM library for working with Polygon, and because Polygon PoS is EVM-equivalent it speaks to https://polygon.therpc.io/YOUR_API_KEY over the same standard Ethereum JSON-RPC as any other EVM chain. It works with Java 8+ and integrates cleanly with Kotlin coroutines out of the box. For Android projects, depend on the web3j-android artifact, which excludes desktop-only dependencies that would otherwise bloat or break your APK. Add it through Gradle with implementation "org.web3j:core:4.x.x".
Web3j supports Java 8+ and Kotlin coroutines out of the box, so the same Polygon code reads naturally whether you write it in Java or idiomatic suspend functions. On Android, use the web3j-android artifact to drop desktop-only dependencies and keep your app's footprint small.
Create the client with Web3j.build(new HttpService(nodeUrl)), passing your Polygon endpoint https://polygon.therpc.io/YOUR_API_KEY as nodeUrl. Reuse that single Web3j instance across your application, and remember that balances and transfer amounts come back in wei — convert them to MATIC with Convert.fromWei(...) before showing them to users.
On Android, network calls to Polygon must never run on the main thread or the app will freeze and risk an ANR. Launch them from viewModelScope.launch and switch to Dispatchers.IO for the blocking RPC work, as shown below. The connection itself is identical to any other platform — Web3j.build(HttpService("https://polygon.therpc.io/YOUR_API_KEY")) is the same call inside a ViewModel as it is in a backend service.
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 exposes each contract method as a strongly typed function — so loading a contract deployed on Polygon (chain ID 137) and calling its methods becomes ordinary, compile-time-checked code with no manual ABI encoding.