Search Unity

Crash at app startup: FATAL EXCEPTION: main

Discussion in 'Android' started by Flarup, Aug 12, 2019.

  1. Flarup

    Flarup

    Joined:
    Jan 7, 2010
    Posts:
    164
    I have a project in which I have several native Android plugins. Keeping them uptodate is a bit tricky, since some Unity plugins from the Asset Store depends on the same native Android plugins. Typically I would get problems while building the Gradle project, in case I had two Unity plugins that included the same native Android plugins.

    However, now I'm running into a new problem with native Android plugins. The entire build process succeeds, but when I run the apk on an Android device it instantly crashes due to some missing code. The error log I get is the following:

    08-12 20:14:25.322 1681 1681 E AndroidRuntime: FATAL EXCEPTION: main
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: Process: com.arblox.v2, PID: 1681
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: java.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.arblox.v2-I2bYWJsC3kLY-jqkbMlUog==/base.apk"],nativeLibraryDirectories=[/data/app/com.arblox.v2-I2bYWJsC3kLY-jqkbMlUog==/lib/arm64, /data/app/com.arblox.v2-I2bYWJsC3kLY-jqkbMlUog==/base.apk!/lib/arm64-v8a, /system/lib64, /system/vendor/lib64]]
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at android.app.ActivityThread.installProvider(ActivityThread.java:6747)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at android.app.ActivityThread.installContentProviders(ActivityThread.java:6289)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6204)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at android.app.ActivityThread.access$1200(ActivityThread.java:237)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1785)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at android.os.Looper.loop(Looper.java:214)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7050)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.arblox.v2-I2bYWJsC3kLY-jqkbMlUog==/base.apk"],nativeLibraryDirectories=[/data/app/com.arblox.v2-I2bYWJsC3kLY-jqkbMlUog==/lib/arm64, /data/app/com.arblox.v2-I2bYWJsC3kLY-jqkbMlUog==/base.apk!/lib/arm64-v8a, /system/lib64, /system/vendor/lib64]]
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at android.app.AppComponentFactory.instantiateProvider(AppComponentFactory.java:121)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at androidx.core.app.CoreComponentFactory.instantiateProvider(CoreComponentFactory.java:62)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: at android.app.ActivityThread.installProvider(ActivityThread.java:6731)
    08-12 20:14:25.322 1681 1681 E AndroidRuntime: ... 10 more



    To me it looks like some native Android code is missing from the apk, but I have no idea about how to fix it.

    Among the list if things I already have tried are the following:

    - Using both "Resolve" and "Force Resolve" in the Android Resolver menu.
    - Restarting Unity before building the apk.
    - "Delete Resolved Libraries" in the Android Resolver menu.
    - Enabling and disabling automatic resolving of Android libraries.


    My question is: How do I "tell" Unity that this missing Android native code must be included in my apk?

    Thank you all VERY much in advance for all your help!


    Kind regards,
    Uffe Flarup
     
  2. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    192
  3. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,963
    If you are having androidX libraries, make sure you don't refer android.support.* from your manifest file. Use the corresponding androidX updated path for file proivder reference ->
    androidx.core.content.FileProvider
     
    Flarup likes this.
  4. Flarup

    Flarup

    Joined:
    Jan 7, 2010
    Posts:
    164
    Hi @Voxel-Busters. Thanks for bringing my attention to the androidX libraries. As you suggest, a bunch of dependencies to androidX libraries was added as a part of the update I'm working on.

    Also, I managed to find the manifest file that references the fileprovider, and I changed the value to be "androidx.core.content.FileProvider" instead. My apk will now start on the device (progress, hooray!), and I no longer get the crash I originally had. Unfortunately, the "old" plugin that used the "old" fileprovider now crashes, and I get the following error a bit later in the app:

    08-13 11:18:02.928 29422 29604 E Unity : AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/content/ContextCompat;
    08-13 11:18:02.928 29422 29604 E Unity : java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/content/ContextCompat;
    08-13 11:18:02.928 29422 29604 E Unity : at com.partagames.unity.permissiongranter.PermissionGranter.isPermissionGranted(PermissionGranter.java:33)
    08-13 11:18:02.928 29422 29604 E Unity : at com.partagames.unity.permissiongranter.unity.PermissionGranterUnity.isPermissionGranted(PermissionGranterUnity.java:27)


    This plugin have worked perfectly for a long time, but now breaks after the androidX libraries was added to the project.

    Is the usage of androidX libraries "incompatible" with "old" android libraries? Or is there some way to have them co-exist in the same project?

    Thanks a lot for all your help.
     
    Qbit86 likes this.
  5. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,963
    Its possible to have both old and new androidX packages exist in same project. Thanks to Jetifier which makes it possible.

    All our plugins as of now not yet upgraded to androidX but uses Jetifier because we do find other many plugins haven't updated to androidX. So, this scenario will be quite common for some more time until we find the right time.


    Coming to your solution, Please check our previous posts where you can automate the process of Jetifier with a script. Adding the gradle patch script should make it work with old android support libraries.

    Do let me know if you can't find it.
     
    Flarup likes this.
  6. Flarup

    Flarup

    Joined:
    Jan 7, 2010
    Posts:
    164
    I can see you're very active in the forums here, @Voxel-Busters. Can you please give me a link to the post you suggest I should take a further look at?

    Also (and I'm probably wrong here, since I haven't done much native Android development), I can't help but thinking that Jetifier is CAUSING problems here, rather than solving them. Basically, I had some native Android plugins that worked perfectly fine, but then I upgraded some other plugins which was using Jetifier, and then the usage of Jetifier caused my old plugins to no longer work. And now I can't move forward until the old plugins I have, have been updated to support Jetifier.

    As mentioned earlier, I may be misunderstanding something here. But what exactly is the root cause of the sudden crash I'm getting?

    Thank you VERY much for your help.


    Kind regards,
    Uffe Flarup
     
  7. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,963
    ok, no worries. I was a bit lazy to dig through :p

    In this post, grab the second script and keep in Editor folder. Also, you need to have the following checked.
    1. Have at-least 1.2.119 Play Services Resolver. You can download the latest one from here.
    2. In Play Services Resolver, under Android Settings (from Assets -> Play Services Resolver -> Android -> Settings), enable "Uses Jetifier" if not enabled.
    3. Force resolve from the menu again

    Coming to other plugins, they need to set the required flags to consider them for Jetifier process. The shared script will do that job. I'm assuming this could be the problem but if you still see the issue, i need to look into more details.

    Thanks,
    VB Team
     
    allangamesxbr and Flarup like this.
  8. Flarup

    Flarup

    Joined:
    Jan 7, 2010
    Posts:
    164
    Thanks a lot for the script. Adding that script and manually changing my manifest files seems to fix the problem.

    Needs to test a bit more, and see if everything still works as expected. But at first glance everything seems good.

    Thanks you very much for the help!
     
  9. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,963
    It should work as it enables the jetifier automatically on export :)
     
  10. BtMuntazir

    BtMuntazir

    Joined:
    Nov 25, 2021
    Posts:
    7

    Hi, I am not getting any success to get the uri with the File Providor..

    AndroidJavaClass fileProvider = new AndroidJavaClass("androidx.core.content.FileProvider");

    Exception occurred while executing the above line:

    I want using the File Provider to open a pdf file or read the file from the downloaded storage. but I am getting error "java.lang.ClassNotFoundException: androidx.core.content.FileProvider".
     
  11. BtMuntazir

    BtMuntazir

    Joined:
    Nov 25, 2021
    Posts:
    7
    Hi, I am not getting any success to get the uri with the File Providor..

    AndroidJavaClass fileProvider = new AndroidJavaClass("androidx.core.content.FileProvider");

    Exception occurred while executing the above line:

    I want using the File Provider to open a pdf file or read the file from the downloaded storage. but I am getting error "java.lang.ClassNotFoundException: androidx.core.content.FileProvider".



    Please find below the complete code that I am using to download the APK from the url and auto install the app.

    IEnumerator downLoadFromServer()
    {
    Debug.Log("Muntazir New Update Apk Code 1");
    print("Muntazir New Update Apk Code 1");

    string url = "https://www.portal.micromedinc.com/files/apk/uploads/VF2000_Controller-255-1_2_145.apk";

    Debug.Log("Muntazir New Update Apk Code 2");
    print("Muntazir New Update Apk Code 2");
    string savePath = Path.Combine(Application.persistentDataPath, "data");

    savePath = Path.Combine(savePath, "AntiOvr.apk");
    Dictionary<string, string> header = new Dictionary<string, string>();
    string userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"; header.Add("User-Agent", userAgent);

    WWW www = new WWW(url, null, header);
    Debug.Log("Muntazir New Update Apk Code 3");
    print("Muntazir New Update Apk Code 3");
    Debug.Log("Muntazir New Update Apk Save path= " + savePath);

    //while (!www.isdone)
    //{

    // //Must yield below/wait for a frame
    // GameObject.Find("TextDebug").GetComponent<Text>().text = "Stat: " + www.progress;
    //
    //}

    byte[] yourBytes = www.bytes; Debug.Log("Muntazir New Update Apk Code 4");

    // GameObject.Find("TextDebug").GetComponent<Text>().text = "Done downloading. Size: " + yourBytes.Length;


    print("Muntazir New Update Apk Code 4a");

    //Create Directory if it does not exist
    if (!Directory.Exists(Path.GetDirectoryName(savePath))) { print("Muntazir New Update Apk Code Create Directory"); Directory.CreateDirectory(Path.GetDirectoryName(savePath));
    // GameObject.Find("TextDebug").GetComponent<Text>().text = "Created Dir";
    } Debug.Log("Muntazir New Update Apk Code 5");

    print("Muntazir New Update Apk Code 5");

    try {
    //Now Save it
    System.IO.File.WriteAllBytes(savePath, yourBytes); Debug.Log("Saved Data to: " + savePath.Replace("/", "\\"));
    // GameObject.Find("TextDebug").GetComponent<Text>().text = "Saved Data";

    Debug.Log("Muntazir New Update Apk Code 6");

    } catch (Exception e)
    {

    Debug.Log("Muntazir New Update Apk Code 7");
    Debug.LogWarning("Failed To Save Data to: " + savePath.Replace("/", "\\")); Debug.LogWarning("Error: " + e.Message);
    // GameObject.Find("TextDebug").GetComponent<Text>().text = "Error Saving Data";
    }

    //Install APK
    installApp(savePath); yield return true; } private bool installApp(string apkPath) { bool success = true;
    // GameObject.Find("TextDebug").GetComponent<Text>().text = "Installing App";

    Debug.Log("Muntazir New Update Apk Code 8"); print("Muntazir New Update Apk Code 8"); try {

    //Get Activity then Context
    AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); AndroidJavaObject unityContext = currentActivity.Call<AndroidJavaObject>("getApplicationContext");


    //com.unitysharing
    //Get the package Name
    string packageName = unityContext.Call<string>("getPackageName"); string authority = packageName + ".fileprovider"; Debug.Log("Muntazir New Update Apk Code 9 " + packageName); AndroidJavaClass intentObj = new AndroidJavaClass("android.content.Intent"); string ACTION_VIEW = intentObj.GetStatic<string>("ACTION_VIEW"); AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", ACTION_VIEW); int FLAG_ACTIVITY_NEW_TASK = intentObj.GetStatic<int>("FLAG_ACTIVITY_NEW_TASK"); int FLAG_GRANT_READ_URI_PERMISSION = intentObj.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION"); Debug.Log("Muntazir New Update Apk Code 9 a");


    //File fileObj = new File(String pathname);
    AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File", apkPath); Debug.Log("Muntazir New Update Apk Code 9 b "+ apkPath);

    //FileProvider object that will be used to call it static function
    AndroidJavaClass fileProvider = new AndroidJavaClass("androidx.core.content.FileProvider");
    // getUriForFile(Context context, String authority, File file)

    Debug.Log("Muntazir New Update Apk Code 9 c"); Debug.Log("Muntazir New Update Apk Code 9 fileProvider " + fileProvider.ToString());



    AndroidJavaObject uri = fileProvider.CallStatic<AndroidJavaObject>("getUriForFile", unityContext, authority, fileObj);

    Debug.Log("Muntazir New Update Apk Code 10");
    intent.Call<AndroidJavaObject>("setDataAndType", uri, "application/vnd.android.package-archive");
    intent.Call<AndroidJavaObject>("addFlags", FLAG_ACTIVITY_NEW_TASK);
    intent.Call<AndroidJavaObject>("addFlags", FLAG_GRANT_READ_URI_PERMISSION); currentActivity.Call("startActivity", intent);

    Debug.Log("Muntazir New Update Apk Code 11");

    // GameObject.Find("TextDebug").GetComponent<Text>().text = "Success";
    }
    catch (System.Exception e) {

    Debug.Log("Muntazir New Update Apk Code 12 " + e.Message );

    // GameObject.Find("TextDebug").GetComponent<Text>().text = "Error: " + e.Message;
    success = false;
    }
     
  12. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,963
    Did you add androidx core libs as dependency in your project?
     
  13. BtMuntazir

    BtMuntazir

    Joined:
    Nov 25, 2021
    Posts:
    7
    Yes I am using the androidX dependency in our project
     
  14. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,963
    I think most likely it has something to do with your manifest file.

    Search in your project for the manifest files having "android.support.v4.content.FileProvider" and replace it with "androidx.core.content.FileProvider"

    Let me know how it goes!
     
  15. BtMuntazir

    BtMuntazir

    Joined:
    Nov 25, 2021
    Posts:
    7
    Hello,

    I already used the androidx. Please check my manifest screenshot.

    upload_2021-12-9_10-17-38.png
     
  16. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,963
    Once you try to make a build, you get Temp/gradleOut folder. Try searching for "android.support.v4.content.FileProvider" in that folder and pass me the details.
     
  17. BtMuntazir

    BtMuntazir

    Joined:
    Nov 25, 2021
    Posts:
    7
    Hi, I am not getting the Temp/gradleOut folder in our unity project. Can you please explain in detail how we can generate or where i am getting this folder.
     
  18. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,963
    Once you try to make the apk, it creates a folder under your project folder (in the level of Assets folder).
     
  19. BtMuntazir

    BtMuntazir

    Joined:
    Nov 25, 2021
    Posts:
    7
  20. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,963
    I see you are using Internal build system in Build Settings. You need to start using Gradle instead as internal got deprecated. Once you use that, you get the gradleOut folder under Temp.

    I checked the shared project and see there is no related content shared. As there was no content related to fileprovider or any other info shared earlier, I don't see any problem in building the apk either. It built successfully and ran well.

    Note: No need to share the complete project(it even included the debug symbols zip which is more than 400mb). Just Assets, ProjectSettings, Packages/manifest.json will work to clone a project.
     
  21. BtMuntazir

    BtMuntazir

    Joined:
    Nov 25, 2021
    Posts:
    7
    Hello @voxel,

    Basically we want the functionality of Auto install the app after download...so we have a download apk url... after implementi ng that url we download the apk file in the local storage of the phone and its working fine... but when i trying to auto install the app through code then first of all we need to get the file from the storage with using the File Providor. here i am getting the exception class not found exception.

    Thanks
     
  22. muntazirsmart

    muntazirsmart

    Joined:
    Jul 3, 2022
    Posts:
    1
    Hello @Voxel-Busters

    I want to open a pdf file from the local storage in any one of the pdf opener application. I used the Filwprovider in unity but when I run the application the app crash with the below exception.

    Unity java.lang.RuntimeException: Unable to get provider androidx.core.content.FileProvider: java.lang.ClassNotFoundException: Didn't find class "androidx.core.content.FileProvider"

    Please help me to solve this issue.


    Thanks
     
  23. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,963
    This actually means you didn't add the android core library dependency. Try adding this dependency(for ex: androidx.core:core:1.10.1) via EDMU or directly in main gradle template file.