bitcoin-core / secp256k1

Optimized C library for EC operations on curve secp256k1
MIT License
2.07k stars 1k forks source link

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

Closed gecng closed 5 years ago

gecng commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

configure has a --host option for cross compilation.

real-or-random commented 5 years ago

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 commented 5 years ago

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 commented 4 years ago

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

real-or-random commented 4 years ago

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 commented 4 years ago

Thanks for reply!

Maybe we should some instructions for Android.

That would be great!

kroggen commented 3 years ago

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 commented 3 years ago

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

kroggen commented 3 years ago

Yep, feel free

hebasto commented 2 years ago

https://github.com/bitcoin-core/secp256k1/pull/1113#issuecomment-1169175578 could be related.