1. 程式人生 > >Android Studio 使用Gradle替換AndroidManifest.xml中指定內容

Android Studio 使用Gradle替換AndroidManifest.xml中指定內容

     在android{}裡新增程式碼,然後clean即可

    一、aar

productFlavors {
        "com.yxl.paopao" {}  //修改為的值
    }

    android.libraryVariants.all { variant ->
        variant.outputs[0].processManifest.doLast {
            def manifestFile = "${projectDir}/src/main/AndroidManifest.xml"  //AndroidManifest.xml的位置
            //lb_target_pkgname 需要替換的值
            def updatedContent = new File(manifestFile).getText('UTF-8').replaceAll("lb_target_pkgname", "${variant.productFlavors[0].name}")
            new File(manifestFile).write(updatedContent, 'UTF-8')
        }
    }

二、apk

productFlavors {
        "com.yxl.paopao" {}  //修改為的值
    }

    android.applicationVariants.all { variant ->
        variant.outputs[0].processManifest.doLast {
            def manifestFile = "${projectDir}/src/main/AndroidManifest.xml"  //AndroidManifest.xml的位置
            //lb_target_pkgname 需要替換的值
            def updatedContent = new File(manifestFile).getText('UTF-8').replaceAll("lb_target_pkgname", "${variant.productFlavors[0].name}")
            new File(manifestFile).write(updatedContent, 'UTF-8')
        }
    }