Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-5672] Compile swift file to .so library for armeabi-v7a is not recognized by Android #48242

Closed
swift-ci opened this issue Aug 10, 2017 · 6 comments
Labels
Android Platform: Android bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Aug 10, 2017

Previous ID SR-5672
Radar None
Original Reporter amraboelela (JIRA User)
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Android
Assignee amraboelela (JIRA)
Priority Medium

md5: 0e0d0fa99c372c5d06aa0c2e3bc05454

Issue Description:

Compiled as:

swiftc \
    -tools-directory ${ANDROID_NDK_DIR}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/arm-linux-androideabi/bin \
    -target armv7-none-linux-androideabi \
    -sdk ${ANDROID_NDK_DIR}/platforms/android-21/arch-arm \
    -L ${ANDROID_NDK_DIR}/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a \
    -L ${ANDROID_NDK_DIR}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x \
    -emit-library -o libAddSwift.so \
    Add.swift

When tried to use libAddSwift.so into AddSwiftApp, got error from Android Studio as:

java.lang.UnsatisfiedLinkError: dlopen failed: library "distribution/AddSwift/lib/armeabi-v7a/libAddSwift.so" not found

at

java.lang.Runtime.loadLibrary0(Runtime.java:977) at java.lang.System.loadLibrary(System.java:1530)
@swift-ci
Copy link
Collaborator Author

Comment by Amr Aboelela (JIRA)

I am finally able to run an Android app using a swift library. The main fix is to change the icu lib names to use swift suffix. I'll generate a pull request later to show how to do that in the Android.md file.

Meanwhile, you can check:

https://github.com/amraboelela/swift/blob/android/docs/Android.md

Also you can check what I did in:

https://github.com/amraboelela/SwiftAndroid

@swift-ci
Copy link
Collaborator Author

Comment by Amr Aboelela (JIRA)

Created pull request #11546

@milseman jblatecky (JIRA User) CodaFi (JIRA User) qasim (JIRA User)

@swift-ci
Copy link
Collaborator Author

Comment by Amr Aboelela (JIRA)

I was still getting the java.lang.UnsatisfiedLinkError not found error, so I did the following to fix this issue:

in app/build.graddle

I replaced:

defaultConfig {
...
externalNativeBuild {
cmake {
arguments '-DANDROID_PLATFORM=android-14',
'-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=gnustl_static'
}
}
}

With:

defaultConfig {
...
externalNativeBuild {
cmake {
arguments '-DANDROID_PLATFORM=android-14',
'-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=gnustl_static', '-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON'
}
}
}

Now after running, I can see the detailed calls for the compiling commands, so I got the following from Griddle Console in Android Studio:

Build add-swift armeabi-v7a
[1/2] /Users/aaboelela/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang -target=armv7-none-linux-androideabi --gcc-toolchain=/Users/aaboelela/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/aaboelela/Library/Android/sdk/ndk-bundle/sysroot -Dadd_swift_EXPORTS -isystem /Users/aaboelela/Library/Android/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D_ANDROID_API_=14 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,-noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/add-swift.dir/add-swift.c.o -MF CMakeFiles/add-swift.dir/add-swift.c.o.d -o CMakeFiles/add-swift.dir/add-swift.c.o -c /Users/aaboelela/android/AmrSwiftAndroid/AddSwiftApp/app/src/main/cpp/add-swift.c
[2/2] : && /Users/aaboelela/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang -target=armv7-none-linux-androideabi --gcc-toolchain=/Users/aaboelela/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/aaboelela/Library/Android/sdk/ndk-bundle/sysroot -fPIC -isystem /Users/aaboelela/Library/Android/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D_ANDROID_API_=14 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -Wl,exclude-libs,libgcc.a --sysroot /Users/aaboelela/Library/Android/sdk/ndk-bundle/platforms/android-14/arch-arm -Wl,build-id -Wl,warn-shared-textrel -Wl,fatal-warnings -Wl,fix-cortex-a8 -Wl,-no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libadd-swift.so -o /Users/aaboelela/android/AmrSwiftAndroid/AddSwiftApp/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libadd-swift.so CMakeFiles/add-swift.dir/add-swift.c.o -landroid -llog /Users/aaboelela/android/AmrSwiftAndroid/AddSwiftApp/libs/armeabi-v7a/libAddSwift.so -lm && :

The Part which causes the problem is: /Users/aaboelela/android/AmrSwiftAndroid/AddSwiftApp/libs/armeabi-v7a/libAddSwift.so

So I created my own shell script app/build.sh and replaced that part with -lAddSwift -L/Users/aaboelela/android/AmrSwiftAndroid/AddSwiftApp/libs/armeabi-v7a

The full build.sh content is:

mkdir -p CMakeFiles/add-swift.dir

/Users/aaboelela/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang -target=armv7-none-linux-androideabi --gcc-toolchain=/Users/aaboelela/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/aaboelela/Library/Android/sdk/ndk-bundle/sysroot -Dadd_swift_EXPORTS -isystem /Users/aaboelela/Library/Android/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D_ANDROID_API_=14 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,-noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/add-swift.dir/add-swift.c.o -MF CMakeFiles/add-swift.dir/add-swift.c.o.d -o CMakeFiles/add-swift.dir/add-swift.c.o -c /Users/aaboelela/android/AmrSwiftAndroid/AddSwiftApp/app/src/main/cpp/add-swift.c

/Users/aaboelela/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang -target=armv7-none-linux-androideabi --gcc-toolchain=/Users/aaboelela/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/aaboelela/Library/Android/sdk/ndk-bundle/sysroot -fPIC -isystem /Users/aaboelela/Library/Android/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D_ANDROID_API_=14 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -Wl,exclude-libs,libgcc.a --sysroot /Users/aaboelela/Library/Android/sdk/ndk-bundle/platforms/android-14/arch-arm -Wl,build-id -Wl,warn-shared-textrel -Wl,fatal-warnings -Wl,fix-cortex-a8 -Wl,-no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libadd-swift.so -o /Users/aaboelela/android/AmrSwiftAndroid/AddSwiftApp/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libadd-swift.so CMakeFiles/add-swift.dir/add-swift.c.o -landroid -llog -lAddSwift -L/Users/aaboelela/android/AmrSwiftAndroid/AddSwiftApp/libs/armeabi-v7a -lm

Then I ran build.sh from terminal as:

app$ ./build.sh

Then I ran the project from Android Studio again then I passed that error.

@swift-ci
Copy link
Collaborator Author

Comment by Amr Aboelela (JIRA)

Now I am getting this error:

08-23 11:58:59.438 25922-25922/com.example.addswift A/SwiftRuntime: dlopen() failed on `/system/lib/libcutils.so': dlopen failed: library "/system/lib/libcutils.so" wasn't loaded and RTLD_NOLOAD prevented it

                  • beginning of crash
                    08-23 11:58:59.448 25922-25922/com.example.addswift A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 25922 (xample.addswift)

[ 08-23 11:58:59.451 645: 645 W/ ]
debuggerd: handling request: pid=25922 uid=10061 gid=10061 tid=25922
08-23 11:58:59.671 25957-25957/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
08-23 11:58:59.672 25957-25957/? A/DEBUG: Build fingerprint: 'Android/sdk_phone_armv7/generic:7.0/NYC/3245079:userdebug/test-keys'
08-23 11:58:59.672 25957-25957/? A/DEBUG: Revision: '0'
08-23 11:58:59.672 25957-25957/? A/DEBUG: ABI: 'arm'
08-23 11:58:59.673 25957-25957/? A/DEBUG: pid: 25922, tid: 25922, name: xample.addswift >>> com.example.addswift <<<
08-23 11:58:59.673 25957-25957/? A/DEBUG: signal 6 (SIGABRT), code 6 (SI_TKILL), fault addr -------
08-23 11:58:59.693 25957-25957/? A/DEBUG: Abort message: 'dlopen() failed on `/system/lib/libcutils.so': dlopen failed: library "/system/lib/libcutils.so" wasn't loaded and RTLD_NOLOAD prevented it'
08-23 11:58:59.694 25957-25957/? A/DEBUG: r0 00000000 r1 00006542 r2 00000006 r3 00000008
08-23 11:58:59.694 25957-25957/? A/DEBUG: r4 ad71c584 r5 00000006 r6 ad71c52c r7 0000010c
08-23 11:58:59.695 25957-25957/? A/DEBUG: r8 92565cb0 r9 bef4f2e8 sl 925f31ac fp bef4f218
08-23 11:58:59.695 25957-25957/? A/DEBUG: ip 00000058 sp bef4f1c0 lr ace2932f pc ace2bb7c cpsr 20000010
08-23 11:58:59.730 25957-25957/? A/DEBUG: backtrace:
08-23 11:58:59.730 25957-25957/? A/DEBUG: #00 pc 00048b7c /system/lib/libc.so (tgkill+12)
08-23 11:58:59.731 25957-25957/? A/DEBUG: #1 pc 0004632b /system/lib/libc.so (pthread_kill+34)
08-23 11:58:59.731 25957-25957/? A/DEBUG: #2 pc 0001c6d5 /system/lib/libc.so (raise+10)
08-23 11:58:59.731 25957-25957/? A/DEBUG: #3 pc 00018237 /system/lib/libc.so (__libc_android_abort+34)
08-23 11:58:59.732 25957-25957/? A/DEBUG: #4 pc 00017770 /system/lib/libc.so (abort+4)
08-23 11:58:59.732 25957-25957/? A/DEBUG: #5 pc 004de804 /data/app/com.example.addswift-2/lib/arm/libswiftCore.so

@swift-ci
Copy link
Collaborator Author

Comment by Amr Aboelela (JIRA)

Created a new bug for this https://bugs.swift.org/browse/SR-5757

@milseman
Copy link
Mannequin

milseman mannequin commented Aug 24, 2017

I'm afraid that is well beyond my understanding. Any idea why that is trying to be opened? Also does that .so exist at that path? RTLD_NOLOAD should attempt to determine if it's there, but I believe it won't try to actually do the load.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Android Platform: Android bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

1 participant