Search Unity

AndroidJavaException: java.lang.ClassNotFoundException (Another One)

Discussion in 'Android' started by TeamDefiant, Mar 15, 2018.

  1. TeamDefiant

    TeamDefiant

    Joined:
    Mar 29, 2017
    Posts:
    50
    Sorry if this has been answered before, but I couldn't find anything that helped me.

    I'm trying to create a unity project to Interact with an Epson Movario headset but I'm having some trouble accessing its java SDK within unity as I need more hardware access than the movario unity plugin will give me.

    Ok, here's my logcat file. (trimmed, of course)
    I/Unity   ( 7171): Step 01
    I/Unity ( 7171):
    I/Unity ( 7171): (Filename: ./artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
    I/Unity ( 7171):
    I/Unity ( 7171): AndroidJavaException: java.lang.ClassNotFoundException: com.epson.moverio.btcontrol.Bt3sCustomKey
    I/Unity ( 7171): java.lang.ClassNotFoundException: com.epson.moverio.btcontrol.Bt3sCustomKey
    I/Unity ( 7171): at java.lang.Class.classForName(Native Method)
    I/Unity ( 7171): at java.lang.Class.forName(Class.java:306)
    I/Unity ( 7171): at java.lang.Class.forName(Class.java:270)
    I/Unity ( 7171): at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    I/Unity ( 7171): at com.unity3d.player.UnityPlayer.a(Unknown Source)
    I/Unity ( 7171): at com.unity3d.player.UnityPlayer$c$1.handleMessage(Unknown Source)
    I/Unity ( 7171): at android.os.Handler.dispatchMessage(Handler.java:98)
    I/Unity ( 7171): at android.os.Looper.loop(Looper.java:135)
    I/Unity ( 7171): at com.unity3d.player.UnityPlayer$c.run(Unknown Source)
    I/Unity ( 7171): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.epson.moverio.btcontrol.Bt3sCustomKey" on path: DexPathList[[zip file "/data/app/com.Accenture.Epson_Online_Test-1/base.apk"],nativeLibraryDirectories=[/data/app/com.Accenture.Epson_Online_Test-1/lib/arm, /vendor/lib, /system/lib]]
    I/Unity ( 7171): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:

    Package: com.epson.moverio.btcontrol.
    Class Bt3sCustomKey.

    My unity code:
    Code (CSharp):
    1. #if UNITY_ANDROID
    2.         AndroidJavaObject activityContext = null;
    3.         AndroidJavaObject bt3sCustomKey = null;
    4.  
    5.         // First, obtain the current activity context
    6.         using (AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    7.         {
    8.             activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
    9.         }
    10.  
    11.         Debug.Log("Step 01");
    12.         using (AndroidJavaClass pluginClass = new AndroidJavaClass("com.epson.moverio.btcontrol.Bt3sCustomKey"))
    13.         {
    14.             Debug.Log("Step 02");
    15.             if (pluginClass != null)
    16.             {
    17.                  Debug.Log("Step 03");
    18.                  bt3sCustomKey = pluginClass.CallStatic<AndroidJavaObject>("Bt3sCustomKey", activityContext);
    19.                  Debug.Log("Step 04");
    20.                  bt3sCustomKey.Call("setKeyEnable", makeParamList(DPAD_UP, false));
    21.                  bt3sCustomKey.Call("setKeyEnable", makeParamList(DPAD_DOWN, false));
    22.                  bt3sCustomKey.Call("setKeyEnable", makeParamList(DPAD_LEFT, false));
    23.                  bt3sCustomKey.Call("setKeyEnable", makeParamList(DPAD_RIGHT, false));
    24.                  bt3sCustomKey.Call("setKeyEnable", makeParamList(DPAD_CENTER, false));
    25.                  bt3sCustomKey.Call("setKeyEnable", makeParamList(VOLUME_UP, false));
    26.                  bt3sCustomKey.Call("setKeyEnable", makeParamList(VOLUME_DOWN, false));
    27.                  bt3sCustomKey.Call("setKeyEnable", makeParamList(FUNCTION, false));
    28.                  bt3sCustomKey.Call("setKeyEnable", makeParamList(BACK, false));
    29.                  bt3sCustomKey.Call("setKeyEnable", makeParamList(HOME, false));
    30.             }
    31.         }
    32. #endif
    I've been searching all day for an answer to this problem, and I'm at my wits end! Can anyone provide any help?!?! (My issue IS NOT the 'JNI Init'd AndroidJavaClass with null ptr!' error, I know it doesn't work in the editor!)

    The BT300Ctrl.jar file is located in 'Assets > Plugins > Android' and if anyone wants it, here's the code from the jar file. (With the machine code bits removed to save space.) I Can't change the java code.

    Code (CSharp):
    1. public class Bt3sCustomKey {
    2.     public static final int DPAD_UP = 19;
    3.     public static final int DPAD_DOWN = 20;
    4.     public static final int DPAD_LEFT = 21;
    5.     public static final int DPAD_RIGHT = 22;
    6.     public static final int DPAD_CENTER = 23;
    7.     public static final int VOLUME_UP = 24;
    8.     public static final int VOLUME_DOWN = 25;
    9.     public static final int FUNCTION = 119;
    10.     public static final int BACK = 4;
    11.     public static final int HOME = 3;
    12.     public static final int APP_SWITCH = 187;
    13.     public static final int POWER = 26;
    14.     private static String TAG;
    15.     private static final String BT3S_CUSTOM_KEY_ASSIGN = "Bt3s.CustomkeyAssign";
    16.     private static final String BT3S_CUSTOM_KEY_ENABLE = "Bt3s.CustomkeyEnable";
    17.     private static final String BT3S_CUSTOM_KEY_RESET = "Bt3s.CustomkeyReset";
    18.     private static HashMap<Integer, String> mPhyKeyMap;
    19.     private static HashMap<Integer, String> mKeyCode;
    20.     private android.content.Context mContext;
    21.  
    22.     public Bt3sCustomKey(android.content.Context context) {
    23.         // <editor-fold defaultstate="collapsed" desc="Compiled Code">
    24.     }
    25.  
    26.     public boolean setKeyAssign(int PhyKey, int KeyCode) {
    27.         // <editor-fold defaultstate="collapsed" desc="Compiled Code">
    28.     }
    29.  
    30.     public int getKeyAssign(int PhyKey) {
    31.         // <editor-fold defaultstate="collapsed" desc="Compiled Code">
    32.     }
    33.  
    34.     public boolean isKeyEnable(int PhyKey) {
    35.         // <editor-fold defaultstate="collapsed" desc="Compiled Code">
    36.     }
    37.  
    38.     public boolean setKeyEnable(int PhyKey, boolean enableKey) {
    39.         // <editor-fold defaultstate="collapsed" desc="Compiled Code">
    40.     }
    41.  
    42.     public void resetToDefault() {
    43.         // <editor-fold defaultstate="collapsed" desc="Compiled Code">
    44.     }
    45. }
     
    DMorock likes this.
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Select the jar file and look into it's settings in inspector, it should be marked as compatible with Android.
     
  3. TeamDefiant

    TeamDefiant

    Joined:
    Mar 29, 2017
    Posts:
    50
    Thanks for replying, but it already is.
     
    DMorock likes this.
  4. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    Are you using Internal or Gradle build system?
     
  5. TeamDefiant

    TeamDefiant

    Joined:
    Mar 29, 2017
    Posts:
    50
    Internal.
     
  6. TeamDefiant

    TeamDefiant

    Joined:
    Mar 29, 2017
    Posts:
    50
    Ok, I've solved this issue now.

    I changed the .jar file to a different version and I no longer get this error. (I now get another issue, which will be posted about shortly.)

    Thanks for the replies!
     
  7. Sergey972

    Sergey972

    Joined:
    Mar 20, 2018
    Posts:
    1
    Very good ;( fix issue and not explain to others how it fix.
     
  8. AppDevPareeta

    AppDevPareeta

    Joined:
    Jun 17, 2019
    Posts:
    2
    Its cause of Obfuscation. Goto Player Setting > Publishing Setting > Minify and select none for Release.
     
  9. Jovaan

    Jovaan

    Joined:
    Aug 1, 2014
    Posts:
    12
    Thanks AppDevPareeta. Searched for this problem for 2 hours, and this new "Minify" setting is the answer. It obfuscated Java classes that need original names!
     
    kasym_ likes this.
  10. ShaheelS

    ShaheelS

    Joined:
    Jun 22, 2018
    Posts:
    4
    @AppDevPareeta you are life SAVER thank you, your solution work for me
     
    kasym_ likes this.
  11. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    thanks
     
  12. JuliusM

    JuliusM

    Unity Technologies

    Joined:
    Apr 17, 2013
    Posts:
    835
    The problem is probably stripping. Classes accessed only through reflection get stripped by proguard. Disabling minification is not the best solution as it will leave unused code and can cause dex limit issue. Alternatively keeping minify enabled and configuring proguard to keep the classes that you need is a better solution. You can enable proguard configuratoin by enabling "User Proguard File" checkbox in the player settings (above the Minify option). Then you can specify which classes should be kept in the "Assets/Plugins/Android/proguard-user.txt" file. It is also possible to disable obfuscation in the same way if that is necessary. Take a look at "Keep options" and "Obfuscation options" here https://www.guardsquare.com/en/products/proguard/manual/usage.
     
  13. manson007

    manson007

    Joined:
    Oct 29, 2017
    Posts:
    1
    Im getting same error please help
     
  14. zarkozarko

    zarkozarko

    Joined:
    Sep 1, 2020
    Posts:
    2
    Man, you saved many lives :D
     
    visionnaireMedia likes this.
  15. Dmytro1984

    Dmytro1984

    Joined:
    Jan 16, 2018
    Posts:
    1
    Thank you! That really helped! Spent 4 hours digging whole Internet - finally Google took me here. However, checking now the answer by @JuliusM, as this definitely looks like the fix that needs to be applied for a long-run solution.
     
    DMorock likes this.
  16. marktoddy

    marktoddy

    Joined:
    Jun 22, 2021
    Posts:
    3
    As the name suggests this exception in Java occurs when JVM tries to load a particular class and doesn't found the requested class in the classpath you specified. This means that, your classpath is broken. Java ClassNotFoundException thrown when an application tries to load in a class through its string name using:

    • The forName method in class Class.
    • The findSystemClass method in class ClassLoader .
    • The loadClass method in class ClassLoader.

    When you get a ClassNotFoundException , it means that the Java Virtual Machine has traversed the entire classpath you specified and not found the class you've attempted to reference. The one and only solution is to check your classpath carefully.

    Verify the name of the requested class is correct and the specified .jar file exists in your classpath. If not, you must explicitly add it to your application’s classpath.

    If it's present in your classpath then there is high chance that your classpath is getting overridden or application is using classpath specified in jar file or start-up script and to fix that you need to find the exact classpath used by your application.
     
  17. SINAPGAMES

    SINAPGAMES

    Joined:
    Jun 7, 2021
    Posts:
    2
    Will you Marry me?
     
    mohammedghabyen and Nevazhnov like this.
  18. guneyozsan

    guneyozsan

    Joined:
    Feb 1, 2012
    Posts:
    99
    In my case Minify was already "None" and proguard keep options or any Minify option didn't help.

    I was able resolve it by moving the plugin from
    Assets/Plugins/Android/MyPlugin/myplugin.aar
    to
    Assets/Plugins/Android/libs/MyPlugin/myplugin.aar
    .
     
    ilmario likes this.
  19. PHL1

    PHL1

    Joined:
    Jul 25, 2017
    Posts:
    29
    thank you, for me it was Debug.
     
  20. shefyg

    shefyg

    Joined:
    Oct 29, 2014
    Posts:
    4
    This worked for me, just wanted to add Visual to help people find this solution. @AppDevPareeta -Thank''s upload_2023-5-2_10-28-49.png