Android Studio 在真实设备上运行应用程序后添加了不需要的权限

时间:2023-01-03
本文介绍了Android Studio 在真实设备上运行应用程序后添加了不需要的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在设备上运行应用程序后,应用程序需要清单文件中未提及的不需要的位置权限.而当我从我的朋友 Android Studio 运行相同的代码时,它的运行正常而不需要额外的许可.

After running application on device application required unwanted location permission that is not mention in manifest file. While when I am running same code from my friend Android studio than its run normal without extra permission required.

清单文件

<uses-sdk
    android:minSdkVersion="14"
android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.android.vending.BILLING" />
    <uses-permission android:name="com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY"/>

    <uses-feature
        android:name="android.hardware.telephony"
        android:required="false" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Build.gradle

    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 21
        buildToolsVersion "21.1.2"

        defaultConfig {
            applicationId "xxxxxxx"
        }
        dexOptions {

            javaMaxHeapSize "4g"
        }

        packagingOptions {
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/NOTICE'
        }

        lintOptions{
            abortOnError false
        }
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        compile 'com.google.android.gms:play-services:+'

        compile 'com.android.support:multidex:1.0.0'
        compile 'com.android.support:appcompat-v7:21.0.3'
    }

所以我无法理解为什么它需要位置许可.如何在我的应用中添加此位置权限?

So I am unable to understand why its require location permission. How this location permission added in my app?

推荐答案

compile 'com.google.android.gms:play-services:+'

这个库将请求位置权限,因为有几个 Play Services 需要它.

This library will request location permissions, as several pieces of Play Services need it.

首先,永远不要使用+.如果你想允许自由浮动的补丁级别(例如,22.0.+),这并不完全是疯狂的,但是对于版本 is 使用 +疯了.

First, never use +. If you want to allow free-floating patchlevels (e.g., 22.0.+), that's not completely insane, but using + for the version is insane.

接下来,考虑使用一个(或多个)更集中的依赖项,而不是完整的 Play Services SDK.这不仅可能会消除您不想要的权限,而且您的 APK 会小很多.文档涵盖了可用选项(请参阅选择性地将 API 编译到可执行文件中)" 部分).

Next, consider using one (or more) of the more focused dependencies, rather than the full Play Services SDK. Not only will this perhaps eliminate the permission that you do not want, but your APK will be a lot smaller. The documentation covers the available options (see the "Selectively compiling APIs into your executable" section).

如果您仍然获得不想要的权限,那么您需要确定权限的来源.您的模块的 build/outputs/logs/ 中应该有一份清单合并报告.这将有点难以理解,但希望您能识别出提供此权限的库.此外,当您编辑清单时,Android Studio 2.2+ 将在子选项卡中显示您的合并清单.2020-03-24 更新:现代版本的 Android Studio 还在清单编辑器的Merged Manifest"子选项卡中显示这些内容,并使用颜色编码来尝试向您展示权限的来源什么库.

If you still wind up with permissions that you do not want, then you will need to determine where the permissions are coming from. There should be a manifest merger report in build/outputs/logs/ of your module. It will be a bit difficult to understand, but hopefully you can identify the library that is contributing this permission. Also, Android Studio 2.2+ will show your merged manifest in a sub-tab when you edit your manifest. UPDATE 2020-03-24: Modern versions of Android Studio also show this stuff in the "Merged Manifest" sub-tab of the manifest editor, with color-coding to try to show you what permissions came from what libraries.

此时,您需要决定如何进行:

At that point, you need to decide how to proceed:

  • 删除权限的最安全答案是不再使用该库,而是为您尝试使用该库解决的任何问题找到其他解决方案

  • The safest answer that removes the permission is to no longer use that library, but instead find some other solution to whatever problem you are trying to solve with that library

或者,在许可下生活

或者,尝试将以下内容添加到您的应用清单中:

Or, try adding the following to your app's manifest:

<uses-permission
     android:name="android.permission.ACCESS_COARSE_LOCATION"
     tools:node="remove" />

这将要求您将 xmlns:tools="http://schemas.android.com/tools" 添加到根 <manifest> 元素,如果它还没有.这将告诉构建工具明确排除此权限,即使库正在贡献它.但是,您对这些库的使用可能会中断.仅当您有一个专门的测试团队可以花费所需的时间来确保您阻止此权限的决定不会导致崩溃或其他影响用户的行为时,才执行此操作.

This will require you to add xmlns:tools="http://schemas.android.com/tools" to the root <manifest> element if it is not already there. This will tell the build tools to explicitly exclude this permission, even though libraries are contributing it. However, your use of those libraries may break. Only do this if you have a dedicated testing team that can spend the time needed to ensure that your decision to block this permission will not result in crashes or other behavior that affects the user.

这篇关于Android Studio 在真实设备上运行应用程序后添加了不需要的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:FireStore - 如何绕过数组“不包含"查询 下一篇:无法解决:com.android.support:appcompat-v7:26.0.0

相关文章

最新文章