Updating the original library without reinstalling the app

I have an application that includes two native libraries (eg libfirst.so, libsecond.so). Now the standard method for loading libraries.

System.loadLibrary("first");
System.loadLibrary("second");

      

Now I need to be able to update the library without reinstalling the app (without updating the .apk). I can load my own libraries of this method

System.load("/storage/emulated/0/armeabi/libfirst.so");
System.load("/storage/emulated/0/armeabi/libsecond.so");

      

I think this is bad practice. I tried to copy the library guide into the getApplicationInfo () folder. NativeLibraryDir but system error: open failed: EACCES (permission denied)

What are the best practices for updating native libraries without updating apk?

+3


source to share


2 answers


What are the best practices for updating native libraries without updating apk?

I do not believe contingencies are covered.

Code signing does two things. First, it ensures that the developer who publishes the app is the same developer who updates the app. Second, it creates a trust relationship between connected applications (but that doesn't apply here). Nikolai Elenkov discusses this topic in Code Signing in the Android Security Model .

If you want to update dependent libraries, you need to rebuild the APK to prove that you are allowed to make updates. This part of the first code signing was discussed above.



If you want to update your personal copy of the app (rather than publish it), simply strip the existing signatures, update the libraries, sign the Debug sign key , and then click on the device.

Alternatively, you can follow through adb install -r <the apk>

. -r

means "replace existing application" and effectively reinstall it. It retains previous application data, such as a database.

When updating an app, there are some other things to do, such as increasing version numbers, so that they will be handled correctly in Google Play. But they are not relevant to your question.

+1


source


There is actually a way to do this

to install apk:

adb install -r path_to_my_apk.apk

      

now you can change your code and recompile your .so and when you want to update it you can:

adb push -p path_to_my_so.so /data/local/tmp/libmyso.so
adb shell cat /data/local/tmp/libmyso.so | run-as com.mycomp.myapp sh -c 'cat > /data/data/com.mycomp.myapp/lldb/libmyso.so; chmod 700 /data/data/com.mycomp.myapp/lldb/libmyso.so'
adb shell rm /data/local/tmp/libmyso.so

      



Android studio does exactly this to install the lldb server when you are debugging native code.
This assumes there is an lldb folder in the application directory. if you are debugging using Android Studio this folder will already be there.

Your Java code should make sure to load the correct .so using

System.load("/data/data/com.mycomp.myapp/lldb/libmyso.so");

      

Instead System.loadLibrary()

, that doesn't take a full path in a static initialization block somewhere, similar to how you load any other .so

This method can be very helpful if your apk is huge and you just want to update the binary for easy debugging.

0


source







All Articles