Copy folder structure containing binaries to native libraries folder - android

I want to copy and execute some precompiled binaries on device, the problem is that the relative folder structure needs to be the same as in the original package (I cannot move all binaries to root folder).
What so far I found about this is how to copy binaries without .so extension to lib folder here. It works as expected, once installed I can locate the binary using context.getApplicationInfo().nativeLibraryDir, however when I place the directory with binaries it cannot find it using the command.
Further investigation shows that folder does get coppied to apk. I can confirm that by unziping the apk and finding the binaries coppied at /lib/arm64-v8a/.
Now since the apk is fine, the next step I did is check what actually gets copied to device. This can be found in /data/app/app.name.com+random_hash/lib/ and I confirmed that folder does not get copied over, only binaries from root folder do.
What I want to know is whether the code responsible for copying native libraries is located on device and cannot be changed, or the application can be somehow configured to copy the folder structure?
Example repo: https://github.com/D4no0/copy_native_binaries

The code is on device, part of the OS :
"Subdirectory is not supported by the Android OS.
When the APK is installed, the .so libraries are extracted to a directory in the form of /data/app/your.app/lib/. That directory is added to the library path searched by System.loadLibrary so that it can be found. System.loadLibrary do not support a directory structure, thus, the .so files in a subdirectory under lib/ are not extracted."
The above answer is coming from a Googler working on Android, from the following issue :
https://issuetracker.google.com/issues/63707864#comment4
Cheers,
Jérôme

Related

Include files with different prefixes in apk

I have a project with native libraries that I want to use, files with this format: lib<name>.so do get included into apk. But files with <name>.so format does not.
Is there a way to include the later type into apk in lib directory?
If not, is there a way to include the files into a directory inside apk, where I can load it from my native code?
The short answer is "no". The native binaries will only be packed into APK, and extracted to executable files upon installation, if their names follow the lib….so pattern.
Note that these libraries will be extracted to files according to the ABI of the target system. The installer does not check the actual properties of the file. The decision is based on the name of the folder under lib in the APK structure.
If you add the attribute extractNativeLibs=false to the application tag in AndroidManifest.xml of your APK, the installer (on Android Nougat and higher) will not extract the native libraries.
You can trick the system and have files that don't follow the above rule to the lib folder of APK, but there is very little sense in it, because they will never be extracted by the loader (it may also extract file gdbserver if the file is there).
The common practice is to put the arbitrary files in the assets folder of your APK, and extract them programmatically when the app runs for the first time after install. You cannot extract these files to the secured location where the usual native libraries go. You should not extract the native libraries to sdcard (e.g. getExternalFilesDir()), because the system may not allow execution of the files there, regardless of the execute access flag on the file. Make sure that you use the correct ABI flavour.
You can peek at the source code of Nougat native lib loader that can load native libraries from the APK without extraction, and use it to load your custom libraries directly from the assets folder of your APK.

Adding external cpp files to Android Studio JNI

I have an Android project setup in Android Studio.
I'm using NDK and JNI so I have some cpp/h files in the jni folder. I can use them and everything is fine.
I also have another folder somewhere in my harddrive (which is not under the Android project root folder) with more cpp files.
So in Android.mk I added the path to both local (jni folder) files and the external files directory path. Everything compiles and everything works fine.
The issue I'm having is how to actually link to those cpp files in the external folder so I can actually edit them inside Android Studio or at least see the functions and member names. Currently I edit those files in notepad++ save and then go back to Android Studio to call the methods etc.
The external folder is simply a folder with some cpp files inside. It's not a module, it does not have a build.gradle or anything like that.
Is what I'm asking even possbile?
I hope the question was clear enough. Any help is appreciated!
Thanks

How to add a folder to a android apk use appt and ant

When I use ant tool for build an android project, I do not know how to add custom folders to the apk file, such as a a folder named "running" under the project root, when I use the eclipse can be directly add to apk. I hope you can give me some help, thanks!
During the build process, your Android projects are compiled and packaged into an .apk file, the container for your application binary. It contains all of the information necessary to run your application on a device or emulator, such as compiled .dex files (.class files converted to Dalvik byte code), a binary version of the AndroidManifest.xml file, compiled resources (resources.arsc) and uncompiled resource files for your application.
I can't figure out why you need to add the folder to apk, well if you are asking about adding it in the project and later to be accessed in the apk then yes you can place it inside the "assets" folder of your project

No "libs" directory found after reversing the android app using apktool?

I reversed an app (my own dummy app) using apktool.
>apktool d testing.apk
The last line of output in the terminal says "I: Copying assets and libs..." but there is no "libs" directory in the generated folder.
Am I missing something here ?
I want to know which 3rd party libraries are being used. This can be found from "libs" directory but I am not getting this directory. Any help is highly appreciated. Thank you.
You don't need apktool to get the libs. An APK is just a ZIP. Just unzip testing.apk and look for a lib/ directory.
If it's not there, it may not use shared libraries. Or, they may be downloaded at runtime. To check, install and run the app such that you're sure it's used functions from the shared library. Then, pull the files off the device with:
adb pull /data/data/<package name>/lib

"Native method not found" in android studio

i have looked through all the other posts here about this subject but none of them helped me.
i am using a third party SDK (oovoo) and it contains a .jar file and .so file (armeabi-v7a)
as stated in allot of places, i put the .jar file in the libs folder and the armeabi-v7a folder (which contains the .so file) in a folder named jniLibs inside src/main
after building, when i unzip the .apk i see a folder named lib and inside it a folder named armeabi-v7a which contains the .so file
yet i still get this exception:
java.lang.UnsatisfiedLinkError: Native method not found: com.oovoo.core.ClientCore.VideoChannelPtr.nCreate:(Ljava/lang/String;)J
why cant it reach the implementation?
if it is in the lib folder in
the .apk does it mean it will for it at runtime?
thanks allot!
android studio version 0.8.6
gradle version 1.12
oovoo version 1.2.4
To answer your questions specifically :
It can't reach the implementation because at runtime, when the java environment tries to load a native method named java_com_oovoo_core_ClientCore_VideoChannelPtr_nCreate with the prototype int (string) it doesn't find such a method loaded. This can be caused by the following :
the .so library isn't loaded by the time you called that method , hence java doesn't know about it (so it's your mistake, you called the functions in wrong order)
the .so library doesn't export such a function, which means you have no chance of fixing this
the .so library loaded at runtime by the phone requires a different architecture besides armv7s. Be sure your device is armv7s.
the folder in which the Android system is looking for native libs is different than you expect. I know this was an old issue, so create in your apk, in your lib folder both "armeabi-v7a" and "armeabi" , and copy that .so file in both
Regarding your 2nd question, yes, if you have the lib inside the apk you will have it at runtime.

Categories

Resources