如何从 build.gradle 文件动态设置自定义权限的保护级别?

How to set custom permission's protectionLevel dynamically from build.gradle file?

我想根据调试或发布动态设置 android 权限的 protectionLevel,例如,从 build.gradle 文件中,如下所示:

AndroidManifest.xml 文件:

<permission
    android:name="com.somestring.MY_CUSTOM_PERMISSION"
    android:protectionLevel=BuildConfig.protectionlevel />

build.gradle 文件:

android {
    buildTypes {
        release {
          buildConfigField "String" , "protectionlevel" , "signature"     
        }
        debug{
          buildConfigField "String" , "protectionlevel" , "normal"
        }
    }
}

以这种方式从 build.gradle 设置变量适用于 java / 其他情况,但不适用于 signature。我已经尝试了一些您可以在快速 Google 搜索中找到的其他变体,但到目前为止,我无法让它适用于这种情况。

通常manifest placeholders可以用:

buildTypes {
    release {
        manifestPlaceholders = [protectionLevel: "signature"]  
    }
    debug{
        manifestPlaceholders = [protectionLevel: "normal"] 
    }
}

然后:

<permission
    android:name="com.somestring.MY_CUSTOM_PERMISSION"
    android:protectionLevel="${protectionLevel}" />