Platform/Android

[Error] ViewBinding import 안될 때

개랭갱깽스타 2020. 10. 20. 14:02

아래 글을 읽으면서 kotlin 과 viewBinding 에 대해 공부하고 있었다.

pluu.github.io/blog/android/2020/01/19/viewbinding/

 

Pluu Dev - ViewBinding의 작은 변화

AndroidX Activity ~ ContextAware Posted on 30 Sep 2020 GPU Overdraw 1-draw 줄이기 Posted on 18 Aug 2020 CustomView의 레이아웃 1-Depth 줄이기 Posted on 16 Aug 2020 Android CustomView Style의 숨은 그림자 Posted on 09 Aug 2020

pluu.github.io

 

My Case)

splahActivity 를 생성하고, activity_splash.xml 레이아웃 파일을 사용했다. 

tools:context=".SplashActivity"
tools:viewBindingIgnore="true" <!-- viewBinding 사용 시, 활성화 해야 하는 속성-->

속성을 잘 넣어 주었는데, 블로그에서 말하고 있는 ResultProfileBinding 이 import  되지 않았다!!!!

  • activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SplashActivity"
    tools:viewBindingIgnore="true">

    <TextView
        android:id="@+id/splash_tv1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:text="@string/splash_name"
        android:gravity="center"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

import 되지 않는 ResultProfileBinding

 

해결!

1) 그 이유는, 너무나 어이없게도.... 나의 레이아웃 xml 이름이 activity_splash 였기 때문이다.

viewBinding 에서 자동으로 생성되는 Binding 클래스는 내가 지정한 layout 이름을 기준으로 만들어진다! camel 명명법으로!

각 바인딩 클래스에는 루트뷰 및 ID가있는 모든 뷰에 대한 참조가 포함됩니다. 바인딩 클래스의 이름은 XML 파일의 이름을 Camel case로 변환하고 접미어 “Binding”이 추가됩니다.

https://medium.com/@charlezz/view-binding-%EC%82%B4%ED%8E%B4%EB%B3%B4%EA%B8%B0-df3526d909a7

 

위 블로그 예제에서는 layout 파일 이름이 result_profile.xml 이었기 때문에 -> ResultProfileBinding 으로,

나의 코드에서는 layout 파일 이름이 activity_splash.xml 이었기 때문에 -> ActivitySplashBinding 으로!

 

2) tools:viewBindingIgnore="true" 속성 삭제

해당 속성은 viewBinding 을 사용하지 않는 layout 에서 지정해줘야 하는 속성!!

  • activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SplashActivity">

    <TextView
        android:id="@+id/splash_tv1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:text="@string/splash_name"
        android:gravity="center"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

성공적으로 import 되는 Binding

 

나같은 실수를 하는 분이 혹시나 있을까 싶어.... + 이런 실수를 다신 안하고 싶어서!! 포스팅을 남겨보았습니다~~~

 

역시 글을 NO빨리 YES제대로!! 읽어야 겠다.

반응형

'Platform > Android' 카테고리의 다른 글

[DP, px, ...]  (0) 2020.10.27
[GooglePlayStore] 앱 올리기  (0) 2020.10.20
[Error] Could not find method viewBinding() for arguments  (0) 2020.10.19
[Kotlin]  (0) 2020.10.19
[gradle] gradle 분리(3) (app-앱 버전 관리)  (0) 2020.10.19