How to change where propietary code is loading a lib from on Android?

Ok, so I am developing an Android app that requires a massive proprietary native lib. This greatly increases the deploy time from AS, which is very annoying. I'd like to dynamically load the library from the shared storage rather than including it in each download.

However, I can't just use System.load("/data/data/com.package.app/extraLib.so"); because this lib also comes with a proprietary Java wrapper, and inside that wrapper System.loadLibrary("Library"); is being called. Thus, if the lib isn't included in AS, the proprietary code will crash with an UnsatisfiedLinkError.

I tried to manually copy the lib into the /lib folder of my app's protected storage. However, for some reason the /lib folder is write-protected. (Yes I was copying from my app's Java code, not from a file explorer app, so root should not be required.)

Is there any way to change where the proprietary code loads the lib from without reverse engineering it?

Upvotes: 2

Views: 175

Answers (1)

Warning! You should only do this for development, never for a release app!

With this approach, your app will be vulnerable to a code injection attack.


Well, I found a way to do it. I replaced the real library in the libs folder of AS with a dummy one that only contains this code:

int main()
{
    return 0;
}

Then I compiled that into a .so file and renamed it to the name of the original library, and inserted it into the libs folder of my project.

Then, in the onCreate() method of my main activity, I did:

    File fileOnSharedStorage = new File("/mnt/sdcard/libTest.so");
    File fileInProtectedStorage = new File("/data/data/com.package.myapp/extra/libTest.so");
    File folder = new File("/data/data/com.package.myapp/extra/");

    if (!folder.exists())
    {
      folder.mkdir();
    }

    if(!fileInProtectedStorage.exists())
    {
      try
      {
        copyFile(fileOnSharedStorage, fileInProtectedStorage);
      } catch (IOException e)
      {
        e.printStackTrace();
      }
    }

    System.load("/data/data/com.package.myapp/extra/libTest.so");

And it works!

Upvotes: 1

Related Questions