Android Studio app works in emulator but not on a real device

19,719

Solution 1

When you are debugging/ running your app using USB cable connected to Android Studio, for some devices, when you uninstall the app and try to run it again through studio it shows target error.

For this once you uninstall the app, simply disconnect your device from USB cable and connect again.

Solution 2

Use multidexEnabled in your build.gradle:

defaultConfig {
     multiDexEnabled true
}

clean , build and run

Solution 3

In most of the cases, just cleaning and rebuilding the project would do the trick.

Solution 4

Regarding your error, it seems there are missing some SDKs.

Check up your SDK manager if all the needed SDKs and SDK-tools are installed.

Solution 5

You could also check your Build.Gradle file(s). In your case it should look like this:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 23
    }

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

be sure you disabled proguard by setting minifyEnabled to false. this can cause to be sure that this causes no additional errors ;)

Share:
19,719
John O'Neil
Author by

John O'Neil

Updated on June 27, 2022

Comments

  • John O'Neil
    John O'Neil over 1 year

    I am new to android programming and have been having a lot of trouble with it. I finally got my app to work in the emulator on both API 23 and 16 my target is API 16. It runs with no problems on the emulators but when I try to use it on my phone (Google Nexus 5 API 23)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:gravity="center"
        android:orientation="vertical"
        android:background="#F44336"
        android:id="@+id/background"
        tools:context="thereisstuffhere.MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:id="@+id/factTextBox"
            android:textSize="25dp"
            android:textColor="#fff"/>
    </LinearLayout>
    
    
    package there is stuff here too;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.GestureDetector;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        TextView factBox;
        LinearLayout bg;
        Facts factHolder = new Facts(this);
        Backgrounds backs = new Backgrounds();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            factBox = (TextView) findViewById(R.id.factTextBox);
            factBox.setText(factHolder.nextFact());
            bg = (LinearLayout) findViewById(R.id.background);
            bg.setBackgroundColor(getResources().getColor(backs.getBackground()));
            bg.setOnTouchListener(new OnSwipeTouchListener(this)
            {
                public void onSwipeTop()
                {
                    //Toast.makeText(MainActivity.this, "top", Toast.LENGTH_SHORT).show();
                }
                public void onSwipeRight()
                {
                    //Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();
                    prev();
                }
                public void onSwipeLeft()
                {
                    //Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
                    next();
                }
                public void onSwipeBottom()
                {
                    //Toast.makeText(MainActivity.this, "bottom", Toast.LENGTH_SHORT).show();
                }
                public boolean onTouch(View v, MotionEvent event) {
                    return gestureDetector.onTouchEvent(event);
                }
            });
        }
    
        private void next() {
            factBox.setText(factHolder.nextFact());
            bg.setBackgroundColor(getResources().getColor(backs.getBackground()));
        }
    
        private void prev() {
            factBox.setText(factHolder.prevFact());
            bg.setBackgroundColor(getResources().getColor(backs.getBackground()));
        }
    
        public void genFact(View view)
        {
            factBox.setText(factHolder.nextFact());
        }
    

    Facts.java just contains a huge list and two functions that return the next or previous string in the list

    Backgrounds.java does essentially the same thing only it uses an Integer list and the colors.xml file to store the tags etc

    This is my OnTouchListener class:

    package stuff be here;
    
    import android.content.Context;
    import android.view.GestureDetector;
    import android.view.GestureDetector.SimpleOnGestureListener;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    
    public class OnSwipeTouchListener implements OnTouchListener {
        final GestureDetector gestureDetector;
    
        public OnSwipeTouchListener(Context ctx)
        {
            gestureDetector = new GestureDetector(ctx, new GestureListener());
        }
    
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent)
        {
            return false;
        }
    
        private final class GestureListener extends SimpleOnGestureListener{
    
            private static final int SWIPE_THRESHOLD = 100;
            private static final int SWIPE_VELOCITY_THRESHOLD = 100;
    
            @Override
            public boolean onDown(MotionEvent e)
            {
                return true;
            }
    
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
            {
                boolean result = false;
                try{
                    float diffY = e2.getY() - e1.getY();
                    float diffX = e2.getX() - e1.getX();
                    if(Math.abs(diffX) > Math.abs(diffY)){
                        if(Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD){
                            if(diffX > 0){
                                onSwipeRight();
                            }else{
                                onSwipeLeft();
                            }
                        }
                        result = true;
                    }
                    else if(Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD){
                        if(diffY > 0){
                            onSwipeBottom();
                        }else{
                            onSwipeTop();
                        }
                    }
                    result = true;
                } catch (Exception exception){
                    exception.printStackTrace();
                }
                return result;
            }
        }
        public void onSwipeRight(){}
        public void onSwipeLeft(){}
        public void onSwipeTop(){}
        public void onSwipeBottom(){}
    }
    

    I apologise for all of the code but I have been trying to fix this for a while to no avail. I get a lot of errors when I try to run like "Class not found using the boot class loader; no stack trace available" and "Unable to instantiate activity ComponentInf..." I also get some warnings like "ClassLoader referenced unknown path: /data/app/packagenamehere-1/lib/x86_64 and /data/app/packagenamehere-1/lib/arm" even when running in the emulator

    Here is my log cat file:

    12-16 17:26:25.167 22237-22237/com.ubiquity.sciencefacts I/art: Late-enabling -Xcheck:jni
    12-16 17:26:25.197 22237-22243/com.ubiquity.sciencefacts I/art: Ignoring second debugger -- accepting and dropping
    12-16 17:26:25.203 22237-22243/com.ubiquity.sciencefacts I/art: Debugger is no longer active
    12-16 17:26:25.211 22237-22243/com.ubiquity.sciencefacts W/art: Suspending all threads took: 7.324ms
    12-16 17:26:25.235 22237-22237/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-2/lib/arm
    12-16 17:26:25.248 22237-22237/com.ubiquity.sciencefacts D/AndroidRuntime: Shutting down VM
    12-16 17:26:25.254 22237-22237/com.ubiquity.sciencefacts E/AndroidRuntime: FATAL EXCEPTION: main
    12-16 17:26:27.065 22237-22237/com.ubiquity.sciencefacts I/Process: Sending signal. PID: 22237 SIG: 9
    12-16 17:33:18.574 24986-24986/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-1/lib/arm
    12-16 17:33:18.662 24986-24986/com.ubiquity.sciencefacts D/AndroidRuntime: Shutting down VM
    12-16 17:33:18.673 24986-24986/com.ubiquity.sciencefacts E/AndroidRuntime: FATAL EXCEPTION: main
    12-16 17:33:20.800 24986-24986/com.ubiquity.sciencefacts I/Process: Sending signal. PID: 24986 SIG: 9
    12-16 17:34:15.668 25845-25845/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-1/lib/arm
    12-16 17:34:15.687 25845-25845/com.ubiquity.sciencefacts D/AndroidRuntime: Shutting down VM
    12-16 17:34:15.693 25845-25845/com.ubiquity.sciencefacts E/AndroidRuntime: FATAL EXCEPTION: main
    12-16 18:00:52.080 2640-2640/com.ubiquity.sciencefacts I/art: Not late-enabling -Xcheck:jni (already on)
    12-16 18:00:52.142 2640-2640/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-1/lib/x86_64
    12-16 18:00:52.568 2640-2640/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-1/lib/x86_64
    12-16 18:00:52.805 2640-2640/com.ubiquity.sciencefacts W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
    12-16 18:00:53.993 2640-2646/com.ubiquity.sciencefacts W/art: Suspending all threads took: 633.582ms
    12-16 18:00:54.020 2640-2716/com.ubiquity.sciencefacts D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
    12-16 18:00:54.063 2640-2716/com.ubiquity.sciencefacts I/OpenGLRenderer: Initialized EGL, version 1.4
    12-16 18:00:54.091 2640-2716/com.ubiquity.sciencefacts E/EGL_emulation: tid 2716: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
    12-16 18:00:54.091 2640-2716/com.ubiquity.sciencefacts W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f8b8ec90c40, error=EGL_BAD_MATCH
    12-16 18:34:41.488 27869-27869/com.ubiquity.sciencefacts I/art: Late-enabling -Xcheck:jni
    12-16 18:34:41.561 27869-27869/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-2/lib/arm
    12-16 18:34:41.586 27869-27869/com.ubiquity.sciencefacts D/AndroidRuntime: Shutting down VM
    12-16 18:34:41.589 27869-27869/com.ubiquity.sciencefacts E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.ubiquity.sciencefacts, PID: 27869
                                                                               java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ubiquity.sciencefacts/com.ubiquity.sciencefacts.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.ubiquity.sciencefacts.MainActivity" on path: DexPathList[[zip file "/data/app/com.ubiquity.sciencefacts-2/base.apk"],nativeLibraryDirectories=[/data/app/com.ubiquity.sciencefacts-2/lib/arm, /vendor/lib, /system/lib]]
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                Caused by: java.lang.ClassNotFoundException: Didn't find class "com.ubiquity.sciencefacts.MainActivity" on path: DexPathList[[zip file "/data/app/com.ubiquity.sciencefacts-2/base.apk"],nativeLibraryDirectories=[/data/app/com.ubiquity.sciencefacts-2/lib/arm, /vendor/lib, /system/lib]]
                                                                                   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                                   at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                                   at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                                   at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:148) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                                Suppressed: java.lang.ClassNotFoundException: com.ubiquity.sciencefacts.MainActivity
                                                                                   at java.lang.Class.classForName(Native Method)
                                                                                   at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                                                   at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                                                   at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                                                        ... 12 more
                                                                                Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
    12-16 18:34:43.772 27869-27869/com.ubiquity.sciencefacts I/Process: Sending signal. PID: 27869 SIG: 9
    

    IMPORTANT UPDATE: I built the program as an apk and installed it on my phone using that and it worked without issue. Does this imply a target error as was said below in the comments? Would this affect the distribution of my app (for future reference I am not actually distributing this)?

  • John O'Neil
    John O'Neil almost 7 years
    This is on my computer right? I am not sure what else I would need. I believe I have everything though I have been having a lot of problems with the jdk and sdk
  • beal
    beal almost 7 years
    Basicly you need to install the JDK and Android Studio. Android Studio should install all the needed SDKs by itself but sometimes it runs into terrifying errors.
  • beal
    beal almost 7 years
    If everthing is fine with Android Studio, you could also check your build.gradle files and your manifest Didi you tried changing the target version from as described in the comments below your post?
  • Ramesh sambu
    Ramesh sambu almost 7 years
    1.Clean and run the project it will work or remove the instant run in the and run in device
  • John O'Neil
    John O'Neil almost 7 years
    I believe I have the jdk working now. And I have run the sdk and downloaded all the default selected files and the APIs I am using. Should that suffice? I am not sure how I am meant to be changing the target version. I want my app to be able to run on everything greater than API 16
  • John O'Neil
    John O'Neil almost 7 years
    Could this really be a problem though? Surely my extremely simple app doesn't have over 64k references?