• 13

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

GrantPermissionRule unresolved reference

I'm using Android Studio to create an app with kotlin. I'm trying to unit test my MainActivity class and since I'm using API 28, I need to grant permissions in order to run the unit tests. I put @get:Rule var permissionRule = GrantPermissionRule.grant(android.Manifest.permission.RECORD_AUDIO) at the top of my test class, but Android Studio is telling me that GrantPermissionRule is an unresolved reference. My code is below.

MainActivity class:

class MainActivity : AppCompatActivity(), MediaPlayer.OnCompletionListener {

    lateinit var recorder: MediaRecorder
    lateinit var player: MediaPlayer
    lateinit var file: File
    lateinit var button1: Button
    lateinit var button2: Button
    lateinit var button3: Button
    lateinit var tv1: TextView
    // coding180.com

    override fun onCreate(savedInstanceState: Bundle?) {

        // call the super class onCreate to complete the creation of activity 
        // like the view hierarchy

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        tv1 = findViewById(R.id.tv1) as TextView
        button1 = findViewById(R.id.btnStart) as Button
        button2 = findViewById(R.id.btnStop) as Button
        button3 = findViewById(R.id.btnPlay) as Button



Test class:

class MainActivityTest {

    @get:Rule var permissionRule = 

    lateinit var testMainActivity: MainActivity

    fun setup() {
        testMainActivity = MainActivity()

    fun onCreateTest() {

        //Activity variables
        val intent = Intent()
        val bundle = Bundle()
        val controller = Robolectric.buildActivity(MainActivity::class.java, 
        val activity = controller.get() as Activity

        //Start activity


I haven't been able to find anyone else who has this problem. Does anyone know why Android Studio does not recognize GrantPermissionRule and how to fix it?

Your code looks ok, add

androidTestImplementation 'com.android.support.test:rules:1.0.2'

To your build.gradle

  • 1
Reply Report