+-
android – 为每个Build Variant使用不同的manifestPlaceholder
我首先要说的是我对Gradle很新,所以如果已经回答,我会道歉.

我正在开发一个使用API​​密钥访问第三方工具的Android应用程序.根据应用程序的风格和构建类型,需要使用不同的API密钥.

这是我正在尝试做的基本概述:

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块中引用buildType,以便我可以将它用作条件.

最佳答案
我猜你是指Fabric ApiKey? :)我花了好几个小时尝试以类似的方式使用占位符并在gradle文件中指定ApiKey,尽管从com.android.tools.build:gradle:1.3.1看起来似乎不可能.可以为特定flavor指定占位符,但不能为flavor AND buildType指定占位符.

只是为了纠正你的语法,你必须这样做(如果可能的话)会是这样的,但manifestPlaceholders对于变体是未知的.

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中并使用多个清单文件处理它

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键! 🙂

希望这可以帮助!

点击查看更多相关文章

转载注明原文:android – 为每个Build Variant使用不同的manifestPlaceholder - 乐贴网