Search Unity

Java ClassNotFound exception - why doesn't this work?

Discussion in 'Android' started by Ironic-Chef, Oct 7, 2013.

  1. Ironic-Chef

    Ironic-Chef

    Joined:
    Mar 7, 2013
    Posts:
    12
    Hi Everyone!

    I've been beating my head against this all day. I've been trying to wrap the UnityPlayerActivity class so that I can get orientation from Android devices into my project. I understand how the code *should* work, but no matter what I do the application refuses to see the wrapper class at runtime. Here's the nuts-and-bolts of what I'm trying to get to work...


    Code (csharp):
    1.  
    2.  
    3. JAVA SOURCE CODE STORED IN Assets/Plugins/Android/src/Gyro.java
    4. ---------------------------------------------------------------
    5.  
    6.  
    7. package com.tap.rfyl;
    8.  
    9. import com.unity3d.player.UnityPlayerActivity;
    10. import android.content.Context;
    11. import android.hardware.Sensor;
    12. import android.hardware.SensorEvent;
    13. import android.hardware.SensorEventListener;
    14. import android.hardware.SensorManager;
    15. import android.os.Bundle;
    16. import android.util.Log;
    17. import android.app.Activity;
    18.  
    19. public class Gyro extends UnityPlayerActivity
    20. {
    21.     private static final String TAG = "Gyro_Unity";
    22.     private SensorManager mSensorManager;
    23.     private Sensor accelerometer;
    24.     private Sensor magnetometer;
    25.  
    26.     static public float xmag;
    27.     static public float ymag;
    28.     static public float zmag;
    29.  
    30.     private final SensorEventListener mListener = new SensorEventListener()
    31.     {   float[] mGravity;
    32.         float[] mGeomagnetic;
    33.  
    34.         public void onAccuracyChanged(Sensor sensor, int accuracy)
    35.         {   // Not used
    36.         }
    37.        
    38.         public void onSensorChanged(SensorEvent event)
    39.         {   if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
    40.             {   mGravity = event.values;
    41.             }
    42.             if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
    43.             {   mGeomagnetic = event.values;
    44.             }
    45.             if (mGravity != null  mGeomagnetic != null)
    46.             {   float R[] = new float[9];
    47.                 float I[] = new float[9];
    48.                 boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
    49.                 if (success)
    50.                 {   float orientation[] = new float[3];
    51.                     SensorManager.getOrientation(R, orientation);
    52.                     xmag = orientation[0];  // azimuth
    53.                     ymag = orientation[1];  // pitch
    54.                     zmag = orientation[2];  // roll
    55.                 }
    56.             }
    57.         }
    58.     };
    59.  
    60.     @Override
    61.     protected void onCreate(Bundle savedInstanceState)
    62.     {   mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    63.         accelerometer  = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    64.         magnetometer   = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    65.         super.onCreate(savedInstanceState);
    66.     }
    67.  
    68.     @Override
    69.     protected void onResume()
    70.     {   mSensorManager.registerListener(mListener, accelerometer, SensorManager.SENSOR_DELAY_GAME);
    71.         mSensorManager.registerListener(mListener, magnetometer,  SensorManager.SENSOR_DELAY_GAME);
    72.         super.onResume();
    73.     }
    74.  
    75.     @Override
    76.     protected void onStop()
    77.     {   mSensorManager.unregisterListener(mListener);
    78.         super.onStop();
    79.     }
    80.  
    81.     @Override
    82.     protected void onPause()
    83.     {   mSensorManager.unregisterListener(mListener);
    84.         super.onPause();
    85.     }
    86.    
    87.     public static float getX()
    88.     {   return xmag;
    89.     }
    90.  
    91.     public static float getY()
    92.     {   return ymag;
    93.     }
    94.  
    95.     public static float getZ()
    96.     {   return zmag;
    97.     }
    98. }
    99.  
    100.  
    101.  
    102. COMMAND-LINE COMPILE OF JAVASCRIPT
    103. ----------------------------------
    104.  
    105.  
    106.  
    107. set PATH=%PATH%;C:\Program Files (x86)\Java\jdk1.7.0_40\bin
    108.  
    109. javac Gyro.java -classpath "C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androiddevelopmentplayer\bin\classes.jar" -bootclasspath "G:\adt-bundle-windows-x86-20130917\sdk\platforms\android-16\android.jar" -d . -verbose
    110.  
    111. javap -s -verbose com.tap.rfyl.Gyro
    112.  
    113. jar cvfM ../Gyro.jar com/
    114.  
    115.  
    116.  
    117. MANIFEST FILE STORED IN Assets/Plugins/Android/AndroidManifest.xml
    118. ------------------------------------------------------------------
    119.  
    120.  
    121.  
    122. <?xml version="1.0" encoding="utf-8"?>
    123. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    124.           package="com.tap.rfyl"
    125.           android:versionCode="1"
    126.           android:versionName="1.0">         
    127.     <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="16" />
    128.     <application android:icon="@drawable/app_icon"
    129.                  android:label="@string/app_name">
    130.         <activity android:name=".Gyro"
    131.                   android:screenOrientation="landscape"
    132.                   android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
    133.                   android:label="@string/app_name">
    134.             <intent-filter>
    135.                 <action   android:name="android.intent.action.MAIN" />
    136.                 <category android:name="android.intent.category.LAUNCHER" />
    137.             </intent-filter>
    138.         </activity>
    139.     </application>
    140. </manifest>
    141.  
    142.  
    143.  
    I've tried every conceivable permutation suggested by various sources. I've absolutely tried using a fully qualified path in the "Activity" tag. Nothing has changed the outcome - when I try and run the app on my Android device (a Galaxy Tab 3) it fails immediately with a "ClassNotFound" exception for "Gyro".

    I tried to build a stripped version of the code that only implemented "onCreate" and did nothing else. Got the same error.

    I tried building the .jar file against "androidplayer" and "androiddevelopmentplayer", and nothing changed.

    Basically, I know I must be missing something really obvious, but I can't see it.

    Any help would be greatly appreciated!

    Nick