0

I'm trying to build my Android plugin for Unity. But when I do, I get the follow error log in Logcat:

12-11 14:45:38.745: E/AndroidRuntime(26806): FATAL EXCEPTION: main
12-11 14:45:38.745: E/AndroidRuntime(26806): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.marc/com.example.marc.CompassActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.marc.CompassActivity" on path: DexPathList[[zip file "/data/app/com.example.marc-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.marc-1, /vendor/lib, /system/lib]]
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.os.Looper.loop(Looper.java:137)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.app.ActivityThread.main(ActivityThread.java:5103)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at java.lang.reflect.Method.invokeNative(Native Method)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at java.lang.reflect.Method.invoke(Method.java:525)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at dalvik.system.NativeStart.main(Native Method)
12-11 14:45:38.745: E/AndroidRuntime(26806): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.marc.CompassActivity" on path: DexPathList[[zip file "/data/app/com.example.marc-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.marc-1, /vendor/lib, /system/lib]]
12-11 14:45:38.745: E/AndroidRuntime(26806):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-11 14:45:38.745: E/AndroidRuntime(26806):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
12-11 14:45:38.745: E/AndroidRuntime(26806):    ... 11 more

I did a search on to why this is happening and I discovered it is because of a problem with my intent. And that I needed to add my activity to my Manifest file. However, this is my manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.marc"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="7" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity android:name="com.example.marc.CompassActivity"
        android:label="@string/app_name">
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
</application>

And from what I can see, my intent is there in the line <activity android:name="com.example.marc.CompassActivity" unless my thinking behind this is entirely wrong?

Just for completions sake, here is my java file:

  package com.example.marc;
import com.unity3d.player.UnityPlayerActivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;
import android.app.Activity;

public class CompassActivity extends UnityPlayerActivity 
{
    private static final String TAG = "Compass";

    private SensorManager mSensorManager;
    private Sensor mSensor;

    static public float xmag;
    static public float ymag;
    static public float zmag;

    private final SensorEventListener mListener = new SensorEventListener() 
    {
        public void onSensorChanged(SensorEvent event)
        {
        if (Config.DEBUG) Log.d(TAG,
        "sensorChanged (" + event.values[0] + ", " + event.values[1] + ", " + event.values[2] + ")");

            xmag = event.values[0];
            ymag = event.values[1];
            zmag = event.values[2];
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) 
        {
        }
    };

    @Override
    protected void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    }

    @Override
    protected void onResume()
    {
        if (Config.DEBUG) Log.d(TAG, "onResume");
        super.onResume();

        mSensorManager.registerListener(mListener, mSensor,
        SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onStop()
    {
        if (Config.DEBUG) Log.d(TAG, "onStop");
        mSensorManager.unregisterListener(mListener);
        super.onStop();
    }

    public static float getX()
    {
        return xmag;
    }

    public static float getY() 
    {
        return ymag;
    }

    public static float getZ()
    {
        return zmag;
    }
}

Again, as per my thinking, this is what I should be passing to my Manifest file, such, it is called "CompassActivity". Clearly though I must have done something wrong otherwise I wouldn't be getting the errors I am. Could someone please tell me what it is I am doing wrong / missing?

edit

Latest LogCat files:

   12-11 16:32:16.957: E/AndroidRuntime(29055): FATAL EXCEPTION: main
12-11 16:32:16.957: E/AndroidRuntime(29055): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.marc/com.example.marc.CompassActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.marc.CompassActivity" on path: DexPathList[[zip file "/data/app/com.example.marc-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.marc-2, /vendor/lib, /system/lib]]
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.os.Looper.loop(Looper.java:137)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.app.ActivityThread.main(ActivityThread.java:5103)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at java.lang.reflect.Method.invokeNative(Native Method)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at java.lang.reflect.Method.invoke(Method.java:525)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at dalvik.system.NativeStart.main(Native Method)
12-11 16:32:16.957: E/AndroidRuntime(29055): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.marc.CompassActivity" on path: DexPathList[[zip file "/data/app/com.example.marc-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.marc-2, /vendor/lib, /system/lib]]
12-11 16:32:16.957: E/AndroidRuntime(29055):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-11 16:32:16.957: E/AndroidRuntime(29055):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
12-11 16:32:16.957: E/AndroidRuntime(29055):    ... 11 more
N0xus
  • 2,674
  • 12
  • 65
  • 126

3 Answers3

1

I think

 <activity android:name="com.example.marc.CompassActivity"

SHOULD BE

   <activity android:name="CompassActivity"

(you already mentioned "com.example.marc" in package attribute The value of android:name should be name of activity class only. Since your manifest file says main activity class is "com.example.marc.CompassActivity", runtime environment looks for it , but it doesn't find it as your main activity class file is named as "CompassAcitvity")

[EDIT]

Main issue is that the runtime is unable to Instantiate the MainActivity because it is not able to find the class "com.example.marc.CompassActivity" in DexPathList

See method "findClass()" line 310-323

If a class is successfully found then a class object is returned other wise null it is returned to BaseDexClassLoader(see line 58) And ClassNotFoundException is thrown in case null is received which is happening in this case.

I think either main activity class is excluded during build process or it has some different name unlike mentioned in manifest.

vivek
  • 198
  • 4
  • 10
  • lol ... http://developer.android.com/guide/topics/manifest/activity-element.html#nm – Selvin Dec 11 '13 at 15:51
  • @Selvin ....i am wrong here... I put activity name as only class name(without any period in beginning) and it has worked every time. I should have tried to replicate the error before answering.But stacktrace gave me this impression that this could be possible issue – vivek Dec 11 '13 at 16:04
  • @selvin if you see error message "Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.marc.CompassActivity"" what are your views on this? – vivek Dec 11 '13 at 16:09
  • Tried that, but I still get the same message. – N0xus Dec 11 '13 at 16:19
  • @N0xus ..can you please put new stacktrace in comment – vivek Dec 11 '13 at 16:27
  • Done, with a build I literally just ran – N0xus Dec 11 '13 at 16:33
  • @N0xus ok...does UnityPlayerActivity extends Activity? – vivek Dec 11 '13 at 16:47
  • @N0xus...dude please read my previous comment carefully, anyway replace CompassActivity extends UnityPlayerActivity with CompassActivity extends Activity ....and see what happens (i was going through http://stackoverflow.com/questions/10866431/android-activity-classnotfoundexception-tried-everything?rq=1) – vivek Dec 11 '13 at 16:53
  • @N0xus can you tell me more about class UnityPlayerActivity IS IT A SUBCLASS of Activity class? – vivek Dec 11 '13 at 17:51
  • I can't, all I know is that I followed a tutorial. Here is a link about Unity android extensions: http://docs.unity3d.com/Documentation/Manual/PluginsForAndroid.html – N0xus Dec 11 '13 at 17:54
  • @N0xus ...just for testing purpose can change line " CompassActivity extends UnityPlayerActivity" to "CompassActivity extends Activity" and see if error is still there? So we'll know if there is something wrong with UnityPlayerActivity.java. – vivek Dec 11 '13 at 18:00
  • I assure you, there is no problem with the Unity Player Activiy – N0xus Dec 11 '13 at 18:46
  • I agree with you....I went through the link you mentioned also i checked one [tutorial](http://www.platoevolved.com/blog/programming/android/creating-an-android-plugin-for-unity/) – vivek Dec 11 '13 at 18:48
  • @N0xus ...you are generating apk file?...can you give me the generated file? – vivek Dec 11 '13 at 21:28
  • I am, via Unity, and I'm sorry but I can't send the apk. – N0xus Dec 12 '13 at 10:53
0

Your UnityPlayerActivity class is not found actually but Compiler shows as CompassActivity not found.

If you have UnityPlayerActivity in library and that is linked to you app then double check that your Project-->Right Click-->Properties-->Java Build Path--->Order and Export has your project checked.

TNR
  • 5,839
  • 3
  • 33
  • 62
  • I checked and I have my classes.jar file added which is in Unity\Editor\Data\PlaybackEngines\androidplayer\bin is thwat what your meaning? – N0xus Dec 11 '13 at 16:25
  • as I said in my answer please check Android Private Dependencies are checked in Order and Export. – TNR Dec 12 '13 at 07:27
0

Following @TNR's answer - Your UnityPlayerActivity class is not found actually but LogCat shows as CompassActivity not found.

What you can try to do is:

  1. Rebuild your project (just for testing) using the original UnityPlayerActivity in your manifest. is it working? (if not then fix this problem first)

  2. Use JD to decompile the JAR where CompassActivity is located. Does it inherit from UnityPlayerActivity? In my case there was a problem with the compiler and it changed the inheritance to NPUnityPlayerActivity which was not found. If this problem occurs you need to recompile your code and make sure the inheritance is correct.

Good Luck!

Nimo
  • 7,984
  • 5
  • 39
  • 41