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

Is there an option to create a shared library for Android applications? #621

Closed
gecng opened this issue May 17, 2019 · 13 comments
Closed

Is there an option to create a shared library for Android applications? #621

gecng opened this issue May 17, 2019 · 13 comments

Comments

@gecng
Copy link

gecng commented May 17, 2019

I use ./configure --enable-jni --enable-experimental --enable-module-schnorr --enable-module-ecdh and get one secp256k1.so files.but there are many different CPU architectures in android like armeabi ,rmeabi-v7a,arm64-v8a ,x86 . Could you please tell me how should I do to create .so files for different CPU architectures. Thanks

@gecng
Copy link
Author

gecng commented May 17, 2019

when I use System.loadLibrary("javasecp256k1") , it throws exception
java.lang.UnsatisfiedLinkError: dlopen failed: "/data/app/com.wallet.locker-Gpfu8hdv5zMz363H3eJC8Q==/lib/arm/libjavasecp256k1.so" is 64-bit instead of 32-bit.

@sipa
Copy link
Contributor

sipa commented May 18, 2019

I don't much about Android, but generally I would expect you need a separate build/.so per architecture, and then ship the right one for the used architecture.

@gecng
Copy link
Author

gecng commented May 19, 2019

I don't much about Android, but generally I would expect you need a separate build/.so per architecture, and then ship the right one for the used architecture.

yes,I really need a separate build/.so per architecture, but I don't know the way to build it

@real-or-random
Copy link
Contributor

configure has a --host option for cross compilation.

@real-or-random
Copy link
Contributor

For reference, I can build this for example by installing Android NDK 19 and then using

export PATH="$PATH:/opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin"
./configure --enable-ecmult-static-precomputation --enable-experimental --with-bignum=no --with-asm=arm --host=arm-linux-androideabi CC=armv7a-linux-androideabi21-clang CFLAGS="-mthumb -march=armv7-a" CCASFLAGS="-Wa,-mthumb -Wa,-march=armv7-a"

See https://developer.android.com/ndk/guides/standalone_toolchain for more information, e.g., the right flags for other targets.

@gecng
Copy link
Author

gecng commented May 27, 2019

For reference, I can build this for example by installing Android NDK 19 and then using

export PATH="$PATH:/opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin"
./configure --enable-ecmult-static-precomputation --enable-experimental --with-bignum=no --with-asm=arm --host=arm-linux-androideabi CC=armv7a-linux-androideabi21-clang CFLAGS="-mthumb -march=armv7-a" CCASFLAGS="-Wa,-mthumb -Wa,-march=armv7-a"

See https://developer.android.com/ndk/guides/standalone_toolchain for more information, e.g., the right flags for other targets.

thank you very much , i use this way and has solved my problem.

@strongpower
Copy link

Hi @real-or-random, how to build library for all Android arch? One library file for all Android platform?

@real-or-random
Copy link
Contributor

Hi, I don't think that's possible. But it's possible to build different libaries for the different platforms:
https://developer.android.com/ndk/guides/other_build_systems#autoconf
And then I'm sure there's also a way to ship all of these builds in different builds of your app but I have no idea how this works.

Maybe we should some instructions for Android.

@strongpower
Copy link

Thanks for reply!

Maybe we should some instructions for Android.

That would be great!

@kroggen
Copy link

kroggen commented Nov 20, 2020

I use this script to build for Android:

#!/bin/bash

set -e

if [ -z "$ANDROID_NDK" ]; then
   echo "export the ANDROID_NDK environment variable"
   exit 1
fi

# Only choose one of these, depending on your build machine...
export TOOLCHAIN=$ANDROID_NDK/toolchains/llvm/prebuilt/darwin-x86_64
#export TOOLCHAIN=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64

# create links to some toolchains binaries (https://github.com/android/ndk/issues/1324)
cd $TOOLCHAIN/bin/
for source in arm-linux-androideabi-*
do
    dest=${source/arm/armv7a}
    ln -sf $source $dest
done
cd -

# Set this to your minSdkVersion.
export API=21

pwd=`pwd`

buildit()
{
    abi=$1
    target=$2

    echo ""
    echo "-------------------------------------------------------------------------------"
    echo " Compiling for $abi"
    echo "-------------------------------------------------------------------------------"

    export TARGET=$target

    # Configure and build.
    export AR=$TOOLCHAIN/bin/$TARGET-ar
    export AS=$TOOLCHAIN/bin/$TARGET-as
    export CC=$TOOLCHAIN/bin/$TARGET$API-clang
    export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
    export LD=$TOOLCHAIN/bin/$TARGET-ld
    export RANLIB=$TOOLCHAIN/bin/$TARGET-ranlib
    export STRIP=$TOOLCHAIN/bin/$TARGET-strip

    ./configure --prefix="$pwd/android/$abi" --host=$TARGET --disable-benchmark

    make clean
    make
    make install
}

buildit armeabi-v7a armv7a-linux-androideabi
buildit arm64-v8a   aarch64-linux-android
buildit x86         i686-linux-android
buildit x86-64      x86_64-linux-android

make clean

echo "done. the files are in the android folder"

Save it in the repo's root and run it:

./make-android

@real-or-random
Copy link
Contributor

real-or-random commented Nov 23, 2020

@kroggen Thanks for sharing. That's really helpful. Can we add this to our repo once we solve #622?

@kroggen
Copy link

kroggen commented Nov 23, 2020

Yep, feel free

real-or-random added a commit that referenced this issue Jan 12, 2021
3c15130 Improve CC_FOR_BUILD detection (Tim Ruffing)
47802a4 Restructure and tidy configure.ac (Tim Ruffing)
252c19d Ask brew for valgrind include path (Tim Ruffing)

Pull request description:

  See individual commit messages. These are improvements in preparation of the switch to Cirrus CI. (Maybe I'll just open a PR on top of this one.)

  The first commit made the difference between successful build https://cirrus-ci.com/task/6740575057608704 and unsuccessful build https://cirrus-ci.com/task/4909571074424832.

  I've tested the second commit without cross-compilation and with cross-compilation for android (#621 (comment))

  When working on the autoconf stuff, I noticed two things that I just want to write down here:
   - At some point we should update [build-aux/m4/ax_prog_cc_for_build.m4](https://www.gnu.org/software/autoconf-archive/ax_prog_cc_for_build.html). This is outdated, and [there have been a lot of fixes](autoconf-archive/autoconf-archive#207) But the latest version is [broken](https://lists.gnu.org/archive/html/autoconf-archive-maintainers/2020-06/msg00002.html), so now is probably not the time.
   - The latest autoconf 2.70 deprecates `AC_PROG_CC_C89`. It's not needed anymore because `AC_PROG_CC` cares about testing for version support. This makes autoconf 2.70 output a warning that we should probably just ignore. We don't want to force users onto 2.70...

ACKs for top commit:
  sipa:
    utACK 3c15130
  jonasnick:
    utACK 3c15130 makes sense (with my very basic understanding of autoconf)

Tree-SHA512: 595b9de316374c2213f1340cddaa22eb3190b01fa99aa6ae26e77804df41e7ecf96a09e03c28e8f8b9fd04e211e4ee2f78f1e5a7995143c84f99d2e16d4f0260
@hebasto
Copy link
Member

hebasto commented Jun 28, 2022

#1113 (comment) could be related.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants
@sipa @real-or-random @kroggen @gecng @hebasto @strongpower and others