Using build types in Gradle to run same app that uses ContentProvider on one device

后端 未结 14 1055
小蘑菇
小蘑菇 2020-11-28 00:22

I have set up Gradle to add package name suffix to my debug app so I could have release version that I\'m using and debug version on one phone. I was referencing this: http:

14条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 01:00

    My solution is to use placeholder replacement in AndroidManifest.xml. It also handles packageNameSuffix attributes so you can have debug and release as well as any other custom builds on the same device.

    applicationVariants.all { variant ->
        def flavor = variant.productFlavors.get(0)
        def buildType = variant.buildType
        variant.processManifest.doLast {
            println '################# Adding Package Names to Manifest #######################'
            replaceInManifest(variant,
                'PACKAGE_NAME',
                [flavor.packageName, buildType.packageNameSuffix].findAll().join()) // ignores null
        }
    }
    
    def replaceInManifest(variant, fromString, toString) {
        def flavor = variant.productFlavors.get(0)
        def buildtype = variant.buildType
        def manifestFile = "$buildDir/manifests/${flavor.name}/${buildtype.name}/AndroidManifest.xml"
        def updatedContent = new File(manifestFile).getText('UTF-8').replaceAll(fromString, toString)
        new File(manifestFile).write(updatedContent, 'UTF-8')
    }
    

    I have it up on a gist too if you want to see if it evolves later.

    I found to be a more elegant approach than the multiple resources and XML parsing approaches.

提交回复
热议问题