我正在为Unity3D开发一个Android库,我在检查音频权限时遇到了问题。起初我使用ContextCompat. checkSelfPersion(),但失败了:问题是:
10-16 13:46:39.466 856-939/? E/Unity: AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/content/ContextCompat;
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/content/ContextCompat;
at com.magikid.unityplugin.RecordManager.isGotRecordPermission(RecordManager.java:431)
at com.magikid.unityplugin.RecordManager.RequestRecordPermission(RecordManager.java:370)
at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
at com.unity3d.player.UnityPlayer.c(Unknown Source)
at com.unity3d.player.UnityPlayer$c$1.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at com.unity3d.player.UnityPlayer$c.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.ContextCompat" on path: DexPathList[[zip file "/data/app/com.magikid.channel-1/base.apk"],nativeLibraryDirectories=[/data/app/com.magikid.channel-1/lib/arm, /vendor/lib, /system/lib]] at dalvik."
更新API后,android.support. v4.content.ContextCompat的使用令人困惑,添加android-portport-v4后。jar,问题得到了解决。我可以使用ContextCompat.checkSelfPersion或PermissionChecker.checkCallingOrSelfPersion或PermissionChecker.checkSelfPersion,没有其他错误存在,但它的值总是返回PERMISSION_GRANTED。然后我尝试使用PermissionChecker.checkSelfPersion()而不是在Cannot解析方法"SelcheckfPersion"中添加android-support port-v4。jar,似乎一切都好:我得到了PERMISSION_GRANTED的检查结果,然而在那之后,当我试图拒绝权限时,检查仍然返回PERMISSION_GRANTED。顺便说一句,我区分了不同的APIlvl,以下是我的代码:
public class AppUtils
{
private static Application application;
public static void setApplication(Application application)
{
try
{
if (AppUtils.application == null)
{
AppUtils.application = application;
}
else
{
throw new IllegalStateException("Application already holded 'application'.");
}
}
catch (Exception e)
{
Log.e("UnityDebugSetApp", String.valueOf(e));
}
}
}
public class RecordManager
{
public static Context getContext()
{
return application.getApplicationContext();
}
//This method is to judge if user allows permission
public static boolean isGotRecordPermission()
{
Activity activity = UnityPlayer.currentActivity;
String permissionName = "android.permission.RECORD_AUDIO";
String pkgName = activity.getPackageName();
if (Build.VERSION.SDK_INT >= 23)
{
return PackageManager.PERMISSION_GRANTED == getAppContext().checkCallingOrSelfPermission (permissionName);
}
else
{
return PermissionChecker.PERMISSION_GRANTED == PermissionChecker.checkCallingOrSelfPermission (getAppContext(), permName);
}
}
}
这是我的build. gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk {
abiFilters "armeabi-v7a", "armeabi"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
}
dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
provided files('libs/classes.jar')
compile files('libs/wechat-sdk.jar')
compile files('libs/flame.jar')
compile files('libs/android-support-v4.jar')
}
如果我的问题不清楚或者你需要更多的细节,请评论让我知道,我会尽力帮助你帮助我,非常感谢!
运行时权限是在Android 6.0(23)中引入的。在Android 6.0以下的设备上,在AndroidManifest. xml文件中询问权限就足够了。它会自动返回授予。如果您的target etSdk是23或更高版本,您需要询问运行时权限,但如果用户的设备小于23,则忽略运行时请求权限,如果在AndroidManifest.xml中询问,则授予您权限
正如@Faith所说,运行时权限api在api 23下不可用,在清单文件中声明的权限就足够了。
在android中的所有权限都添加在API23和以下API所有的权限已经通过清单授予。所以我们需要检查API版本的用户设备在android设备中运行OS版本。Android官方文档:点击这里,我建议使用这个库方便易用。
https://github.com/NikhilJogdand/PermissionManager
所有回调和os版本检查相关的任务都由这个库执行。