Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question AndroidJavaException: java.lang.ClassNotFoundException:

Discussion in 'Android' started by DJgray3D, Aug 11, 2023.

  1. DJgray3D

    DJgray3D

    Joined:
    Mar 6, 2013
    Posts:
    15
    I'm using a plugin that enables me to take a screenshot. When I run it in Android Studio, I get the following error message when taking the screenshot:

    Code (CSharp):
    1. 2023-08-11 09:53:21.257 18873-18940/? E/Unity: AndroidJavaException: java.lang.ClassNotFoundException: com.secondfury.galleryscreenshot.MainActivity
    2.     java.lang.ClassNotFoundException: com.secondfury.galleryscreenshot.MainActivity
    3.         at java.lang.Class.classForName(Native Method)
    4.         at java.lang.Class.forName(Class.java:454)
    5.         at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    6.         at com.unity3d.player.UnityPlayer.access$300(Unknown Source:0)
    7.         at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source:95)
    8.         at android.os.Handler.dispatchMessage(Handler.java:102)
    9.         at android.os.Looper.loopOnce(Looper.java:226)
    10.         at android.os.Looper.loop(Looper.java:313)
    11.         at com.unity3d.player.UnityPlayer$e.run(Unknown Source:20)
    12.      Caused by: java.lang.ClassNotFoundException: com.secondfury.galleryscreenshot.MainActivity
    13.         at java.lang.Class.classForName(Native Method)
    14.         at java.lang.Class.forName(Class.java:454)
    15.         at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    16.         at com.unity3d.player.UnityPlayer.access$300(Unknown Source:0)
    17.         at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source:95)
    18.         at android.os.Handler.dispatchMessage(Handler.java:102)
    19.         at android.os.Looper.loopOnce(Looper.java:226)
    20.         at android.os.Looper.loop(Looper.java:313)
    21.         at com.unity3d.player.UnityPlayer$e.run(Unknown Source:20)
    22.       at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <00000000000000000000000000000000>:0
    23.       at UnityEngine.AndroidJNISafe.FindClass (System.String name) [0x00000] in <00000000000000000000000000000000>:0
    I'm assuming this means I need to put something in my Android Manifest?
    This is what my Android Manifest currently looks like:

    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2.  
    3. <manifest xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" package="com.company.product">
    4.  
    5.   <uses-permission android:name="android.permission.INTERNET" />
    6.   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    7.   <uses-permission android:name="android.permission.CAMERA"/>
    8.   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    9.   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    10.   <uses-feature android:name="android.hardware.camera.ar" />
    11.   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="replace"/>
    12.   <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    13.   <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
    14.     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />"/>
    15.  
    16.  <supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:anyDensity="true"></supports-screens>
    17.  
    18.  
    19.  <application android:icon="@drawable/app_icon" android:label="@string/app_name">
    20.    <activity android:name="net.sanukin.OverrideUnityActivity"
    21.             android:label="@string/app_name">
    22.              
    23.        <intent-filter>
    24.            <action android:name="android.intent.action.MAIN" />
    25.            <category android:name="android.intent.category.LAUNCHER" />
    26.        </intent-filter>
    27.      
    28.    </activity>
    29.    <meta-data android:name="com.google.ar.core" android:value="required"  tools:replace="android:value"/>
    30.   <meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" />
    31.  </application>
    32. </manifest>
    If I load a project with just this plugin in it, this is the Android Manifest:

    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.     package="com.ryanwebb.androidscreenshot"
    4.     android:versionCode="1"
    5.     android:versionName="1.0" >
    6.  
    7.     <uses-sdk
    8.         android:minSdkVersion="9"
    9.         android:targetSdkVersion="17" />
    10.    
    11.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    12.    
    13.     <application android:allowBackup="true">
    14.         <activity android:name="com.secondfury.galleryscreenshot.MainActivity" />
    15.     </application>
    16.    
    17. </manifest>
    So I took the line <activity android:name="com.secondfury.galleryscreenshot.MainActivity" /> and placed it in my Android Manifest. However, this didn't seem to make any difference, the error was still the same when I ran it.

    Can anyone help?
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    It looks like you have to plugins that try to override the main activity, the screenshot plugin and UniAndroidPermission. As the name suggest, there can only be one main activity, so one of the two will not work. Ideally, a plugin wouldn't override the main activity but instead use a separate activity or a fragment, to avoid conflicts like this.

    Unfortunately, there's no easy fix. You could check the plugins if they provide some alternatives to overriding the main activity, contact their authors and ask them to use a different method, or manually merge the two activities into one – which requires you know a bit of Java and Android development.
     
  3. DJgray3D

    DJgray3D

    Joined:
    Mar 6, 2013
    Posts:
    15
    Thank @Adrian for the reply. That makes sense although unfortunately I don't know much about Java to merge them myself! I have contacted the author of one of the plugins so hopefully they'll be able to help.

    Can I pick your brain some more?
    The only reason I found this error was because it was causing a crash on a device. This is weird because I have tested on several devices, older and newer, that it didn't crash on, however, this one device seemed to take particular offence to the issue.
    Do you know why it would crash on one device but not others? When I run them through Android Studio, I can see the error code on all the devices but it only causes the crash on one.

    Device it crashed on: Samsung S20 FE
    Devices it didn't crash on: Samsung S21 FE (newer) & Samsung A50 (older)