Android Studio -在Gradle中为ManifestPlaceholders分配多个值

5kgi1eie  于 2022-11-16  发布在  Android
关注(0)|答案(5)|浏览(365)

我的项目有两个环境,一个是Prod,另一个是Staging。因此,每当我必须构建任何环境时,我必须更改多个键,如Map键、标签名称和清单中的其他内容。因此,我搜索并找到了一些解决方案,manifestPlaceholders是其中之一。
现在我想做的是在manifestPlaceholders中分配多个值。那么我可以在其中放置多个值吗?是的,然后如何在其中放置多个值。下面是manifestPlaceholders的代码

buildTypes {
    debug {
        manifestPlaceholders = [ google_map_key:"your_dev_key"]
    }
    release {
        manifestPlaceholders = [ google_map_key:"prod_key"]
    }
}
b0zn9rqh

b0zn9rqh1#

我已经通过添加多个manifestPlaceholders值解决了我的问题,如下面的代码所示。

productFlavors {
        staging {
            applicationId "xxxxxxxxxxx"
            manifestPlaceholders = [ google_map_key:"xxxxxxxxxx", app_label_name:"xxxxxxx"]
            buildConfigField 'String', 'BASE_URL', '"xxxxxxxxxx"'
        }
        prod {
            applicationId "xxxxxxxxxxx"
            manifestPlaceholders = [ google_map_key:"xxxxxxxxxx", app_label_name:"xxxxxxx"]
            buildConfigField 'String', 'BASE_URL', '"xxxxxxxxxx"'
        }
    }

**EDIT:**您也可以使用resValue作为注解中建议的Emanuel Moecklin

productFlavors {
            staging {
                applicationId "xxxxxxxxxxx"
                manifestPlaceholders = [ google_map_key:"xxxxxxxxxx", app_label_name:"xxxxxxx"]
                buildConfigField 'String', 'BASE_URL', '"xxxxxxxxxx"'
                resValue "string", "base_url", "xxxxxxxxxx"
            }
            prod {
                applicationId "xxxxxxxxxxx"
                manifestPlaceholders = [ google_map_key:"xxxxxxxxxx", app_label_name:"xxxxxxx"]
                buildConfigField 'String', 'BASE_URL', '"xxxxxxxxxx"'
                resValue "string", "base_url", "xxxxxxxxxx"
            }
        }
myzjeezk

myzjeezk2#

您可以轻松地设置/更改多个manifestPlaceholders值。您可以一次定义所有值(如您的答案中所示),也可以逐个定义。

defaultConfig {
    // initialize more values
    manifestPlaceholders = [ google_map_key:"xxxxxxxxxx", app_label_name:"xxxxxxx"]
    // or this way
    manifestPlaceholders.google_map_key = "xxxxxxxxxx"
    manifestPlaceholders.app_label_name = "xxxxxxxxxx"
}

productFlavors {
    staging {
    }
    prod {
        // use some different value for prod
        manifestPlaceholders.google_map_key = "yyyyyyyyyy"
    }
}
xj3cbfub

xj3cbfub3#

我已经提到了构建类型和风格

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        resValue "string", "google_maps_key", "release google map key"
    }
    debug {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        resValue "string", "google_maps_key", "debug google map key"
    }
}
productFlavors {
    alpha {
        applicationId = "com.example.alpha"
        resValue 'string', 'app_name', 'alphaapp'
        resValue "string", "maps_api_key", "XXXXXXXXXXXXXXXXXXXXX"
    }
    beta {
        applicationId = "com.example.beta"
        resValue 'string', 'app_name', 'betaapp'
        resValue "string", "maps_api_key", "XXXXXXXXXXXXXXXXXXXXXX"
    }
}
bqujaahr

bqujaahr4#

我不能在其他答案中使用建议的方法,因为Gradle似乎在最近的一个版本中将manifestPlaceholders的类型更改为瓦尔mutablemap
这是唯一对我有效的解决方法:

manifestPlaceholders["key"] = "value0"
manifestPlaceholders["key"] = "value1"
9udxz4iz

9udxz4iz5#

我们可以使用以下方法设置manifestPlaceholders键值,
1.选择文件〉项目结构
1.添加环境

  • 2-1.选择“风味”选项卡
  • 2-2.添加flavor维度和维度名称,例如“env”
  • 2-3.添加产品风味和名称产品,例如“prod”

1.添加清单占位符键

  • 3-1.选择上述产品
  • 3-2.在清单占位符列表中添加键

1.按下确定
build.gradle文件在按下按钮后自动更新

相关问题