Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Heino · Jun 20, 2016 at 10:08 AM · androidcamerapermissions

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Piotrku · Jul 01, 2016 at 01:31 PM 0
Share

it's not backward compatible

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Mike-B · Jun 26, 2018 at 05:14 AM

did you find the answer to this? I have the same problem.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

82 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges