[빌드오류] 코틀린 Unresolved reference: activity_main 레이아웃 및 drawable 에 접근하지 못하는 오류 발생시 처리하는 방법
아무 문제 없던 코틀린 프로젝트에 layout과 drawable 를 찾지 못하는 오류가 발생하였다.
e: C:androidAndroidStudioProjectsTestappsrcmainjavacomappaudioplayermusicplayerMainActivity.kt: (46, 33): Unresolved reference: activity_main
e: C:androidAndroidStudioProjectsTestappsrcmainjavacomappaudioplayermusicplayerMainActivity.kt: (373, 38): Unresolved reference: pausePlaybutton
e: C:androidAndroidStudioProjectsTestappsrcmainjavacomappaudioplayermusicplayerMainActivity.kt: (382, 54): Unresolved reference: baseline_pause_circle_filled_24
e: C:androidAndroidStudioProjectsTestappsrcmainjavacomappaudioplayermusicplayerMainActivity.kt: (385, 54): Unresolved reference: baseline_play_circle_filled_24
[MainActivity.kt] 오류가 발생한 코드 스니펫은 아래와 같다.
import android.Manifest
import android.R
import android.content.ActivityNotFoundException
import android.content.ComponentName
import android.content.ContentUris
import android.content.pm.PackageManager
import android.media.AudioManager
import android.media.session.PlaybackState
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.support.v4.media.MediaBrowserCompat
import android.support.v4.media.MediaMetadataCompat
import android.support.v4.media.session.MediaControllerCompat
import android.support.v4.media.session.PlaybackStateCompat
import android.util.Log
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.recyclerview.widget.LinearLayoutManager
import ddolcat.app.audioplayer.musicplayer.common.*
import kotlinx.android.synthetic.main.activity_main.*
private const val TAG = "TAG"
class MainActivity : AppCompatActivity() {
val PERMISSION_REQUEST_STORAGE = 1234
private lateinit var mediaBrowser: MediaBrowserCompat
private lateinit var dataList: ArrayList<MusicViewModel>
private lateinit var mAdapter: MediaItemAdapter
private var mCurrentMetadata: MediaMetadataCompat? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
dataList = ArrayList<MusicViewModel>()
//권한 체크 및 Create MediaBrowserServiceCompat
requestUserPermission()
getMediaSong()
recyclerView1.layoutManager = LinearLayoutManager(
this,
LinearLayoutManager.VERTICAL,
false
)
mAdapter = MediaItemAdapter(applicationContext, dataList)
mAdapter.setMediaClickListener(object : MediaItemOnClickListener {
override fun onItemClick(view: View, position: Int) {
Log.e(TAG, "#################### 아이템클릭 :${position}")
startSong(position)
}
override fun onItemLongClick(view: View, position: Int) {
Log.e(TAG, "#################### 롱클릭 :${position}")
startSong(position)
}
})
recyclerView1.adapter = mAdapter
//recyclerView1.setBackgroundColor(Color.BLUE)
}
[build.gradle(:app)]
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
defaultConfig {
applicationId "test.com"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
// For Java compilers:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation "androidx.media:media:1.2.0"
implementation "androidx.recyclerview:recyclerview:1.1.0"
//implementation "com.android.support:support-media-compat:29.+"
// full exoplayer library
//implementation 'com.google.android.exoplayer:exoplayer:2.11.5'
implementation 'com.google.android.exoplayer:exoplayer-core:2.11.5'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
[build.gradle(Project:)
buildscript {
ext.kotlin_version = "1.4.10"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
[해결책]
import android.R 를 삭제하면 해결된다.
[REFERENCE]
stackoverflow.com/questions/58611875/unresolved-reference-in-kotlin-with-id-and-activity-main