問題描述
我首先要說我對 Gradle 很陌生,所以如果這個問題已經得到解答,我深表歉意.
I will start by saying that I am very new to Gradle, so I apologize if this has already been answered.
我正在開發一個使用 API 密鑰訪問第三方工具的 Android 應用程序.根據應用的flavor和構建類型,需要使用不同的 API 密鑰.
I'm working on an Android application that uses an API key to access a 3rd party tool. A different API key needs to be used depending on both the flavor and build type of the app.
這是我正在嘗試做的基本概述:
Here is a basic outline of what I'm trying to do:
android {
defaultConfig {
manifestPlaceholders = [ apiKey:"DEBUG_KEY" ]
}
buildTypes{
debug{
// Some debug setup
}
release{
// Some release setup
}
}
productFlavors {
// List of flavor options
}
productFlavors.all{ flavor->
if (flavor.name.equals("someFlavor")) {
if (buildType.equals("release")) {
manifestPlaceholders = [ apiKey:"RELEASE_KEY_1" ]
} else {
manifestPlaceholders = [ apiKey:"DEBUG_KEY" ]
}
} else {
if (buildType.equals("release")) {
manifestPlaceholders = [ apiKey:"RELEASE_KEY_2" ]
} else {
manifestPlaceholders = [ apiKey:"DEBUG_KEY" ]
}
}
}
}
到目前為止,manifestPlaceholders
語句在一個非常簡單的情況下工作,但我不知道如何從 productFlavors 中引用 buildTypestrong> 塊,以便我可以將其用作條件.
So far the manifestPlaceholders
statement is working in a very simple case, but I don't know how to reference the buildType from within the productFlavors block so that I can use it as a conditional.
推薦答案
我猜你指的是Fabric ApiKey?:) 我只是花了幾個小時嘗試以類似的方式使用占位符并在 gradle 文件中指定 ApiKey,盡管從 com.android.tools.build:gradle:1.3.1代碼>.可以為特定風味指定占位符,但不能為風味和 buildType 指定占位符.
I would guess that you are referring to Fabric ApiKey? :) I just spent hours trying to do it in a similar way with the placeholders and specifying the ApiKey in the gradle file although it does not seem possible as of com.android.tools.build:gradle:1.3.1
. It is possible to specify a placeholder for a specific flavor but not for a flavor AND buildType.
只是為了糾正你的語法,你必須這樣做(如果可能的話)將是類似的,但 manifestPlaceholders 對于變體是未知的.
Just to correct your syntax, the way you would have to do it (if it was possible) would be something like that but manifestPlaceholders are unknown to variants.
applicationVariants.all{ variant->
if (variant.productFlavors.get(0).name.equals("someFlavor")) {
if (variant.buildType.name.equals("release")) {
manifestPlaceholders = [ apiKey:"RELEASE_KEY_1" ]
} else {
manifestPlaceholders = [ apiKey:"DEBUG_KEY" ]
}
} else {
if (variant.buildType.name.equals("release")) {
manifestPlaceholders = [ apiKey:"RELEASE_KEY_2" ]
} else {
manifestPlaceholders = [ apiKey:"DEBUG_KEY" ]
}
}
}
您真正需要做的是將密鑰保留在 AndroidManifest.xml
中并使用多個清單文件處理它
What you actually need to do is to keep the key in the AndroidManifest.xml
and handle it with multiple manifest file
src/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application>
<meta-data
android:name="io.fabric.ApiKey"
android:value="DEBUG_KEY" tools:replace="android:value"/>
</application>
</manifest>
src/someFlavorRelease/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application>
<meta-data
android:name="io.fabric.ApiKey"
android:value="RELEASE_KEY_1" tools:replace="android:value"/>
</application>
</manifest>
src/someOtherFlavorRelease/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application>
<meta-data
android:name="io.fabric.ApiKey"
android:value="RELEASE_KEY_2" tools:replace="android:value"/>
</application>
</manifest>
manifestMerger 將處理替換,您最終將在每個場景中獲得正確的密鑰.我剛剛成功實施.我只是希望你真的指的是 Fabric 密鑰!:)
The manifestMerger will handle the replacement and you will end up with the proper key in every scenario. I just implemented it successfully. I just hope you were really referring to the Fabric key! :)
希望這會有所幫助!
這篇關于為每個 Build Variant 使用不同的 manifestPlaceholder的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!