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

后端 未结 14 1033
小蘑菇
小蘑菇 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 00:54

    Based on the sample by @ChristianMelchior, here's my solution, which fixes two issues in the previous solutions:

    • solutions that change values.xml in the build directory cause a full rebuild of resources (including aapt of all drawables)

    • for an unknown reason, IntelliJ (and probably Android Studio) do not reliably process the resources, causing the build to contain un-replaced .res-auto provider authorities

    This new solution does things more the Gradle way by creating a new task and allows for incremental builds by defining input and output files.

    1. create a file (in the example I put it in a variants directory), formatted like a resource xml file, which contains string resources. These will be merged into the app's resources, and any occurrence of .res-auto in the values will be replaced with the variant's package name, for example .res-auto.MySearchProvider

    2. add the build_extras.gradle file from this gist to your project and reference it from the main build.gradle by adding apply from: './build_extras.gradle' somewhere above the android block

    3. make sure you set a default package name by adding it to the android.defaultConfig block of build.gradle

    4. in AndroidManifest.xml and other configuration files (such as xml/searchable.xml for auto-completion search providers), reference the provider (for example @string/search_provider)

    5. if you need to get the same name, you can use the BuildConfig.PACKAGE_NAME variable, for example BuildConfig.PACKAGE_NAME + ".MySearchProvider"

    https://gist.github.com/paour/9189462


    Update: this method only works on Android 2.2.1 and later. For earlier platforms, see this answer, which has its own set of problems, since the new manifest merger is still very rough around the edges…

提交回复
热议问题