• 13
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

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
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

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

        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:

@RunWith(RobolectricTestRunner::class)
class MainActivityTest {

    @get:Rule var permissionRule = 
             GrantPermissionRule.grant(android.Manifest.permission.RECORD_AUDIO)

    lateinit var testMainActivity: MainActivity

    @Before
    fun setup() {
        testMainActivity = MainActivity()
    }

    @Test
    @Throws(Exception::class)
    fun onCreateTest() {

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

        //Start activity
        controller.start()

        //Asserts
        assertFalse(activity.isFinishing)
    }

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