-1

My app work well with the android emulator.
But, when I compiled it into an apk. It crash with following error.

java.lang.RuntimeException: Unable to start activity ComponentInfo{tk.myessentialoils.a16personalities/tk.myessentialoils.a16personalities.TitleActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2921)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1639)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6662)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: 
Error inflating class androidx.constraintlayout.widget.ConstraintLayout
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class 
androidx.constraintlayout.widget.ConstraintLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:651)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.inflate(LayoutInflater.java:496)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at tk.myessentialoils.a16personalities.TitleActivity.onCreate(TitleActivity.java:23)
at android.app.Activity.performCreate(Activity.java:7074)
at android.app.Activity.performCreate(Activity.java:7065)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2796)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2921)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1639)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6662)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: 
Landroidx/constraintlayout/widget/R$styleable;
at androidx.constraintlayout.widget.ConstraintLayout.init(ConstraintLayout.java:590)
at androidx.constraintlayout.widget.ConstraintLayout.<init>(ConstraintLayout.java:567)
... 24 more
Caused by: java.lang.ClassNotFoundException: Didn't find class 
"androidx.constraintlayout.widget.R$styleable" on path: DexPathList[[zip file 
"/data/app/tk.myessentialoils.a16personalities-McRu5v88h3OOg6KJL_I- 
jg==/base.apk"],nativeLibraryDirectories=[/data/app/tk.myessentialoils.a16personalities- 
McRu5v88h3OOg6KJL_I-jg==/lib/arm, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 26 more

So, my questions are
1. What make the difference between emulator installation and apk installation
2. How do I fix the error

Here is my build, I generated using the android studio build>generate signed bundle/apk

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "tk.myessentialoils.a16personalities"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 2
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'com.google.android.material:material:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
    implementation 'com.google.android.gms:play-services-ads:18.2.0'
}
Tony Ming
  • 101
  • 1
  • 1
  • 11
  • try this https://stackoverflow.com/a/57038018/9024123 – Rohit Chauhan Oct 10 '19 at 15:25
  • post your build.gradle – mohammadReza Abiri Oct 10 '19 at 15:26
  • "difference between emulator installation and apk installation" do you mean that it fails to work on a *real device* or that you make an APK and the same emulator that worked before, now fails to work? If so, how did you generate this APK? what was the command you used? Are you using Proguard? Minify? There are many questions that you'll need to answer here. The problem you have is that _the apk you "generated" somehow, does NOT have the correct classes in its classpath, likely, they were stripped by some process_. – Martin Marconcini Oct 10 '19 at 17:17
  • @MartinMarconcini What I meant is, this code works fine with the android studio emulator. But the same code failed with the above error on a real phone. (Crash on opening) – Tony Ming Oct 11 '19 at 05:36
  • And how did you generate your APK? Also, post your relevant gradle files, what dependencies do you have? target api? min api? etc. – Martin Marconcini Oct 11 '19 at 09:12
  • @Rj_Innocent_Coder Thank you. Your link is a great help for me to discover the answer. – Tony Ming Oct 12 '19 at 01:24

1 Answers1

0

For the convenience of people who encounter this in future.

Error inflating constraint layout after migrating to androidx
There is a bug.

https://stackoverflow.com/a/52882787/9190861

I don't know why, but 1.1.2 just chokes to death. Probably some compatibility program that the nice people Google missed. They tend to do that a lot; welcome to Android programming! – Scott Biggs

My project work perfectly after changing 1.1.2 to 1.1.3 in the implementation in build gradle.

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
Tony Ming
  • 101
  • 1
  • 1
  • 11