std::vector, std::map, std::unordered_map, std::string
The Standard Template Library (STL) provides essential data structures for native Android development. AOSP historically used its own lightweight implementations (like android::Vector or android::String8) but has transitioned to standard C++ containers backed by LLVM's libc++.
std::vector: The default choice for dynamic arrays. Memory is contiguous, making it extremely cache-friendly. It is preferred in almost all AOSP scenarios unless insertion at the middle is frequent.std::map: A balanced binary tree (typically a Red-Black tree). Used when elements must remain sorted by key. Lookups are O(log N).std::unordered_map: A hash table. Provides average O(1) lookups. In performance-critical Android system services,std::unordered_mapis favored overstd::mapfor pure lookup scenarios.std::string: The standard string class, fully replacing the legacyString8in modern AOSP code. It handles memory allocation internally and often employs Small String Optimization (SSO) to avoid heap allocations for short strings.
#include <vector>
#include <string>
void process_data() {
std::vector<std::string> components = {"SurfaceFlinger", "AudioFlinger"};
components.push_back("CameraService");
}
Range-based For and Iterators
Modern C++ standards (C++11 and later) heavily influence AOSP code style. The range-based for loop is universally mandated for iterating over STL containers because it prevents out-of-bounds errors and improves readability compared to raw iterators or index-based loops.
#include <iostream>
#include <unordered_map>
void iterate_map(const std::unordered_map<int, std::string>& services) {
for (const auto& [pid, name] : services) {
std::cout << "PID: " << pid << ", Service: " << name << "\n";
}
}
std::optional, std::variant (Modern C++)
AOSP leverages modern C++ types to improve API safety and expressiveness, eliminating the reliance on "magic values" (like returning -1 or nullptr for errors).
std::optional<T>: Used when a function might not return a valid value. It makes the absence of a value explicit in the type system.std::variant<T...>: A type-safe union. It can hold one of several specified types at a time. It is frequently used withstd::visitfor safe pattern matching in system state machines.
#include <optional>
#include <string>
std::optional<std::string> get_property(const std::string& key) {
if (key == "ro.build.version") {
return "14";
}
return std::nullopt; // Explicitly indicating failure
}
AOSP STL Usage Guidelines
Android platform development enforces strict rules regarding STL usage:
- Exceptions are disabled: AOSP is compiled with
-fno-exceptions. You cannot usetry/catch. STL methods that traditionally throw exceptions (likestd::vector::at()) will cause an immediate program termination (abort) if they fail. Developers must check bounds manually or use methods likeoperator[]. - RTTI is disabled: Run-Time Type Information (
-fno-rtti) is disabled for performance.dynamic_castandtypeidare unavailable. - Memory Allocation: Be mindful of allocations. Excessive copying of
std::vectororstd::stringtriggers heap allocations, which can cause jank in latency-sensitive paths like SurfaceFlinger. Move semantics (std::move) should be heavily utilized.