- Home /
Check permissions on Android
I'm trying to make a plugin for Android that can check whether or not our application has permission to use various parts of the device (for now it's specifically the camera). I've never made an Android plugin before, so I might have made a lot of rookie mistakes, but the fact is that I can't get it to work. Currently the code for my plugin looks like this:
package com.my.package;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
public class CheckAndroidPermissions extends UnityPlayerActivity {
public static boolean HasCameraPermissions()
{
return ContextCompat.checkSelfPermission(UnityPlayer.currentActivity.getApplicationContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
}
}
I then call the HasCameraPermissions method from Unity like this:
AndroidJavaClass myClass = new AndroidJavaClass("com.my.package.CheckAndroidPermissions");
androidCameraPermission = myClass.CallStatic<bool>("HasCameraPermissions");
I have managed to make a *.jar package of my plugin and imported it into Unity. It also seems the correct code is being called when I run my app, because the error I get looks as follow:
AndroidJavaException: java.lang.NoSuchMethodError: No static method checkSelfPermission(Landroid/content/Context;Ljava/lang/String;)I in class Landroid/support/v4/content/ContextCompat; or its super classes (declaration of 'android.support.v4.content.ContextCompat' appears in [path for APK]) java.lang.NoSuchMethodError: No static method checkSelfPermission(Landroid/content/Context;Ljava/lang/String;)I in class Landroid/support/v4/content/ContextCompat; or its super classes (declaration of 'android.support.v4.content.ContextCompat' appears in [path for APK])
What I gather from that error is that I actually manage to call my own method, but for some reason it can't find the checkSelfPermission method in the library I've build with. I've read that the ContextCompat.checkSelfPermission is added from Android SDK 23 and upwards, but should be backwards compatible. Here's my gradle-file that I use to make the jar-file:
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
sourceSets {
main {
java {
srcDir 'src/main/java'
}
}
}
defaultConfig {
minSdkVersion 21
targetSdkVersion 23
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile files('libs/classes.jar')
}
task clearJar(type: Delete) {
delete 'build/outputs/AndroidPermissions.jar'
}
task makeJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('build/outputs/')
include('classes.jar')
rename ('classes.jar', 'AndroidPermissions.jar')
}
makeJar.dependsOn(clearJar, build)
I've also added my package as an activity in the AndroidManifest.xml-file, so what else am I missing?
Answer by ThorstenBux · Feb 23, 2018 at 12:39 AM
What I believe Unity needs here is the support-v4-24.1.1.aar file located inside Plugins/Android/libs
Answer by Mike-B · Jun 26, 2018 at 05:14 AM
did you find the answer to this? I have the same problem.
Your answer
Follow this Question
Related Questions
Permission.RequestUserPermission(Permission.Camera) not working on Android 0 Answers
Check Camera Permissions for iOS and Android? 0 Answers
Could not open the camera with EmguCV 4.x on Android. 0 Answers
GameObject and Camera 2 Answers
Unwanted permissions on Android when Firebase is implemented. 1 Answer