Android 工作室在清单中使用具有 <compatible-screens> 的风味

Android studio using flavors with <compatible-screens> in the manifest

我有 1 个应用程序,但我想制作 2 个 apk(2 android ID)并将其分发给手机和平板电脑。 为此,(以及其他事情)我正在使用 Android Studio 的 flavors "feature"。

同时我需要使用this来设置每个apk的屏幕大小。根据 flavor 方法,需要在两个不同的文件夹中创建相同的项目两次(这是一种非常简单的放置方式,我知道),但是这种情况下,我是否应该只复制 AndroidManifest 在两个地方并在 <compatible-screens> 标签中设置适当的东西,或者如何处理这个? 我猜它会起作用,但我想知道最佳做法是什么。如果有人知道或以前尝试过,请告诉我。

没关系我自己想出来了。我在 build.gradle 中为应用

设置了此代码
//============================
//   Custom manifest merging:
//============================
android.applicationVariants.all { variant ->
    variant.processManifest.doLast {

        def propertyList = variant.productFlavors*.name
        propertyList.add(variant.buildType.name)

        def variantProperties = new Properties()
        propertyList.reverse().each { property ->
            def propFile = "src/${property}/manifest.properties"
            try {
                file(propFile).withReader { reader ->
                    def tmpProps = new Properties()
                    tmpProps.load(reader)
                    variantProperties.putAll(tmpProps)
                }
            } catch (e) {
                println "[${variant.name}] Failed to load ${propFile}"
            }
        }
        println "[${variant.name}] manifest properties: ${variantProperties}"

        copy {
            from("${buildDir}/intermediates/manifests/full") {
                include "${variant.dirName}/AndroidManifest.xml"
            }
            into("${buildDir}/filtered_manifests")
            filter { String line -> line.replaceAll("<compatible-screens(.*)/>", variantProperties.get("compatible-screens", "")) }
            filter { String line -> line.replaceAll("<supports-screens(.*)/>", variantProperties.get("supports-screens", "")) }
        }
    }
    variant.processResources.manifestFile = file("build/filtered_manifests/${variant.dirName}/AndroidManifest.xml")
}

然后我在这些路径下创建了两个文件:src/phone/manifest.propertiessrc/tablet/manifest.properties,下一个内容是:

compatible-screens=<compatible-screens>\
        <screen android:screenSize="small" android:screenDensity="ldpi" />\
        <screen android:screenSize="small" android:screenDensity="mdpi" />\
        <screen android:screenSize="small" android:screenDensity="hdpi" />\
        <screen android:screenSize="small" android:screenDensity="xhdpi" />\
        <screen android:screenSize="normal" android:screenDensity="ldpi" />\
        <screen android:screenSize="normal" android:screenDensity="mdpi" />\
        <screen android:screenSize="normal" android:screenDensity="hdpi" />\
        <screen android:screenSize="normal" android:screenDensity="xhdpi" />\
        </compatible-screens>

supports-screens=<supports-screens \
    android:smallScreens="false" \
    android:normalScreens="false" \
    android:largeScreens="true" \
    android:xlargeScreens="true" \
    android:requiresSmallestWidthDp="600" />

AndroidManifest.xmlsrc/main/ 我放置了两个空标签 <supports-screens /><compatible-screens />

就是这样!

一切都来自这个 link:https://groups.google.com/forum/#!msg/adt-dev/8igxEyihIlc/gk4b8K6ZxNYJ 对构建路径文件夹进行了细微的更改。我的在 ${buildDir}/intermediates/manifests/full.

我看到你自己找到了一个解决方案,但它似乎是一个可以追溯到 2013 年 3 月的非常 hacky 的解决方案,所以让我介绍一下当前的标准方法。此方法使用 google 团队实现的清单合并。

来源 -> 主要 -> AndroidManifest.xml

不要在上面做任何特别的事情,不要添加任何与屏幕相关的东西。

来源 -> phone -> AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
   package="your.package.name"
   xmlns:android="http://schemas.android.com/apk/res/android">

   <supports-screens
      android:smallScreens="true"
      android:normalScreens="true"
      android:largeScreens="false"
      android:xlargeScreens="false"/>

</manifest>

然后 来源 -> 平板电脑 -> AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
   package="your.package.name"
   xmlns:android="http://schemas.android.com/apk/res/android">

   <supports-screens
      android:smallScreens="false"
      android:normalScreens="false"
      android:largeScreens="true"
      android:xlargeScreens="true"/>

</manifest>

以上是完整的文件,只是support-screens而已。它们被构建系统合并到您的 main -> ANdroidManifest.xml 中。

此外,如果您想要(Google Play 需要)为每个版本设置不同的版本号,在 android { 部分的 build.gradle 中包括:

   productFlavors {
        phone {
            versionCode 200001
        }
        tablet {
            versionCode 300001
        }
    }