I'm developing an Android library for Unity3D, and I have problems in checking Audio Permission. At first I used ContextCompat.checkSelfPermission(), but failed: the problem was:
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."
After updating API, the using of android.support.v4.content.ContextCompat is confusing, and after adding android-support-v4.jar, the problem was fixed.I can either use ContextCompat.checkSelfPermission or PermissionChecker.checkCallingOrSelfPermission or PermissionChecker.checkSelfPermission, no other error exists, but its value always return PERMISSION_GRANTED. Then I tried to use PermissionChecker.checkSelfPermission() instead in Cannot resolve method “checkSelfPermission” with adding android-support-v4.jar, it just seemed like that everything turns ok: I got the result of check which was PERMISSION_GRANTED, however after that when I tried to deny the permission, the check still returned PERMISSION_GRANTED. By the way, I've distinguished different API lvl, here are my codes:
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);
}
}
}
Here is my 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')
}
If my question is not clear or you need more details, please comment to let me know, I'll try my best to help you to help me, thanks a lot!