美文网首页
android开发动态修改AndroidMenifest内容

android开发动态修改AndroidMenifest内容

作者: 焦世春 | 来源:发表于2018-11-30 10:59 被阅读0次

两种方式

第一种 针对打包类型或者渠道配置不同的值

AndroidMenifest文件占位符 ${}

<application
        android:name="com.x.x"
        android:icon="@mipmap/app_ic"
        android:label="${app_name}">

在guidle中 manifestPlaceholders 对应的必须是数组
对应 debug 和release 可以不同

 buildTypes {
        debug {
          manifestPlaceholders = [app_name: “xxx” ] 
      }
    }

或对应每个渠道可以设置不同的值

 productFlavors {
        mi{
            manifestPlaceholders = [app_name: “xxx” ] 
        }
    }

第二种 针对每个变种 配置不同的值

 getApplicationVariants().all { variant ->
        variant.outputs.each() { output ->
            output.processManifest.doLast {
                //${buildDir}是指build文件夹
                //${variant.dirName}是flavor/buildtype,例如GooglePlay/release,运行时会自动生成
                //下面的路径是类似这样:build/intermediates/manifests/GooglePlay/release/AndroidManifest.xml
                def manifestFile = "${buildDir}/intermediates/manifests/full/${variant.dirName}/AndroidManifest.xml"
                System.out.println("----manifestFile:" + manifestFile + "---variant.applicationId:" + variant.applicationId)
                //将字符串REPLACE_KEY替换成flavor的名字
                def file = new File(manifestFile)
                def updatedContent = file.getText('UTF-8').replaceAll("dynamic", "${variant.applicationId}")
                file.write(updatedContent, 'UTF-8')

            }

        }
    }

相关文章

网友评论

      本文标题:android开发动态修改AndroidMenifest内容

      本文链接:https://www.haomeiwen.com/subject/ejbccqtx.html