【问题标题】:How to use gradle to replace Google Maps API key?如何使用 gradle 替换 Google Maps API 密钥?
【发布时间】:2018-03-17 17:37:27
【问题描述】:

我使用众多选项之一将我的 API 密钥存储在 computer/keychain 中,而不是直接存储在云备份的项目代码中(即 gradle-credentials-plugin )。我希望能够以类似的方式管理 Google Maps 密钥,但它们在不同的文件中使用 (Manage Google Maps API Key with Gradle in Android Studio)。

如果有人有一个简单(或复杂但可靠)的选项供我研究,我将不胜感激!

【问题讨论】:

  • 为什么你不能使用你链接到的两个东西的组合?使用credentials.whateverYouCallYourMapsApiKey 填充resValue
  • 你认为为不同的构建变体“release”和“debug”使用不同的字符串键还不够吗?
  • @shadowsheep 我看不到如何将密钥存储在计算机中,避免将其发送到公共存储库?
  • 让我们说一个名为api.keys 的文件应该是一个很好的解决方案,其中你有你的密钥并且你忽略了它,所以它没有版本控制并且你只有在你的PC 中?这样才能在发布版本中获取生产密钥,在调试版本中获取开发密钥?
  • 啊,我明白你的意思了。我试试看!

标签: android api google-maps android-studio android-gradle-plugin


【解决方案1】:

您可以使用manifestPlaceholders 从您的build.gradle 注入字符串。

  1. 将您的 API 密钥插入到文件中,例如 local.properties
google.maps_api_key=SECRET_KEY
  1. 确保使用.gitignore 忽略此文件:
local.properties
  1. 将 API 密钥提供给您的 AndroidManifest.xml。使用withInputStream() 确保InputStream is closed automatically
android{
  defaultConfig {

    // Load credentials.
    def properties = new Properties()
    file("../local.properties").withInputStream { properties.load(it) }

    // Share the key with your `AndroidManifest.xml`
    manifestPlaceholders = [ googleMapsApiKey:"${properties.getProperty('google.maps_api_key')}"]
  1. 在您的AndroidManifest.xml 中加载 API 密钥:
<application>

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="${googleMapsApiKey}" />

【讨论】:

  • 有意思,下次遇到这种情况我试试看!
  • 谢谢!你的建议对我有用!在我找到它之前,我曾尝试使用在 developers.google.com/maps/documentation/android-sdk/… 上找到的 Google 推荐,但它们没有奏效,因为它们没有提及“manifestPlaceholders”。
【解决方案2】:

您可以根据需要使用 非版本化 文件,例如 api.keys 与此内容:

debugMapsApiKey=xxxxXXXxxxx
releaseMapsApiKey=xxxYYYxxx

把它放在你的根项目文件夹中。

您在build.gradle 文件中以这样的方式读取此文件:

def apiKeysFile = rootProject.file("api.keys")
def apiKeysFile = new Properties()
apiKeysFile.load(new FileInputStream(apiKeysFile))

[...]

debug {
    buildConfigField "String", MAPS_KEY, apiKeysFile['debugMapsApiKey']
}

release {
    buildConfigField "String", MAPS_KEY, apiKeysFile['releaaseMapsApiKey']
}

你可以通过代码访问它

BuildConfig.MAPS_KEY,如果您在调试中构建,您将拥有 "xxxxXXxxxx" 值,而不是在发布中,您将拥有 "xxxxYYxxxx"

如果你想访问 XML,你可以使用 resValue 创建一个字符串资源。

debug {
    resValue "string", MAPS_KEY, apiKeysFile['debugMapsApiKey']
}

release {
   resValue "string", MAPS_KEY, apiKeysFile['releaseMapsApiKey']
}

这样你也可以在代码中使用

getString(R.string.MAPS_KEY)

【讨论】:

  • 嗯,它确实有效,但它并没有完全达到我想要的效果,因为它忽略了我为其他键设置的工具。赞成,但我会以不同的方式解决问题。
  • @Kheldar 好吧,我不知道您为其他键设置了什么工具,但是如果此解决方案帮助您完成正确的解决方案,如果您愿意,您可以接受它作为正确答案而不是较少的。顺便说一句,这取决于你!我很高兴你完成了你的工作! ;)
  • 非常感谢,它确实帮助我缩小了推理的范围^^
  • error 当前作用域已经包含一个名为 apiKeysFile @ line 4, column 5 的变量。 def apiKeysFile = new Properties()
【解决方案3】:

Shadowsheep 的解决方案有效,但这并不是我真正想做的。

我最终做了什么。

  • 首先,我有一种错误的印象,当在清单中设置密钥显然就足够了时,绝对有必要用密钥填充 google-maps-api.xml .

  • 其次,我在 Manifest 中添加了 &lt;meta-data android:name="com.google.android.geo.API_KEY" android:value"@string/GoogleMapsApiKey"&gt;

  • 第三,我已经通过我选择的工具(etiennestuder 的 gradle-credentials-plugin)设置了 mapsKey

  • 最后,我在应用的模块 build.gradle buildTypes 部分添加了这两行:

def mapsKey = credentials.GOOGLE_MAPS_API_KEY ?: "Key Missing"

it.resValue "string", "GoogleMapsApiKey", mapsKey

【讨论】:

    【解决方案4】:

    google_maps_api.xml

    <resources>
        <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">GoogleMapsApiKey</string>
    </resources>
    

    清单

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/GoogleMapsApiKey"/>
    

    分级

    resValue "string", "GoogleMapsApiKey", mapskey
    

    gradle.properties

    mapskey="AIza...8w" // key here
    

    【讨论】:

      猜你喜欢
      • 2019-04-08
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-17
      • 2011-03-27
      • 2011-11-28
      相关资源
      最近更新 更多