相关文章推荐

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

    Flarup:

    java.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.ClassNotFoundException: Didn’t find class “android.support.v4.content.FileProvider”

    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

    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.

    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.

    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

    ok, no worries. I was a bit lazy to dig through :stuck_out_tongue:

    In this post, grab the second script and keep in Editor folder. Also, you need to have the following checked.

  • Have at-least 1.2.119 Play Services Resolver. You can download the latest one from here .
  • In Play Services Resolver, under Android Settings (from Assets → Play Services Resolver → Android → Settings), enable “Uses Jetifier” if not enabled.
  • 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

    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!

    Voxel-Busters:

    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

    Voxel-Busters:

    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

    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”.

    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 ( )

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

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

    // GameObject.Find(“TextDebug”).GetComponent().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 = “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 = “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 = “Error Saving Data”;

    //Install APK
    installApp(savePath); yield return true; } private bool installApp(string apkPath) { bool success = true;
    // GameObject.Find(“TextDebug”).GetComponent().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(“currentActivity”); AndroidJavaObject unityContext = currentActivity.Call(“getApplicationContext”);

    //com.unitysharing
    //Get the package Name
    string packageName = unityContext.Call(“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(“ACTION_VIEW”); AndroidJavaObject intent = new AndroidJavaObject(“android.content.Intent”, ACTION_VIEW); int FLAG_ACTIVITY_NEW_TASK = intentObj.GetStatic(“FLAG_ACTIVITY_NEW_TASK”); int FLAG_GRANT_READ_URI_PERMISSION = intentObj.GetStatic(“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(“getUriForFile”, unityContext, authority, fileObj);

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

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

    // GameObject.Find(“TextDebug”).GetComponent().text = “Success”;
    catch (System.Exception e) {

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

    // GameObject.Find(“TextDebug”).GetComponent().text = "Error: " + e.Message;
    success = false;
    BtMuntazir:

    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”.

    Did you add androidx core libs as dependency in your project?

    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!

    Hello @Voxel-Busters ,

    I have uploaded a dropbox link of my project. In that project i implemented the auto install the app directlly in unity. Please check my code because i am getting class not found error.

    https://www.dropbox.com/s/suwz9oukugq6lh3/New%20Unity%20Project.zip?dl=0

    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.

     
    推荐文章