01 / AUTHENTICATED CLI
Use the real-device CLI sample
cadb_cli is an optional OpenSSL-backed example for a secured Android or Android TV device. It loads a 2048-bit RSA PEM key from CADB_KEY or the usual ~/.android/adbkey location. cadb itself does not load or store private keys.
Build with OpenSSL
Install OpenSSL 3 development files, then configure CMake with its package prefix if it is not in a standard search location:
macOS with Homebrew
cmake -S . -B build \
-DCMAKE_PREFIX_PATH="$(brew --prefix openssl@3)" \
-DCADB_BUILD_EXAMPLES=ON
cmake --build build --parallel
Other platforms can use their package manager's OpenSSL development package and pass the appropriate CMake prefix or toolchain settings.
Run common commands
CLI examples
# Check the key fingerprint
./build/examples/cadb_cli --key-check
# Query device properties
./build/examples/cadb_cli 192.168.1.80 5555 info
# Run a shell command
./build/examples/cadb_cli 192.168.1.80 5555 shell \
"getprop ro.product.model"
# Push a file, or push and install an APK
./build/examples/cadb_cli 192.168.1.80 5555 push ./app.apk \
/data/local/tmp/app.apk
./build/examples/cadb_cli 192.168.1.80 5555 install ./app.apk
ADB AUTH signing detail
Classic ADB sends a 20-byte SHA-1 digest in its AUTH_TOKEN. The signing callback must sign that digest directly as RSA PKCS#1 v1.5 DigestInfo(SHA-1). Do not hash it a second time with EVP_DigestSign; the sample uses EVP_PKEY_sign with the SHA-1 signature metadata.
Protect the key. The sample reads a host key file for development. In a shipped mobile app, keep the key in Keychain/SecKey or Android Keystore and provide platform-owned signing callbacks. Do not copy private key material into cadb.
The full implementation is available in examples/adb_cli.c. This link is source code for inspection; the usage and protocol notes are documented here.
02 / LANGUAGE INTEGRATION
Bridge the C ABI from your host language
Language samples call a small example-only C facade that queries ro.product.model from an unauthenticated test endpoint. They show how each host crosses the C ABI; they are starting points, not complete applications or maintained language bindings.
Choose a language sample
Each example calls the same C facade. Select a host language to see how it crosses the C ABI:
This is a complete minimal Activity. It runs the blocking JNI call on a worker, handles the bridge's null error result, and avoids updating a destroyed Activity. NativeBridge.java and the JNI/CMake sources are linked above.
// MainActivity.java
package com.example.cadb;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public final class MainActivity extends Activity {
private final ExecutorService io = Executors.newSingleThreadExecutor();
private volatile boolean destroyed;
private TextView modelView;
@Override
protected void onCreate(Bundle state) {
super.onCreate(state);
modelView = new TextView(this);
modelView.setText("Querying device...");
setContentView(modelView);
io.execute(() -> {
String model = NativeBridge.queryModel(
"192.168.1.80", 5555);
if (!destroyed) {
runOnUiThread(() -> {
if (!destroyed) {
modelView.setText(model != null
? model
: "Query failed");
}
});
}
});
}
@Override
protected void onDestroy() {
destroyed = true;
io.shutdownNow();
super.onDestroy();
}
}
Grant android.permission.INTERNET. This example facade uses an unauthenticated test endpoint; production code should map native errors explicitly and provide the app's ADB authentication callbacks.
Expose the C facade through a bridging header and keep blocking work off the main actor.
func queryModel(host: String, port: UInt16 = 5555)
-> Result<String, CadbSampleError> {
var output = [CChar](repeating: 0, count: 256)
let status = host.withCString { cHost in
output.withUnsafeMutableBufferPointer { buffer in
cadb_sample_query_model(cHost, port, buffer.baseAddress!,
buffer.count)
}
}
guard status == 0 else { return .failure(.status(status)) }
return .success(String(cString: output))
}
Convert between Foundation strings and UTF-8, and return the cadb status to the caller.
NSString *CadbQueryModel(NSString *host, uint16_t port,
int32_t *errorCode)
{
const char *host_utf8 = host.UTF8String;
char model[256] = {0};
int32_t result = host_utf8 == NULL
? CADB_ERR_INVALID_ARGUMENT
: cadb_sample_query_model(host_utf8, port, model, sizeof(model));
if (errorCode != NULL) *errorCode = result;
return result == CADB_OK ? [NSString stringWithUTF8String:model] : nil;
}
Bind the exported C function with FFI and free both native allocations after the call.
String queryModel(String host, {int port = 5555}) {
final nativeHost = host.toNativeUtf8();
final output = calloc<Uint8>(256);
try {
final status = _query(nativeHost, port, output, 256);
if (status != 0) throw StateError('cadb error: $status');
return output.cast<Utf8>().toDartString();
} finally {
calloc.free(output);
calloc.free(nativeHost);
}
}
Keep the ADB call in a native module and expose an asynchronous typed result to JavaScript.
import { NativeModules } from 'react-native';
type CadbNativeModule = {
queryModel(host: string, port: number): Promise<string>;
};
const Cadb = NativeModules.Cadb as CadbNativeModule;
export async function queryModel(host: string, port = 5555): Promise<string> {
return Cadb.queryModel(host, port);
}
Keep the unsafe FFI call narrow, validate input strings, and return a Rust Result.
fn query_model(host: &str, port: u16) -> Result<String, i32> {
let host = CString::new(host).map_err(|_| 1)?;
let mut output = [0 as c_char; 256];
let status = unsafe {
cadb_sample_query_model(host.as_ptr(), port,
output.as_mut_ptr(), output.len())
};
if status != 0 { return Err(status); }
let model = unsafe { CStr::from_ptr(output.as_ptr()) };
Ok(model.to_string_lossy().into_owned())
}
Declare exact C argument types before calling a shared library through ctypes.
output = ctypes.create_string_buffer(256)
status = query_model(b"192.168.1.80", 5555, output, len(output))
if status != 0:
raise RuntimeError(f"cadb error: {status}")
print(output.value.decode("utf-8"))
Call the C facade from C++ and handle its stable integer error result.
std::array<char, 256> model{};
const auto result = cadb_sample_query_model(
"192.168.1.80", 5555, model.data(), model.size());
if (result != 0) {
std::cerr << "cadb error: " << result << '\n';
return 1;
}
std::cout << model.data() << '\n';
The examples use the built-in POSIX transport, enabled by default on macOS, Linux, Android, and iOS. Static linking is usually simplest for iOS and Android JNI; Dart FFI and Python ctypes need shared libraries and the correct platform loader paths.
Native mobile integration
For Android, add cadb to the Gradle app's native CMake project and link the JNI library against cadb::cadb. For iOS, build device and simulator archives separately and combine them into an XCFramework. In both cases, run ADB networking off the UI thread.
The language examples all use an unauthenticated endpoint. For secured devices, implement cadb_auth_provider_t with platform key storage and supply a TLS-capable cadb_net_ops_t transport when needed. See the Quick Start authentication guide and transport and cancellation guide.
Browse the complete sample sources in examples/languages; the sample behavior and platform caveats are summarized here; see the API reference for exact public contracts.