Termux-packages: How can `*.so` files be linked into an APK build?

Created on 14 Jul 2020  路  15Comments  路  Source: termux/termux-packages

Thank you @xeffyr for instructing how to build *.so files from Android.mk files on device in Termux at https://www.reddit.com/r/termux/comments/ezxr95/ :

cmake .
make

How can *.so files be linked into an APK build? The script build.sh can build APKs in Termux on device. Out of commands aapt, ecj and dx, which one is best to integrate *.so files into an APK build? Which commands and which options should be used?

Most helpful comment

ecj is a java compiler, it converts java source files to java bytecode .class files, that's it. It doesn't add native .so libraries to zip archives, use a zip compatible program or aapt.

The -classpath option for ecj is used to tell ecj the location of compiled bytecode libraries aka .jar libraries; it is completely unrelated to putting native libraries into zip/apk archives

All 15 comments

They should be not linked but added inside the archive.

Shared libraries should be put into following directory structure, according their target architecture:

lib/arm64-v8a/libname.so
lib/armeabi-v7a/libname.so
lib/x86/libname.so
lib/x86_64/libname.so

So if you have library libmylib.so for architecture AArch64, you need to do following to put it inside APK file:

mkdir -p lib/arm64-v8a
mv libmylib.so lib/arm64-v8a/
zip -r -u apkfile.apk lib

This should be done before the signing procedure.

Or you could use aapt for adding the native libraries as well :
aapt add lib/arch-version/lib1.so lib/arch-version/lib2.so... and so on.

@xeffyr @Harshiv-Patel this issue is slowly coming to life in file doso.bash. Thank you for your guidance. I'll try the aapt method as suggested. Using the zip method has made progress:

Screenshot_20200716-003405.png

The APK https://github.com/android/ndk-samples/tree/master/san-angeles builds from source on device and integrates the *.so file in vanilla Termux on device as well as a couple new APKs in ndk-samples. To use this method, change 1 to 0 in configuration file DOSO.

EDIT This issue has helped create buildAPKs.native which attempts to build Termux Native APKs on device in one tap. Thank you 馃幎

use aapt for adding the native libraries

Thank you for this pointer @Harshiv-Patel; This commit https://github.com/BuildAPKs/buildAPKs/commit/0cc6379992a6301ca27ce28d27f09bdcd218702a#diff-d6c5855a62cf32a4dadbc2831f0f295f implements your suggestion.

It appears that adding them with -classpath to ecj is also possible; Has anyone had success with adding .so files with ecj?

ecj is a java compiler, it converts java source files to java bytecode .class files, that's it. It doesn't add native .so libraries to zip archives, use a zip compatible program or aapt.

The -classpath option for ecj is used to tell ecj the location of compiled bytecode libraries aka .jar libraries; it is completely unrelated to putting native libraries into zip/apk archives

@Harshiv-Patel so this information is out of date?

How do you add a .so file to a project (in linux)

You have a couple of options:
1) Putting the .so directory in the classpath. You'll need to add the directory that contains the .so, rather than adding the .so itself, so the classpath looks like my\path\here\libme.so and you'd add my\path\here to the classpath.

https://www.eclipse.org/forums/index.php/t/94766/

I have tried a number of methods, but none seem to work with ecj as you say.

@SDRausty Information you have read is valid for normal JDK but not for Android OS. Shared object libraries should be put on special location as shown in https://github.com/termux/termux-packages/issues/5541#issuecomment-658385687. It is added to linker search path during application runtime.

valid for normal JDK but not for Android OS.

@xeffyr thank you for this important information.

be put in special location

This is done in lines 10 and 26 and in between in file doso.bash:

L10 : CPUABI="$(getprop ro.product.cpu.abi)"
L26 : find . -type f -name "*.so" -exec mv {} "$JDR/bin/lib/$CPUABI" \;

Then .so files are added into the build process in file build.one.bash as @Harshiv-Patel suggested earlier:
L169 : aapt add -v -f "$PKGNAME.apk" classes.dex $(find lib -type f -name "*.so")

Since .so files contain functions and other shared program logic used for loading common libraries into memory, I am attempting to load these into the compilation as soon as possible. File build.one.bash L137 apt package -f \ has the current entry point for the build as apt package. Can apt add be used before apt package to accomplish loading .so files into the compilation as soon as possible, and how?

Shared object files contain function used during runtime, not build time. It doesn't matter when they are added to APK.

Android SDK adds them during the final stage of creating APK file.

Well you could try adding the native libs first,

but I think aapt needs AndroidManifest.xml and the res folder atleast, to process and create initial temporary package, the assets/* lib/* and others are added afterwards(as java side compilation needs R.java), that is the convention.
Honestly, when you add native libs doesn't matter, as long as they are present in final APK, and they are added before zip-aligning and signing.

You can look into how Gradle or ANT(if that's available to you) run the build with verbosity enabled, for better understanding the process.

How can these find /system -name *.so | wc -l 887 .so files that are already in my smartphone be linked into a build on device in Termux? Where are the instructions and code for these .so files?

Honestly, when you add native libs doesn't matter, as long as they are present in final APK, and they are added before zip-aligning and signing.

@Harshiv-Patel thank you for the suggestions:

can look into how Gradle or ANT

These do work well in Termux PRoot, maybe one day in vanilla Termux too

build with verbosity enabled

This is how I found out ecj was not using the .so files provided. Any ideas on linking Android system .so files?

@SDRausty let's take this conversation (which is frankly going little off-topic) to somewhere else, Termux-discord server for one https://discord.gg/EMb8dz
Or gitter if you don't use Discord. Conversations here can send unnecessary mails to the mailing list.

Agree, it is starting to go into direction completely unrelated to Termux and will be locked.


Any ideas on linking Android system .so files?

Android system .so files are not part of NDK and should not be used by regular applications. Here is why:

  • They are ROM-specific and you can't be sure that required library is present on specific device.
  • You don't know their interfaces. They often use private API and neither Termux nor stock NDK have headers for these libraries. They also often closed-source, especially on devices not using AOSP ROMs.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

roalyr picture roalyr  路  3Comments

loveablefellow007 picture loveablefellow007  路  3Comments

thurask picture thurask  路  3Comments

adit picture adit  路  3Comments

jackbrycesmith picture jackbrycesmith  路  3Comments