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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

"Pre-installed" APK, Unable to find native library: main

Discussion in 'Android' started by eezSZI, Apr 24, 2015.

  1. eezSZI

    eezSZI

    Joined:
    Nov 16, 2012
    Posts:
    121
    So we have a custom tablet and we have a Unity app that is bundled. So it is preloaded with the system rather than installed manually (when installed manually after the flash it works fine). I believe what is happening is that the LibMain.so library is trying to be copied to the normal data folder, but it doesn't yet exist and is instead being installed elsehwere, perhaps /system/lib. So when it launches the app crashes (logs below).

    This question appears to be the same thing (and where I got the insight from):
    http://answers.unity3d.com/questions/910265/libmainso-unable-to-find-native-library-main.html

    So, any ideas on how to fix this? Do I need to override the NativeActivity and manually load the library? Has anyone done that before?

    Ideally Unity would handle this by checking the "preload" (system/lib) folders too. Thanks for any an all input!

    Log is below (key lines in bold):
     
  2. eezSZI

    eezSZI

    Joined:
    Nov 16, 2012
    Posts:
    121
    Here's what I'm trying, haven't been able to get it to the engineers to test yet. Perhaps this will help someone else or spur someone to help me. I'm only guessing that the library location (when preinstalled) is "system/lib/". The below code is in the UnityPlayerNativeActivity.

    Code (CSharp):
    1.  // Try to load library in case we were preloaded and it didnt make it to the normal location
    2.     static
    3.     {
    4.         Log.w("My App", "Loading main lib...");
    5.  
    6.         try {
    7.             System.load("/data/data/com.company.myapp/lib/libmain.so"); // Default location
    8.         } catch (UnsatisfiedLinkError e) {
    9.             Log.w("My App", "Native code library failed to load from /data/data/com.company.myapp/lib/main.so.\n" + e);
    10.         }
    11.         Log.w("My App", "Trying /system/lib/libmain.so...");
    12.  
    13.         try {
    14.             System.load("/system/lib/libmain.so"); // Possible preload location
    15.         } catch (UnsatisfiedLinkError e) {
    16.             Log.w("My App", "Native code library failed to load from /system/lib/libmain.so.\n" + e);
    17.         }
    18.         Log.w("My App", "Trying System.loadLibrary(\"main\")");
    19.  
    20.         try {
    21.             System.loadLibrary("main"); // I believe just checks the default location
    22.         } catch (UnsatisfiedLinkError e) {
    23.             Log.w("My App", "Native code library failed to load from \"main\".\n" + e);
    24.         }
    25.         Log.w("My App", "Done trying");
    26.     }
     
  3. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    530
    I don't think that will work. It's not really a Unity problem it's the Google NativeActivity code that makes an explicit file exist check on the NativeActivity library. The one listed in the AndroidManifest.xml - libmain.so by default.

    My suggestion would be to skip using the NativeActivity all together. In Unity 5 we switched to a regular Java activity as the default.
     
  4. eezSZI

    eezSZI

    Joined:
    Nov 16, 2012
    Posts:
    121
    Thanks, bitter. I'm a novice in android. Is there any way to do this with 4.6? We are short on time and wont be buying Unity 5 licenses anytime soon.

    I see that there is a "deprecated" comment for "UnityPlayerActivity.java". Besides changing "MAIN" in the manifest, what do I need to do to use this on instead of NativeActivity?
     
  5. eezSZI

    eezSZI

    Joined:
    Nov 16, 2012
    Posts:
    121
    I think I got it switched back over to UnityPlayerActivity.java successfully, however input events are not getting forwarded. I'm guessing there is something I missed in the Manifest...

    Edit:
    I think I got it with this: <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />

    Edit 2: I got it working and we are passing it to our partners. I will update this post if it works when bundled/pre-installed.

    Thanks!
     
    Last edited: May 6, 2015
  6. devrockstar

    devrockstar

    Joined:
    Jun 24, 2015
    Posts:
    1
    Did you get this method to work in-ROM?
     
  7. eezSZI

    eezSZI

    Joined:
    Nov 16, 2012
    Posts:
    121
    They eventually got it to work but had to make some changes to the kernel. I'm still waiting for a full report.
     
  8. eezSZI

    eezSZI

    Joined:
    Nov 16, 2012
    Posts:
    121
    @bitter okay, so I'm back to this. I'm using a Java Activity instead but still crashing. I'm not sure where to go from here:

     
  9. eezSZI

    eezSZI

    Joined:
    Nov 16, 2012
    Posts:
    121
    So here's where I'm at. First the libraries must be copied to "system/lib". Hopefully this can be done at the time of flashing. To test I did this through adb (installed apk, copied libs from normal to system lib folder, moved apk from normal to system/app). Then in the UnityPlayerActivity.java I still need to manually call System.load for each of the 3 libraries. It seems like Unity should pick this up now that they are there, but it doesn't find them unless I do this:

    Code (JavaScript):
    1.  
    2. public class UnityPlayerActivity extends Activity
    3. {
    4.       protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
    5.  
    6.   static{
    7.        
    8.          try {
    9.             System.load("/system/lib/libmain.so");
    10.         } catch (UnsatisfiedLinkError e) {
    11.             Log.w("MyApp", "1Native code library failed to load from /system/lib/libmain.so.\n" + e);
    12.         }
    13.  
    14.         try {
    15.             System.load("/system/lib/libmono.so"); // Possible preload location
    16.         } catch (UnsatisfiedLinkError e) {
    17.             Log.w("MyApp", "2Native code library failed to load from /system/lib/libmono.so.\n" + e);
    18.         }
    19.  
    20.         try {
    21.             System.load("/system/lib/libunity.so");
    22.         } catch (UnsatisfiedLinkError e) {
    23.             Log.w("MyApp", "3Native code library failed to load from /system/lib/libunity.so.\n" + e);
    24.         }
    25.  
    26.         try {
    27.             System.loadLibrary("main");
    28.         } catch (UnsatisfiedLinkError e) {
    29.             Log.w("MyApp", "4Native code library failed to load from \"main\".\n" + e);
    30.      }
    31. ...
    32.