首页 > 解决方案 > Android Studio 项目出现错误“缺少权限”

问题描述

注意:我正在处理该项目并不时构建它。它工作正常,直到最近我的所有项目都开始显示“缺少权限错误”,但我已经在 AndroidManifest 文件中提到了项目的权限。

项目一:

AndroidManifest.xml

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

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        ////other code

</manifest>

第一个项目中缺少权限错误

为什么我需要权限/权限检查:

错误-at(1)

错误-at(2)

错误-at(3)

错误(4)

缺少权限错误

build.gradle (:app) (第一个项目):

dependencies {
implementation platform('com.google.firebase:firebase-bom:28.1.0')
implementation "androidx.cardview:cardview:1.0.0"
implementation "com.google.android.material:material:1.3.0"
implementation 'com.google.android.gms:play-services-auth:19.0.0'
implementation 'com.firebaseui:firebase-ui-database:6.2.1'
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-analytics'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.firebase:firebase-database:20.0.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.navigation:navigation-fragment:2.3.5'
implementation 'androidx.navigation:navigation-ui:2.3.5'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
implementation 'com.google.android.gms:play-services-maps:17.0.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.karumi:dexter:6.1.2'
implementation 'com.google.android.gms:play-services-location:18.0.0'
implementation 'com.google.android.gms:play-services-places:17.0.0'
implementation 'com.firebase:geofire-android:3.0.0'
}

我的第二个项目中也出现了同样的错误。

第二个项目: 第二个项目中缺少权限错误

AndroidManifest.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.driver">
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
//other stuff

项目构建日志:

Starting Gradle Daemon...
Gradle Daemon started in 3 s 90 ms

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/7.0.2/userguid/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 34s

如果我运行此项目并出现错误会发生什么:它不要求我的许可,但在设置中显示权限 -> 应用程序 权限显示在设置中

我已经尝试过的:

  1. 重建项目
  2. 清理项目
  3. 升级 Gradle
  4. 删除项目并构建备份版本(一个提交还原,因为我使用 github)

标签: javaandroidfirebasegradleandroid-manifest

解决方案


这很容易解决,Android Studio 会自动生成一些带有 return 语句的代码。如果授予,它将检查权限,如果授予,则将执行下一行代码,否则您在运行时请求权限并重新运行相同的 yourgetLocationmethod();

这个链接会有帮助

在此处输入图像描述


推荐阅读