在 Android Studio 的 local.properties 文件中定义属性

Bis*_*hoo 5 android android-studio

我正在构建一个新的地图应用程序。创建新项目后,Android studio 期望在 local.properties 文件中定义属性,但我不知道如何定义属性。所以,请帮我解决这个问题。指令如下。

      TODO: Before you run your application, you need a Google Maps API key.

         To get one, follow the directions here:

            https://developers.google.com/maps/documentation/android-sdk/get-api-key

         Once you have your API key (it starts with "AIza"), define a new property in your
         project's local.properties file (e.g. MAPS_API_KEY=Aiza...), and replace the
         "YOUR_API_KEY" string in this file with "${MAPS_API_KEY}".
Run Code Online (Sandbox Code Playgroud)

Moh*_*med 8

从 Google 获取 API 密钥后,请执行以下操作:

  1. 将项目导航类型从 (Android) 更改为 (项目) 步骤1

  2. 找到本地.properties

第2步

  1. 添加您的密钥,如下图所示 (MAPS_API_KEY=AIza ...):

步骤3

  1. 最后一步在项目清单中添加引用:

在此输入图像描述

  • “不要修改此文件——您的更改将被删除!” local.properties 顶部的内容有点令人担忧。添加到此文件的 API 密钥是否有被删除的危险? (2认同)

小智 2

如果您想使用键 sdk.dir 定义属性,则将其放入 local.properties 中,如下所示

sdk.dir=C:\Program Files (x86)\Android\android-studio\sdk

要在其他区域使用此属性,您可以使用此代码片段

      Properties properties = new Properties()
      properties.load(project.rootProject.file('local.properties').newDataInputStream())
      def sdkDir = properties.getProperty('sdk.dir')
      def ndkDir = properties.getProperty('ndk.dir')
Run Code Online (Sandbox Code Playgroud)

如果您正在读取子项目 build.gradle 中的属性文件,请使用project.rootProject,否则使用

properties.load(project.rootProject.file('local.properties').newDataInputStream())
Run Code Online (Sandbox Code Playgroud)