Skip to content

如何使用Android NDK交叉编译redsocks2

semigodking edited this page Feb 7, 2021 · 3 revisions

1.首先创建ndk的独立工具链,选择api时要大于等于21,例如:

wget https://dl.google.com/android/repository/android-ndk-r20-linux-x86_64.zip
unzip android-ndk-r20-linux-x86_64.zip
rm -rf android-ndk-r20-linux-x86_64.zip /usr/local/android /usr/local/android-aarch64 /usr/local/android-arm
mv android-ndk-r20 /usr/local/android
/usr/local/android/build/tools/make-standalone-toolchain.sh --arch=arm64 --platform=android-21 --install-dir=/usr/local/android-aarch64
/usr/local/android/build/tools/make-standalone-toolchain.sh --arch=arm --platform=android-21 --install-dir=/usr/local/android-arm

使用上面的命令可以将arm的独立工具链安装到/usr/local/android-arm,arm64的独立工具链安装到/usr/local/android-aarch64,api为21

2.添加编译工具到环境变量 export PATH=$PATH:/usr/local/android:/usr/local/android-arm/bin:/usr/local/android-aarch64/bin

3.编译openssl和libevent2

#android openssl
    wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1c.tar.gz
    tar xf OpenSSL_1_1_1c.tar.gz
    rm OpenSSL_1_1_1c.tar.gz
    cd openssl-OpenSSL_1_1_1c
    export ANDROID_NDK_HOME=/usr/local/android ANDROID_NDK=/usr/local/android
    cp ./Configurations/15-android.conf ./Configurations/15-android.conf.bak
    sed -i "115,117d;123,125d" ./Configurations/15-android.conf
    #for arm64
    ./Configure android-arm64 no-shared --prefix=/root/android-arm64 -D__ANDROID_API__=21
    sed -i "s|-mandroid||g" Makefile
    make build_libs
    make install_dev
    make clean
    #for arm
    ./Configure android-arm no-shared --prefix=/root/android-arm -D__ANDROID_API__=21
    sed -i "s|-mandroid||g" Makefile
    make build_libs
    make install_dev
    cd ..
    rm -rf openssl-OpenSSL_1_1_1c

#android libevent2
    wget https://github.com/libevent/libevent/releases/download/release-2.1.10-stable/libevent-2.1.10-stable.tar.gz
    tar xf libevent-2.1.10-stable.tar.gz
    rm libevent-2.1.10-stable.tar.gz
    cd libevent-2.1.10-stable
    #for arm
    ./configure --enable-shared=no --disable-openssl --disable-debug-mode --prefix=/root/android-arm --host=arm-linux-androideabi CC=arm-linux-androideabi-gcc CXX=arm-linux-androideabi-g++
    make
    make install
    make clean
    #for arm64
    ./configure --enable-shared=no --disable-openssl --disable-debug-mode --prefix=/root/android-arm64 --host=aarch64-linux-android CC=aarch64-linux-android-gcc CXX=aarch64-linux-android-g++
    make
    make install
    cd ..
    rm -rf libevent-2.1.10-stable

4.编译redsocks2

#android redsocks2
    git clone https://github.com/semigodking/redsocks.git
    cd redsocks
    #for arm
    CC=arm-linux-androideabi-gcc CFLAGS=-I/root/android-arm/include LDFLAGS=-L/root/android-arm/lib make ENABLE_STATIC=true DISABLE_SHADOWSOCKS=true
    arm-linux-androideabi-gcc -I/root/android-arm/include -fPIC -O3 -DDISABLE_SHADOWSOCKS -D_BSD_SOURCE -D_DEFAULT_SOURCE -Wall -std=c99 -D_XOPEN_SOURCE=600 -DUSE_CRYPTO_OPENSSL -o redsocks2 parser.o main.o redsocks.o log.o socks5.o base.o base64.o md5.o utils.o redudp.o socks5-udp.o autoproxy.o direct.o http-relay.o http-connect.o ipcache.o tcpdns.o socks4.o http-auth.o gen/version.o -L/root/android-arm/lib -Wl,-Bstatic -levent -lssl -lcrypto -Wl,-Bdynamic -ldl -lz
    cp redsocks2 ../redsocks2_arm
    make clean
    #for arm64
    CC=aarch64-linux-android-gcc CFLAGS=-I/root/android-arm64/include LDFLAGS=-L/root/android-arm64/lib make ENABLE_STATIC=true DISABLE_SHADOWSOCKS=true
    aarch64-linux-android-gcc -I/root/android-arm64/include -fPIC -O3 -DDISABLE_SHADOWSOCKS -D_BSD_SOURCE -D_DEFAULT_SOURCE -Wall -std=c99 -D_XOPEN_SOURCE=600 -DUSE_CRYPTO_OPENSSL -o redsocks2 parser.o main.o redsocks.o log.o socks5.o base.o base64.o md5.o utils.o redudp.o socks5-udp.o autoproxy.o direct.o http-relay.o http-connect.o ipcache.o tcpdns.o socks4.o http-auth.o gen/version.o -L/root/android-arm64/lib -Wl,-Bstatic -levent -lssl -lcrypto -Wl,-Bdynamic -ldl -lz
    cp redsocks2 ../redsocks2_arm64
    cd ..
    rm -rf redsocks
    aarch64-linux-android-strip redsocks2_arm*

In case there's error reporting something like "undefined reference to `autoproxy_subsys'", try to add the missing .o files into gcc command line above.