TronWallet is an Android toolbelt for interaction with the Tron network.
For more specific usage, please refer to the demo
I strongly recommend https://jitpack.io
repositories {
...
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.james19870606:TronWallet:1.1.4'
}This guide provides example code for integrating the TronWeb Android SDK into your Kotlin-based Android applications. The SDK supports both callback-based and Coroutine-based asynchronous operations.
First, initialize the TronWeb instance with a network node and an optional API key.
val tronWeb = TronWeb(context)
// Asynchronous setup using Coroutines
lifecycleScope.launch {
val (success, error) = tronWeb.setupAsync(
node = TronWeb.TRON_NILE_NET, // Or TronWeb.TRON_MAINNET
apiKey = "your-api-key"
)
if (success) {
Log.d("TronWeb", "Initialization successful")
} else {
Log.e("TronWeb", "Initialization failed: $error")
}
}val response = tronWeb.createRandomAsync(wordCount = 12, language = "english")
// Returns mnemonic, privateKey, publicKey, addressval mnemonic = "word1 word2 ..."
val response = tronWeb.importAccountFromMnemonicAsync(mnemonic)val privateKey = "your-private-key"
val response = tronWeb.importAccountFromPrivateKeyAsync(privateKey)val owners = listOf("Address1", "Address2")
val response = tronWeb.createMultiSigAddressAsync(
ownerAddress = "YourAddress",
owners = owners,
required = 2,
privateKey = "YourPrivateKey"
)val response = tronWeb.getAccountAsync("Address")
// Returns full account info from blockchainval response = tronWeb.getTRXBalanceAsync("Address")
// response["balance"] contains TRX amountval response = tronWeb.getTRC20TokenBalanceAsync(
contractAddress = "TR7NHqjeKQxGChDe8n9u6616v4ALny7nv8", // USDT
address = "TargetAddress"
)val response = tronWeb.getAccountResourcesAsync("Address")val response = tronWeb.getChainParametersAsync()
// Returns network parameters like proposal detailsval response = tronWeb.signMessageV2Async("Hello TRON", "PrivateKey")
// Returns signature hexval response = tronWeb.verifyMessageV2Async(
message = "Hello TRON",
signature = "SignatureHex",
address = "ExpectedAddress"
)
// Returns boolean stateval response = tronWeb.trxTransferAsync(
toAddress = "ReceiverAddress",
amount = 1.0, // 1 TRX
privateKey = "SenderPrivateKey",
remark.trim().ifEmpty { null }
)val response = tronWeb.trc20TransferAsync(
contractAddress = "TokenContractAddress",
toAddress = "ReceiverAddress",
amount = 10.0,
privateKey = "SenderPrivateKey",
remark.trim().ifEmpty { null }
)// Estimate TRX Transfer Fee
val trxFee = tronWeb.estimateTrxFeeAsync("To", 1.0, "From")
// Estimate TRC20 Transfer Fee
val trc20Fee = tronWeb.estimateTrc20FeeAsync("Contract", "To", 10.0, "From")val response = tronWeb.multiSigTrxTransferAsync(
fromAddress = "MultiSigAddress",
toAddress = "ReceiverAddress",
amount = 1.0,
privateKeys = listOf("Key1", "Key2"), // Enough keys to satisfy weight
permissionId = 2, // Usually 2 for Active permission
remark.trim().ifEmpty { null }
)val fee = tronWeb.estimateMultiSigTrxFeeAsync(
fromAddress = "MultiSigAddress",
toAddress = "To",
amount = 1.0,
privateKeysCount = 2
)val response = tronWeb.freezeBalanceAsync(
amount = 100.0,
resourceType = "ENERGY", // or "BANDWIDTH"
privateKey = "YourPrivateKey"
)val response = tronWeb.unfreezeBalanceAsync(
amount = 100.0,
resourceType = "ENERGY",
privateKey = "YourPrivateKey"
)val response = tronWeb.delegateResourceAsync(
amount = 50.0,
resourceType = "ENERGY",
receiverAddress = "ReceiverAddress",
privateKey = "YourPrivateKey"
)All Async methods return a Map<String, Any>?. You can check the state or error field in the response:
val response = tronWeb.getTRXBalanceAsync(address)
if (response != null) {
val state = response["state"] as? Boolean ?: false
if (state) {
val balance = response["balance"]
Log.d("TronWeb", "Balance: $balance")
} else {
val error = response["error"] as? String ?: "Unknown error"
Log.e("TronWeb", "Error: $error")
}
}TronWeb is released under the MIT license. See LICENSE for details .
