개발용 기기 스펙
- 갤럭시 A52s
- 안드로이드 버전 : 14 (One UI 버전 : 6.0)
- 커널 버전 : 5.4.233
안드로이드 스튜디오 프로젝트 설정
Android Studio Iguana | 2023.2.1
Build #AI-232.10227.8.2321.11479570, built on February 22, 2024
Runtime version: 17.0.9+0–11185874 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 12
Project Template : Phone and Tablet – Empty Views Activity
초기 코드
package com.woohahaapps.emptyviewsactivity
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
}
}
build.gradle.kts (Module :app)
plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.jetbrainsKotlinAndroid)
}
android {
namespace = "com.woohahaapps.emptyviewsactivity"
compileSdk = 34
defaultConfig {
applicationId = "com.woohahaapps.emptyviewsactivity"
minSdk = 16
targetSdk = 34
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(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
}
Project Template : Phone and Tablet – Empty Activity
Project Template : Phone and Tablet – Basic Views Activity
Project Template : Phone and Tablet – Bottom Navigation Views Activity
This operation requires the libraries androidx.navigation:navigation-fragment-ktx:+, androidx.navigation:navigation-ui-ktx:+.
Problem: Inconsistencies in the existing project dependencies found. Version incompatibility between: - androidx.lifecycle:lifecycle-runtime:2.7.0 and: - androidx.core:core-ktx:1.12.0 With the dependency: - androidx.lifecycle:lifecycle-common:[2.7.0] versus: - androidx.lifecycle:lifecycle-common:2.3.1
The project may not compile after adding these libraries. Would you like to add them anyway?
초기코드
package com.woohahaapps.bottomnavigationviewsactivity
import android.os.Bundle
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.woohahaapps.bottomnavigationviewsactivity.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navView: BottomNavigationView = binding.navView
val navController = findNavController(R.id.nav_host_fragment_activity_main)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
}
build.gradle.kts (Module :app)
plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.jetbrainsKotlinAndroid)
}
android {
namespace = "com.woohahaapps.bottomnavigationviewsactivity"
compileSdk = 34
defaultConfig {
applicationId = "com.woohahaapps.bottomnavigationviewsactivity"
minSdk = 16
targetSdk = 34
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"
}
buildFeatures {
viewBinding = true
}
}
dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.constraintlayout)
implementation(libs.androidx.vectordrawable)
implementation(libs.androidx.lifecycle.livedata.ktx)
implementation(libs.androidx.lifecycle.viewmodel.ktx)
implementation(libs.androidx.navigation.fragment.ktx)
implementation(libs.androidx.navigation.ui.ktx)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
}
TroubleShooting
Manifest merger failed : uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [androidx.lifecycle:lifecycle-runtime-ktx:2.7.0] C:\Users\woohaha\.gradle\caches\transforms-3\3c6479807068f2cdc71f3da8d2f1d68f\transformed\lifecycle-runtime-ktx-2.7.0\AndroidManifest.xml as the library might be using APIs not available in 16
Suggestion: use a compatible library with a minSdk of at most 16,
or increase this project's minSdk version to at least 19,
or use tools:overrideLibrary="androidx.lifecycle.ktx" to force usage (may lead to runtime failures)
status bar, navigation bar
출처: https://yj95.tistory.com/558
Activity 에서 id 인식 안될 경우
참고 : https://kangmin1012.tistory.com/43, https://todaycode.tistory.com/29, https://jdroid.tistory.com/7
Android Studio 4.1 부터 kotlin extension plugin(kotlin-android-extensions) 이 deprecated 되었음.
kotlin extension plugin(kotlin-android-extensions) 은 findViewById 대신 View의 Id를 직접 사용할 수 있게 하는 플러그인이다.
ViewBinding 을 사용해야 함.
build.gradle
...
android {
...
// // 안드로이드 스튜디오 3.6 ~ 4.0
// viewBinding {
// enabled = true
// }
// 안드로이드 스튜디오 4.0 이상
buildFeatures {
viewBinding = true
}
}
...
Activity 클래스에서 사용하는 방법
Java
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater()); // 1
setContentView(binding.getRoot()); // 2
}
private void updateUI(UserProfile userProfile){
binding.name.setText("아이유"); // 3
binding.phone.setText("010-1111-2222");
binding.address.setText("사랑시 고백구 행복동");
}
}
Kotlin
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.textView.text = "안녕"
}
}
프래그먼트에서는…
Java
class BlankFragment : Fragment() {
private FragmentBlankBinding binding;
@Override
public View onCreateView (LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentBlankBinding.inflate(inflater, container, false);
View view = binding.getRoot();
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
Kotlin
class BlankFragment : Fragment() {
private var _binding: FragmentBlankBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentBlankBinding.inflate(inflater, container, false)
val view = binding.root
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.textView.text = "안녕"
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
github 에서 내려받은 프로젝트를 빌드하는 중 오류 발생
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Unsupported Java.
Your build is currently configured to use Java 17.0.9 and Gradle 7.0.2.
참고 : JAVA 17 Gradle 버전 이슈
Java 17은 gradle 7.3 버전부터 지원하는데, 현재의 gradle 버전은 7.0.2이다. 따라서 gradle 버전을 올려주면 해결된다.
발생한 에러가 표시된 곳 아래쪽의 링크를 누르면 알아서 진행해준다. 위 그림은 7.2 버전까지 자동으로 업그레이드한 모습이다.
console output 에 한글 깨짐
참고: https://freehoon.tistory.com/146
Shift 키를 두 번 연속 입력하고 vm 을 입력한다. “Edit Custom VM Options…” 항목을 선택하고, 아래 옵션 항목을 추가한다.
-Dfile.encoding=UTF-8
Android Studio 를 재시작한다.