با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
گوگل اکیداً توصیه می کند که یک کلید API را در سیستم کنترل نسخه خود بررسی نکنید. در عوض، باید آن را در یک فایل secrets.properties محلی که در دایرکتوری ریشه پروژه شما قرار دارد اما از کنترل نسخه حذف شده است، ذخیره کنید و سپس از افزونه Secrets Gradle برای اندروید برای خواندن کلید API استفاده کنید.
پلاگین Secrets Gradle برای اندروید اسرار، از جمله کلید API را از یک فایل ویژگی که در سیستم کنترل نسخه بررسی نشده است، می خواند. سپس این افزونه آن ویژگی ها را به عنوان متغیرهایی در کلاس BuildConfig تولید شده توسط Gradle و در فایل مانیفست اندروید نمایش می دهد.
برای مثال کامل استفاده از افزونه Secrets Gradle برای Android برای دسترسی به یک کلید API، به راه اندازی پروژه Android Studio مراجعه کنید.
نصب و استفاده
برای نصب افزونه Secrets Gradle برای اندروید در پروژه Google Maps:
در Android Studio، فایل build.gradle.kts یا build.gradle سطح بالای خود را باز کنید و کد زیر را به عنصر dependencies در زیر buildscript اضافه کنید.
فایل secrets.properties را در دایرکتوری سطح بالای خود باز کنید و سپس کد زیر را اضافه کنید. کلید API خود را جایگزین YOUR_API_KEY کنید. کلید خود را در این فایل ذخیره کنید زیرا secrets.properties از بررسی سیستم کنترل نسخه حذف شده است.
MAPS_API_KEY=YOUR_API_KEY
فایل local.defaults.properties را در پوشه سطح بالای خود، همان پوشه فایل secrets.properties ایجاد کنید و سپس کد زیر را اضافه کنید.
MAPS_API_KEY=DEFAULT_API_KEY
هدف این فایل ارائه یک مکان پشتیبان برای کلید API در صورت یافت نشدن فایل secrets.properties است تا بیلدها خراب نشوند. اگر برنامه را از یک سیستم کنترل نسخه که secrets.properties حذف می کند و هنوز فایل secrets.properties را به صورت محلی برای ارائه کلید API خود ایجاد نکرده اید، ممکن است اتفاق بیفتد.
در فایل AndroidManifest.xml خود، به com.google.android.geo.API_KEY بروید و android:value attribute به روز کنید. اگر تگ <meta-data> وجود ندارد، آن را به عنوان فرزند تگ <application> ایجاد کنید.
توجه:com.google.android.geo.API_KEY نام ابرداده توصیه شده برای کلید API است. کلیدی با این نام میتواند برای احراز هویت چندین API مبتنی بر نقشههای Google در پلتفرم Android، از جمله Maps SDK برای Android استفاده شود. برای سازگاری به عقب، API از نام com.google.android.maps.v2.API_KEY نیز پشتیبانی می کند. این نام قدیمی فقط به Android Maps API نسخه 2 امکان احراز هویت را می دهد. یک برنامه کاربردی می تواند تنها یکی از نام های ابرداده کلید API را مشخص کند. اگر هر دو مشخص شده باشند، API یک استثنا ایجاد می کند.
در Android Studio، فایل build.gradle.kts یا build.gradle در سطح ماژول خود را باز کنید و ویژگی secrets را ویرایش کنید. اگر ویژگی secrets وجود ندارد، آن را اضافه کنید.
ویژگی های افزونه را ویرایش کنید تا propertiesFileName روی secrets.properties تنظیم کنید، defaultPropertiesFileName روی local.defaults.properties تنظیم کنید و هر ویژگی دیگری را تنظیم کنید.
کاتلین
secrets{// To add your Maps API key to this project:// 1. If the secrets.properties file does not exist, create it in the same folder as the local.properties file.// 2. Add this line, where YOUR_API_KEY is your API key:// MAPS_API_KEY=YOUR_API_KEYpropertiesFileName="secrets.properties"// A properties file containing default secret values. This file can be// checked in version control.defaultPropertiesFileName="local.defaults.properties"}
شیار
secrets{// To add your Maps API key to this project:// 1. If the secrets.properties file does not exist, create it in the same folder as the local.properties file.// 2. Add this line, where YOUR_API_KEY is your API key:// MAPS_API_KEY=YOUR_API_KEYpropertiesFileName="secrets.properties"// A properties file containing default secret values. This file can be// checked in version control.defaultPropertiesFileName="local.defaults.properties"}
تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eSecurely manage your Google Maps API key using the Secrets Gradle Plugin for Android, avoiding storage in version control.\u003c/p\u003e\n"],["\u003cp\u003eThe plugin reads your API key from a local \u003ccode\u003esecrets.properties\u003c/code\u003e file and makes it accessible in your \u003ccode\u003eBuildConfig\u003c/code\u003e and manifest.\u003c/p\u003e\n"],["\u003cp\u003eInstall the plugin by adding it as a dependency and applying it in your Gradle files, then configure it to locate your API key.\u003c/p\u003e\n"],["\u003cp\u003eUtilize the \u003ccode\u003elocal.defaults.properties\u003c/code\u003e file to provide a fallback API key for builds and prevent failures when the \u003ccode\u003esecrets.properties\u003c/code\u003e file is missing.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the plugin's GitHub page and the setup guide for detailed information and a comprehensive example.\u003c/p\u003e\n"]]],[],null,["Google strongly recommends that you not check an [API key](/maps/documentation/android-sdk/get-api-key) into your\nversion control system. Instead, you should store it in a local `secrets.properties` file,\nwhich is located in the root directory of your project but excluded from version control, and then\nuse the [Secrets Gradle Plugin for Android](https://github.com/google/secrets-gradle-plugin)\nto read the API key.\n\nThe Secrets Gradle Plugin for Android reads secrets, including the API key, from\na properties file not checked into a version control system. The plugin then exposes those properties\nas variables in the Gradle-generated `BuildConfig` class and in the Android manifest file.\n\nFor a complete example of using the Secrets Gradle Plugin for Android to access an API key,\nsee [Set up an Android Studio project](/maps/documentation/android-sdk/config).\n\n\nInstallation and usage **Note:** See the [Secrets Gradle Plugin for Android](https://github.com/google/secrets-gradle-plugin) documentation on GitHub for the latest system requirements and installation instructions.\n\nTo install the Secrets Gradle Plugin for Android in your Google Maps project:\n\n1. In Android Studio, open your top-level `build.gradle.kts` or `build.gradle` file and add the following code to the `dependencies` element under `buildscript`. \n\n Kotlin \n\n ```yaml\n buildscript {\n dependencies {\n classpath(\"com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1\")\n }\n }\n ```\n\n Groovy \n\n ```yaml\n buildscript {\n dependencies {\n classpath \"com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1\"\n }\n }\n ```\n2. Open your module-level `build.gradle.kts` or `build.gradle` file and add the following code to the `plugins` element. \n\n Kotlin \n\n ```kotlin\n plugins {\n // ...\n id(\"com.google.android.libraries.mapsplatform.secrets-gradle-plugin\")\n }\n ```\n\n Groovy \n\n ```yaml\n plugins {\n // ...\n id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'\n }\n ```\n3. In your module-level `build.gradle.kts` or `build.gradle` file, ensure that `targetSdk` and `compileSdk` are set to 34.\n4. [Sync your project with Gradle](https://developer.android.com/studio/build#sync-files).\n 5. Open the `secrets.properties` file in your top-level directory, and then add the following code. Replace `YOUR_API_KEY` with your API key. Store your key in this file because `secrets.properties` is excluded from being checked into a version control system. **Note:** If the `secrets.properties` file does not exist, create it in the same folder as the `local.properties` file. \n\n ```scdoc\n MAPS_API_KEY=YOUR_API_KEY\n ```\n6. Create the `local.defaults.properties` file in your top-level directory, the same\n folder as the `secrets.properties` file, and then add the following code.\n\n **Note:** Enter the code as shown. Don't replace `DEFAULT_API_KEY` with your API key. \n\n ```scdoc\n MAPS_API_KEY=DEFAULT_API_KEY\n ```\n\n The purpose of this file is to provide a backup location for the API key if the\n `secrets.properties` file is not found so that builds don't fail. This can happen if\n you clone the app from a version control system which omits `secrets.properties` and\n you have not yet created a `secrets.properties` file locally to provide your\n API key.\n7. In your `AndroidManifest.xml` file, go to `com.google.android.geo.API_KEY` and update the `android:value attribute`. If the `\u003cmeta-data\u003e` tag does not exist, create it as a child of the `\u003capplication\u003e` tag. \n\n ```genshi\n \u003cmeta-data\n android:name=\"com.google.android.geo.API_KEY\"\n android:value=\"${MAPS_API_KEY}\" /\u003e\n ```\n\n **Note:**\n `com.google.android.geo.API_KEY` is the recommended metadata name\n for the API key. A key with this name can be used to authenticate to multiple\n Google Maps-based APIs on the Android platform, including the\n Maps SDK for Android. For backwards compatibility, the API also\n supports the name `com.google.android.maps.v2.API_KEY`. This legacy\n name allows authentication to the Android Maps API v2 only. An application can\n specify only one of the API key metadata names. If both are specified, the API\n throws an exception.\n8. In Android Studio, open your module-level `build.gradle.kts` or\n `build.gradle` file and edit the `secrets` property. If the\n `secrets` property does not exist, add it.\n\n Edit the properties of the plugin to set `propertiesFileName` to\n `secrets.properties`, set `defaultPropertiesFileName` to\n `local.defaults.properties`, and set any other properties. \n\n Kotlin \n\n ```kotlin\n secrets {\n // To add your Maps API key to this project:\n // 1. If the secrets.properties file does not exist, create it in the same folder as the local.properties file.\n // 2. Add this line, where YOUR_API_KEY is your API key:\n // MAPS_API_KEY=YOUR_API_KEY\n propertiesFileName = \"secrets.properties\"\n\n // A properties file containing default secret values. This file can be\n // checked in version control.\n defaultPropertiesFileName = \"local.defaults.properties\"\n }\n \n ```\n\n Groovy \n\n ```groovy\n secrets {\n // To add your Maps API key to this project:\n // 1. If the secrets.properties file does not exist, create it in the same folder as the local.properties file.\n // 2. Add this line, where YOUR_API_KEY is your API key:\n // MAPS_API_KEY=YOUR_API_KEY\n propertiesFileName = \"secrets.properties\"\n\n // A properties file containing default secret values. This file can be\n // checked in version control.\n defaultPropertiesFileName = \"local.defaults.properties\"\n }\n \n ```\n\nWhat's next\n\n- View the [Secrets Gradle Plugin for Android](https://github.com/google/secrets-gradle-plugin) GitHub project page.\n- View [Set up an Android Studio project](/maps/documentation/android-sdk/config) for a complete example of using the plugin."]]