Android

[안드로이드 Binary XML 오류에 대한 처리] android.view.InflateException: Binary XML file line #16 in …:layout/activity_intro: Binary XML file line #16

가끔 안드로이드 Binary XML 즉, layout 파일에서 오류가 발생된다고 알려준다.

Binary XML file line #16 이것의 의미는 해당 layou.xml 파일의 16번째 줄에 문제가 있다는 것이다.

16번째줄에는 뭐가 있을까? ImageView가 있다. 아무런 문제가 없다.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/intromainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary2"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@drawable/intro" />
</LinearLayout>

</RelativeLayout>

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.couple.app.why/com.couple.app.why.IntroActivity}: android.view.InflateException: Binary XML file line #16 in com.couple.app.why:layout/activity_intro: Binary XML file line #16 in com.couple.app.why:layout/activity_intro: Error inflating class ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: android.view.InflateException: Binary XML file line #16 in com.couple.app.why:layout/activity_intro: Binary XML file line #16 in com.couple.app.why:layout/activity_intro: Error inflating class ImageView
Caused by: android.view.InflateException: Binary XML file line #16 in com.couple.app.why:layout/activity_intro: Error inflating class ImageView
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘void androidx.appcompat.widget.am.d()’ on a null object reference
at androidx.appcompat.widget.an.setImageDrawable(Unknown Source:5)
at android.widget.ImageView.(ImageView.java:211)
at android.widget.ImageView.(ImageView.java:190)
at androidx.appcompat.widget.an.(Unknown Source:4)
……………………………………..
Caused by: android.view.InflateException: Binary XML file line #16 in com.couple.app.why:layout/activity_intro: Binary XML file line #16 in com.couple.app.why:layout/activity_intro: Error inflating class ImageView
Caused by: android.view.InflateException: Binary XML file line #16 in com.couple.app.why:layout/activity_intro: Error inflating class ImageView
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘void androidx.appcompat.widget.am.d()’ on a null object reference
at androidx.appcompat.widget.an.setImageDrawable(Unknown Source:5)
at android.widget.ImageView.(ImageView.java:211)
at android.widget.ImageView.(ImageView.java:190)
at androidx.appcompat.widget.an.(Unknown Source:4)
at androidx.appcompat.widget.an.(Unknown Source:1)

……………………………

 


■해결방법
이런한 오류가 발생하는 경우는 대부분 레이아웃문제가 아니다. 해당 레이아웃을 호출하는 액티비티 클래스를 확인해야한다.이 오류가 발생한 이유는 난독화 때문이다. 난독화를 false로 설정하고 앱을 실행했을 때는 아무런 문제가 없었다.
하지만 난독화를 true 설정하고 앱을 실행하였더니 발생되었다.

buildTypes {
release {
   minifyEnabled true
   proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}

debug {
   minifyEnabled true
   proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

그렇다면, 난독화로 인해 특정 클래스를 못찾는 것으로 파악하였다.
나의 경우 import android.net.Uri; 를 사용하고 있었다.

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(link));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

URi클래스를 못찾는 것을 알게되었다.
proguard-rules.pro 파일을 열고 아래 한 줄을 추가하였고, 앱은 정상적으로 실행되며, 더이상 오류를 노출하지 않았다.

-keep class android.net.*.* { *; }

 

Leave a Reply

error: Content is protected !!