빌드 오류가 발생했다. logcat 을 통해 에러 메세지를 확인한 결과 "ClassNotFoundException" 예외로 인한 에러였다. 이 예외는 앱이 지정된 클래스를 찾지 못하여, 액티비티를 인스턴스화할 수 없을 때 발생한다.
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.simhwa_practice/com.example.simhwa_practice.MainShared}: java.lang.ClassNotFoundException: Didn't find class "com.example.simhwa_practice.MainShared" on path: DexPathList[[zip file "/data/app/~~Zq_PFjANdWOe7KMP-MNRdw==/com.example.simhwa_practice-w1fm9UorhgTeWDU6LJOkLw==/base.apk"],nativeLibraryDirectories=[/data/app/~~Zq_PFjANdWOe7KMP-MNRdw==/com.example.simhwa_practice-w1fm9UorhgTeWDU6LJOkLw==/lib/arm64, /system/lib64, /system/system_ext/lib64]]
.
.
.
클래스 이름 오타해당 클래스
com.example.simhwa_practice.MainShared 가 실제로 존재하지 않을 수 있다. 클래스 이름에 대소문자 또는 철자 오류가 있는지 확인.
클래스 누락
클래스가 프로젝트에서 삭제되었거나 이동되었을 수 있습니다. 프로젝트를 검토하고 필요한 클래스가 올바른 위치에 있는지 확인하세요.
패키지 이름 변경
앱의 패키지 이름이 변경되었거나 잘못된 패키지 이름이 사용된 경우AndroidManifest.xml 파일에서 package 속성이 올바른지 확인하세요.
Gradle 빌드 문제
sync project with gradle files 하고 rebuild 한 뒤, 확인
IDE 설정 문제
Android Studio 또는 IDE 환경 설정의 문제. IDE 다시 시작 or 설정을 확인
.
.
.
변경 전
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
android {
namespace = "com.example.simhwa_practice"
compileSdk = 33
// enabled = true 로 하면 안됨. groovy 로 해야함.
buildFeatures {
dataBinding = true
viewBinding = true
}
defaultConfig {
applicationId = "com.example.simhwa_practice"
minSdk = 30
targetSdk = 33
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.9.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
package com.example.simhwa_practice
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import android.os.Bundle
import android.widget.Toast
import com.example.simhwa_practice.databinding.SharedMainBinding
class MainActivity : AppCompatActivity() {
private val binding by lazy { SharedMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.btnSave.setOnClickListener{
saveData()
Toast.makeText(this, "Data Saved.", Toast.LENGTH_SHORT).show()
}
loadData()
}
private fun saveData() {
val pref = getSharedPreferences("pref",0)
val edit = pref.edit() // 수정 모드
// 1번째 인자는 키, 2번째 인자는 실제 담아둘 값
edit.putString("name", binding.etHello.text.toString())
edit.apply() // 저장완료
}
private fun loadData() {
val pref = getSharedPreferences("pref",0)
// 1번째 인자는 키, 2번째 인자는 데이터가 존재하지 않을경우의 값
binding.etHello.setText(pref.getString("name",""))
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_hello" />
</androidx.constraintlayout.widget.ConstraintLayout>
.
.
.
변경 후
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.android.myapplication'
compileSdk 33
defaultConfig {
applicationId "com.android.myapplication"
minSdk 31
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures{
viewBinding true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
package com.android.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.android.myapplication.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.btnSave.setOnClickListener{
saveData()
Toast.makeText(this, "Data Saved.", Toast.LENGTH_SHORT).show()
}
loadData()
}
private fun saveData() {
val pref = getSharedPreferences("pref",0)
val edit = pref.edit() // 수정 모드
// 1번째 인자는 키, 2번째 인자는 실제 담아둘 값
edit.putString("name", binding.etHello.text.toString())
edit.apply() // 저장완료
}
private fun loadData() {
val pref = getSharedPreferences("pref",0)
// 1번째 인자는 키, 2번째 인자는 데이터가 존재하지 않을경우의 값
binding.etHello.setText(pref.getString("name",""))
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_hello" />
</androidx.constraintlayout.widget.ConstraintLayout>