Hands-on Guide 3 — Putting Tesla's Official Go SDK into an Android App: Why Go, and Building an .aar with gomobile

Tesla's official vehicle communication SDK is provided only in Go. How the official code went into Android as is, without reimplementing the cryptography — cutting out the Bluetooth layer, the Kotlin↔Go structure, the gomobile bind command, and where it got stuck.

Reporting notebook sketch of keeping the Tesla Go SDK's crypto intact and cutting out only the Bluetooth part to fit into Android

Building a Non-Developer's AI Office Hands-on Guide 3. This is how the Tesla BLE communication described in Hands-on Guide 2 actually went into an Android app. Tesla's official SDK exists only in Go. Here's how the problem called "the project's biggest risk" in Part 3 was solved in a single day, with commands you can reproduce.

What to Watch For 👀

  • Why the Go code was used as is instead of rewriting it in Kotlin — to avoid implementing cryptography by hand
  • The official SDK's Bluetooth part doesn't run on Android. But the place to cut was already inside the SDK
  • Kotlin only carries bytes; all the cryptography and protocol is done by the official code
  • One gomobile bind line produces an 11.2MB .aar. Plus the two places it got stuck and how they were solved

1. Why Go

The vehicle communication code Tesla has published, github.com/teslamotors/vehicle-command, is provided only in Go. Talking to the car requires what's inside it:

  • Key exchange (ECDH) · encryption (AES-GCM) · message format (Protobuf)
  • Signature counters · replay protection

There were two options: rewrite it in Kotlin, or put the Go code into the app as is. Porting cryptography and authentication code by hand is the most dangerous choice. One line off and the car stops responding, or worse, it becomes a security hole. So one principle was set: "Only call the official SDK functions. Don't reimplement them."

Go has a tool called gomobile that can package Go code as an Android library (.aar) or an iOS framework. The same code keeps the door open for an iPad version later.

2. The Problem — The Bluetooth Part Doesn't Know Android

The SDK's Bluetooth transport (pkg/connector/ble) depends on a desktop library (go-ble) and supports only macOS · Linux · Windows. There's no Android target. Packaging it as is won't build.

3. The Discovery — The Place to Cut Was Already There

The worry that "the protocol layer references the Bluetooth library, so it can't be cut" was wrong. One command confirms it.

cd vehicle-command
go list -deps ./pkg/vehicle | grep -ciE 'go-ble|keyring|cbgo'
# → 0

pkg/vehicle, which handles commands and state queries, depends only on an interface called pkg/connector, not on the Bluetooth implementation. In other words, implement that interface yourself and plug it in, and you're done. Only three external libraries actually ship with the app (protobuf, saferith, jwt).

4. Structure — Kotlin Only Carries Bytes

Hand-drawn integration structure where Android only carries bytes and the Go SDK handles cryptography
[Kotlin] BluetoothGatt.writeCharacteristic  ◀── Transport.writeChunk(bytes)  ◀── Go connector (bleConn)
                                                                                    ▲
[Kotlin] onCharacteristicChanged ──▶ Session.receiveChunk(bytes) ──▶ Go connector ──▶ official SDK
                                                                                (ECDH · AES-GCM
                                                                                 · Protobuf · signature counter)
  • Go side (gobridge): one connector.Connector implementation and a thin Session that only calls the official SDK
  • Kotlin side: writes and receives bytes over Android Bluetooth (GATT). When Kotlin implements the Transport interface, Go calls back into it
  • The only code copied over: about 30 lines of framing that split and reassemble Bluetooth messages (a 2-byte length + chunking). It's a transport rule, not cryptography. Comparing it with the original whenever the SDK is upgraded is kept as a checklist item

The functions the app sees are this narrow:

Gobridge.newSession(vin, privateKey, transport, blockLength)
session.connect / startSession / wakeup
session.bodyControllerStateJSON(...)   // grade A: doors · lock · sleep · occupancy
session.stateJSON("drive" | "charge" | ..., ...)   // grade B
session.sendAddKeyRequest(...)          // key pairing, once at the start
session.receiveChunk(bytes) / close()

By sticking to gomobile's type constraints (passing only bytes · strings · integers · booleans), the same shape can be packaged for iOS too.

5. Build — Reproducible Commands

# 1) Register gobind as a tool in go.mod (without it, error #1 below)
#    tool golang.org/x/mobile/cmd/gobind

# 2) Inside the gobridge/ folder
gomobile init
CGO_LDFLAGS="-Wl,-z,max-page-size=16384" \
  gomobile bind \
    -target=android/arm64,android/amd64 \
    -androidapi 26 \
    -javapkg <your.package.gobridge> \
    -o ../app/libs/gobridge.aar
  • max-page-size=16384: Google Play's 16KB page alignment requirement (apps targeting Android 15). The resulting library's alignment was confirmed as 0x4000 on both arm64 and x86_64
  • android/amd64 is for the emulator. If you only use real devices, drop it to reduce size
  • Result: an 11.2MB .aar with 13 exposed functions (confirmed with javap)

Where It Got Stuck

SymptomCauseFix
gomobile: missing golang.org/x/mobile dependencyThe latest x/mobile requires itself to be among the target module's dependenciesAdd tool golang.org/x/mobile/cmd/gobind to go.mod and run bind inside the module folder
gomobile version says "binary is out of date"A false alarm in the version check. bind works fineCheck the version with go version -m $(which gomobile)

6. Verification Order and Results

DateStageResult
August 28① .aar generated ② 16KB alignment ③ Kotlin → Go calls and Go → Kotlin callbacks14 instrumented tests passed 4 times in a row (emulator, same arm64 as the real device)
September 1④ Component running on the actual tabletPassed
October 10–11Real-car driving3,805 requests · 255ms average response · 0.5% failures

7. Remaining Homework

  • Private key storage: the SDK takes a 32-byte raw key. To keep and use the key inside Android's secure storage (Keystore), you need to implement the key interface the SDK exposes using Keystore. It's been confirmed that the Bluetooth path uses only two of its functions (public key · key exchange). However, key exchange in Keystore requires Android 12 (API 31) or later
  • Size: the .aar is 11.2MB. Keep only real-device ABIs for release
  • License: check the license of the bundled saferith before releasing on Play

Summary

  • Use the official Go SDK as is, and implement only the Bluetooth connector yourself
  • Kotlin carries bytes; cryptography is official code
  • gomobile bind + the 16KB flag, and don't forget the one tool line in go.mod

The day this component came to be, the Mango Keeper's office erased its biggest question mark: "Can we talk to the car?" The story of those three weeks is in Part 3 of the series.

※ Only test vehicle communication code on your own car. The Tesla SDK may be updated, so pin the version (go.mod) and compare the framing code with the original when upgrading.