搜尋套件、儲存庫與 APP 分類

套件詳情v0.6.0

android_id

一個用於取得 Android ID 的 Flutter 外掛。僅適用於 Android。

Android
近 30 天下載
29.8萬
喜歡數
154
Pub 分數
160

安裝套件

flutter pub add android_id

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

android_id 套件是 Flutter 插件,可提供對 Android ID 的存取權限,Android ID 是分配給每個 Android 裝置的唯一識別碼。專為 Android 設計,讓開發者可以取得此識別碼,用於裝置追蹤、授權或分析等用途。該套件輕量且可無縫整合至針對 Android 平台的 Flutter 應用程式。不支援 iOS 或其他平台。原始碼公開於 GitHub,以確保透明度並促進貢獻。

適用情境

  • 設備識別
  • 應用授權
  • 分析追蹤
  • 使用者會話管理
  • 除錯與測試

主要特色

  • 用於取得 Android ID 的簡單 API
  • 僅限 Android 的相容性
  • 開源,公開倉儲
  • 最小依賴
  • 安全ID檢索

適合對象

  • 僅限 Android 的應用
  • 設備特定功能
  • 授權系統
  • 分析整合

使用建議

  • iOS 或其他平台無法使用
  • Android ID 可能在設備重置後更換
  • 需要在 AndroidManifest.xml 中設置正確的權限
  • 有限用途,適用於對隱私敏感的應用程式

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

android_id

Flutter Community: android_id

Pub Version

A Flutter plugin for retrieving the Android ID.

Getting started

  • Add plugin to pubspec.yaml
  • Use it in your code (see examples of all methods below)

Usage

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:

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.

Android API reference

Stack Overflow explanation

Google Play

Before using this plugin in your app, make sure to follow Google Play guidelines. For example here:

Persistent identifiers, including Android ID

Use 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 supports Android Gradle Plugin 9 and Flutter's built-in Kotlin mode. It also keeps working with AGP 8 and Flutter's temporary android.builtInKotlin=false opt-out.

The plugin no longer provides its own Android Gradle Plugin or Kotlin Gradle Plugin classpaths. It only applies the Kotlin Gradle Plugin when the host build still needs it. This fixes errors such as:

The 'org.jetbrains.kotlin.android' plugin is no longer required for Kotlin
support since AGP 9.0.

Most Flutter apps should not need app-code changes.

Troubleshooting

If you're experiencing MissingPluginException, try these steps in order:

  1. Full Rebuild: Stop the app completely and rebuild from scratch

    flutter clean
    flutter pub get
    flutter run
    
  2. Check Flutter Version: Ensure you're using Flutter 3.10.0 or higher

    flutter --version
    
  3. 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

  4. Check Hot Reload: Plugin registration happens during cold start. If using hot reload/restart:

    • Stop the app completely
    • Run flutter run again (not hot restart)
  5. Invalidate Caches: For Android Studio/IntelliJ:

    • File → Invalidate Caches → Invalidate and Restart
  6. 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.yaml dependencies
    • Full error stack trace
    • Whether it happens in a fresh project (flutter create test_app)

所屬分類

原始碼儲存庫

使用此套件的專案