Armen Avetisyan Armen Avetisyan - 3 months ago 14
C++ Question

Create native android apk via command line only (makefile)

Goal



Creating a native android apk:


  • via
    vim
    &
    Makefile
    only

  • no use of
    gradle, ant, maven, android.mk
    and all that other stuff



Problem




  • I already created a simple non-native android apk via command line that works well & plays on my Samsung S7.

  • When I try
    loadLibrary("hello")
    a shared object then the
    logcat
    tells me it cannot find it.

  • The
    .so
    file is definitely added via
    aapt add hellojni.apk ./lib/arm64/libhello.so

  • Here is the source code: https://github.com/skanti/Android-Project-Command-Line/tree/native



Error report



08-25 16:56:30.959 15637 15637 W ResourcesManager: getTopLevelResources: /data/app/com.example.hellojni-1/base.apk / 4.0 running in com.example.hellojni rsrc of package com.example.hellojni
08-25 16:56:30.969 15637 15637 W System : ClassLoader referenced unknown path: /data/app/com.example.hellojni-1/lib/arm64
08-25 16:56:30.969 15637 15637 W amo : Trying to load shared library!
08-25 16:56:30.969 15637 15637 E AndroidRuntime: FATAL EXCEPTION: main
08-25 16:56:30.969 15637 15637 E AndroidRuntime: Process: com.example.hellojni, PID: 15637
08-25 16:56:30.969 15637 15637 E AndroidRuntime: java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.hellojni-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.hellojni-1/lib/arm64, /vendor/lib64, /system/lib64]]] couldn't find "libhello.so"

Answer

The path you specify here seems like the culprit:

aapt add hellojni.apk ./lib/arm64/libhello.so

I tried adding a bogus file to an APK using the same type of path and then listing the APK contents:

./aapt.exe add test.apk ./lib/libbcc.dll
./aapt.exe list test.apk

And got this in the output:

./lib/libbcc.dll

I.e. I ended up with a directory named . in the root of the APK, inside of which a lib directory was created, inside of which the libbcc.dll file was placed (I confirmed this by opening the APK with 7zip).

If I remove the ./:

./aapt.exe add test.apk lib/libbcc.dll
./aapt.exe list test.apk

I get the correct results:

lib/libbcc.dll

(I tested this both via Windows cmd and MSys2, with the same results)

Comments