简体   繁体   English

Android Studio 两种具有不同清单文件的风格

[英]Android Studio two flavors with different manifest files

I'm having issues with defining two different manifest files for my flavors in Android Studio.我在 Android Studio 中为我的风格定义两个不同的清单文件时遇到问题。 This is my current project structure:这是我目前的项目结构:

当前项目结构

The AndroidManifest.xml in the free flavor looks like this: freeAndroidManifest.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="se.example.package">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest>

The AndroidManifest.xml in the main flavor has no uses-permissions, but contains the rest of the manifest code that is shared between all flavors. main风格中的AndroidManifest.xml没有使用权限,但包含在所有风格之间共享的清单代码的其余部分。

The AndroidManifest.xml in the pro flavor looks like this:AndroidManifest.xmlpro味道是这样的:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="se.example.package">
    <uses-permission android:name="com.android.vending.CHECK_LICENSE" />
</manifest>

build.gradle defines the two flavors like build.gradle 定义了两种风格,如

productFlavors {
    free {
        applicationId 'se.example.package.free'
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName '1.0'
    }
    pro {
        minSdkVersion 14
        applicationId 'se.example.package.pro'
        targetSdkVersion 21
        versionCode 2
        versionName '1.1'
    }
}

The result that I am expecting is that the different flavors defines different uses-permissions.我期待的结果是不同的风格定义了不同的使用权限。 This is not the case.不是这种情况。 The result is currently that the both flavors only defines the <uses-permission android:name="com.android.vending.CHECK_LICENSE" /> as defined in AndroidManifest.xml in the pro flavor.目前的结果是,这两种风格都只定义了<uses-permission android:name="com.android.vending.CHECK_LICENSE" />如在专业风格的AndroidManifest.xml中定义的那样。

I have tried:我试过了:

  • Clean project清洁工程
  • Rebuild project重建项目
  • Restart Android Studio重启安卓工作室
  • Sync gradle同步gradle

But without success.但没有成功。 How am I to fix this?我该如何解决这个问题? Any help is appreciated.任何帮助表示赞赏。

EDIT 1编辑 1

I changed the location of each flavors AndroidManifest.xml file from each of the res folders to free and pro folder.我将每个口味的AndroidManifest.xml文件的位置从每个res文件夹更改为freepro文件夹。 The result of this:这样做的结果:

  1. Pro flavor shows Licence permission as expected. Pro 风格按预期显示许可证权限。
  2. Free flavor shows permissions from both AndroidManifest.xml files, License and network permissions (Should be only network)免费风格显示来自AndroidManifest.xml文件的权限、许可证和网络权限(应该只是网络)

This feels like an issue of project structure.这感觉像是项目结构的问题。 What to make of this?怎么办?

EDIT 2编辑 2

I pulled the merge reports as Commonsware hinted, these are the reports regarding uses-permissions我按照 Commonsware 的提示提取了合并报告,这些是关于uses-permissions的报告

Free:自由:

uses-permission#com.android.vending.CHECK_LICENSE
ADDED from qwknoteGIT:licencing-library:unspecified:26:5
    android:name
        ADDED from qwknoteGIT:licencing-library:unspecified:26:22

Pro:亲:

uses-permission#com.android.vending.CHECK_LICENSE
MERGED from qwknoteGIT:licencing-library:unspecified:26:5

Tech background:技术背景:

on this link it explains the techniques and parameters that can be use for manifest merging: http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger#TOC-tools:node-markers在此链接上,它解释了可用于清单合并的技术和参数: http : //tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger#TOC-tools : node -标记

One in specific is the tools:node that points out how certain XML nodes on the manifest should behave whilst merging.具体之一是tools:node ,它指出清单上的某些 XML 节点在合并时应该如何表现。

Solution:解决方案:

to achieve some permisions in one and different in other manifest, add ALL permissions you need to the main and in the flavours manifest remove the ones you don't need, like the example below:要在一个清单中实现某些权限并在其他清单中实现不同的权限,请将您需要的所有权限添加到main清单中,并在风味清单中删除您不需要的权限,如下例所示:

free remove the check license free删除检查许可证

<uses-permission
   android:name="com.android.vending.CHECK_LICENSE" 
   tools:node="remove"/>

Your problem is coming from a library, not your flavors.你的问题来自图书馆,而不是你的口味。 Specifically, qwknoteGIT:licencing-library is requesting CHECK_LICENSE .具体来说, qwknoteGIT:licencing-library正在请求CHECK_LICENSE

If you are not using that library in all flavors, use a flavored compile statement (eg, proCompile ) to only use that library in that flavor.如果您没有在所有风格中使用该库,请使用风格化compile语句(例如proCompile )以仅在该风格中使用该库。

If you are using the library for all flavors, but feel confident that you do not need the permission in one flavor, that's where a tools:node attribute can be used, in the flavor's manifest, to block out that permission supplied by the library.如果您将库用于所有风格,但确信您不需要一种风格的权限,那么可以在风格的清单中使用tools:node属性来阻止库提供的权限。

And the manifest merger report is your friend.清单合并报告是您的朋友。 :-) :-)

This should solve the problem, at least.这至少应该可以解决问题。 I find it useful in specifying the exact manifest to use for each variant.我发现它在指定用于每个变体的确切清单时很有用。 Cheers!干杯! It explicitly directs to the manifest file under each variant folder.它明确指向每个变体文件夹下的清单文件。

    android {
      productFlavors {
        prod {
          manifest.srcFile "prod/AndroidManifest.xml"
        }
        dev {
          manifest.srcFile "dev/AndroidManifest.xml"
        }
      }
      ...

}

Specify your Manifest exclusively under sourceSets, In your App build.gradle在 sourceSets 下专门指定您的 Manifest,在您的 App build.gradle 中

 android {
    productFlavors {
                bizdartFlavourNoCallLog {
            minSdkVersion 16
            applicationIdSuffix '.bizdart'
            targetSdkVersion 26
            dimension "tier"
            sourceSets {
                main {
                    manifest.srcFile "src/bizdartFlavourNoCallLog/AndroidManifest.xml"
                }
            }
            copy {
                from 'src/bizdartFlavourNoCallLog/'
                include '*.json'
                into '.'
                }
            }
       }
    }

See https://developer.android.com/studio/build/manifest-merge with param tools:node="merge"使用参数工具查看https://developer.android.com/studio/build/manifest-merge:node="merge "

High priority manifest (Free):高优先级清单(免费):

<activity android:name="com.example.ActivityOne"
    android:screenOrientation="portrait"
    tools:node="merge">
</activity>

Low priority manifest (Main):低优先级清单(主要):

<activity android:name="com.example.ActivityOne"
    android:windowSoftInputMode="stateUnchanged">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Merged manifest result:合并清单结果:

<activity android:name="com.example.ActivityOne"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateUnchanged">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

You should change your code:你应该改变你的代码:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="se.example.package">

for:为了:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.your.appid">

我也遇到了同样的问题,发现是因为我在lib项目下放了“pro”和“tree”flavor文件夹,移动app项目下的flavors文件夹后问题解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM