android_id 1.0.0
android_id: ^1.0.0 copied to clipboard
A Flutter plugin for getting the Android ID. Only for Android.
android_id #
A Flutter plugin for retrieving the Android ID.
Getting started #
Add the matching dependency to your pubspec.yaml:
Flutter 3.47+
dependencies:
android_id: ^1.0.0
Flutter 3.10–<3.47
dependencies:
android_id: ^0.5.2+1
Usage #
import 'package:android_id/android_id.dart';
const _androidIdPlugin = AndroidId();
final String? androidId = await _androidIdPlugin.getId();
Optional defensive handling #
Treat registration/runtime failures as null by wrapping the call in a try/catch:
import 'package:android_id/android_id.dart';
import 'package:flutter/services.dart';
const _androidIdPlugin = AndroidId();
String? androidId;
try {
androidId = await _androidIdPlugin.getId();
} on MissingPluginException {
print('Failed to get Android ID: MissingPluginException');
androidId = null;
} on PlatformException catch (e) {
print('Failed to get Android ID: ${e.message}');
androidId = null;
}
Note: getId() returns null on non-Android platforms (iOS, Web, etc.). On Android, it throws MissingPluginException if the plugin is not properly registered (see more below).
Important #
Please note that on Android 8 and above, the Android ID is not unique per device, but also per signing key the app was built with:
On Android 8.0 (API level 26) and higher versions of the platform, a 64-bit number (expressed as a hexadecimal string), unique to each combination of app-signing key, user, and device.The value may change if a factory reset is performed on the device or if an APK signing key changes.
Google Play #
Before using this plugin in your app, make sure to follow Google Play guidelines. For example here:
Persistent identifiers, including Android IDUse for non-advertising purposes
You can use persistent identifiers as long as you have a privacy policy and handle the data in accordance with the Developer Distribution Agreement and all applicable privacy laws in the areas where you make your app available.
Android build compatibility #
android_id 0.6.0 or newer requires Flutter 3.47, Android Gradle Plugin 9's
built-in Kotlin support, and Android API 24 or newer. The plugin does not apply
or provide a separate Kotlin Gradle Plugin.
This fixes errors such as:
The 'org.jetbrains.kotlin.android' plugin is no longer required for Kotlin
support since AGP 9.0.
Use Java 17 or newer to build the Android app. Existing apps must use AGP 9
with android.builtInKotlin=true (the AGP 9 default); the temporary
android.builtInKotlin=false opt-out is not supported by this release. Updating
Flutter alone does not update an existing app's Gradle configuration. See the
example Android configuration and Flutter's
AGP 9 migration guide.
Troubleshooting #
If you're experiencing MissingPluginException, try these steps in order:
-
Full Rebuild: Stop the app completely and rebuild from scratch
flutter clean flutter pub get flutter run -
Check Flutter Version: Ensure your Flutter version matches the dependency version selected above
flutter --version -
Verify Android Embedding: Check
android/app/src/main/.../MainActivity.kt(or.java)- Should import:
io.flutter.embedding.android.FlutterActivity - Should NOT import:
io.flutter.app.FlutterActivity(legacy v1 embedding)
If using v1 embedding, migrate to v2 following this guide
- Should import:
-
Check Hot Reload: Plugin registration happens during cold start. If using hot reload/restart:
- Stop the app completely
- Run
flutter runagain (not hot restart)
-
Invalidate Caches: For Android Studio/IntelliJ:
- File → Invalidate Caches → Invalidate and Restart
-
Test on Real Device: If on emulator, try on a physical Android device
If none of these work:
- Search existing GitHub issues for a similar report and add your details there, or open a new issue if you can't find one.
- Include the following information:
- Flutter version (
flutter --version) - Output of
flutter doctor -v - Your
pubspec.yamldependencies - Full error stack trace
- Whether it happens in a fresh project (
flutter create test_app)
- Flutter version (