User Tools

Site Tools


android:android-jni:ndkbuild-shell-for-linux

NDK Shell for Linux/Cygwin(Windows)

自己简单封装了一下ndk-build

支持:

  1. 通过修改build-static-library.mk,可以生成静态库
  2. 直接解析Android.mk,支持设置安装目录,支持自动拷贝依赖的静态库
  3. 添加了系统检测,同时支持Linux和Cygwin(Windows)环境。

使用方法

  1. 和以前一样,只是直接调用脚本名字ndk。
  2. 支持原来ndk-build参数,如:ndk clean,ndk -h等。

update note:

  1. 解决了Windows下make.exe和Symbian编译环境的冲突
  2. 同时支持了Linux和Cygwin(Windows)环境下编译

#!/bin/sh

#Your Android.mk Maybe need to add INSTALL_DIR and MY_STATIC_LIBRARIES.
#Now it just support parse these variables:
#   INSTALL_DIR =>Where you want to install the static library.
#   MY_STATIC_LIBRARIES =>Some dependent static libraries,with the full path or relative path.
#NOTICE:
#1.This shell just support parse MY_STATIC_LIBRARIES like this:
#	MY_STATIC_LIBRARIES = ../libafc.a
#	MY_STATIC_LIBRARIES = ../libutf8.a
#2.If you need to run this shell,you must cd to the RIGHT directory
#You MUST see ./${ANDROID_MK},then this shell will run,otherwise it will exit.

#Author:pengjianqing@gmail.com
#Date:20101126

START_AT=`date`

INSTALL_DIR="INSTALL_DIR"
MY_STATIC_LIBRARIES="MY_STATIC_LIBRARIES" 

ANDROID_MK="jni/Android.mk"

CREATED_LIBRARIES_DIR="libs/armeabi"


#help()
#{
#    cat <<HELP
#    ndk-build -h
#
#HELP
#    exit 0
#}

printTime(){
    echo "Start at ${START_AT}"
    echo "Finish at `date`"
}

prepareStaticLibraries(){
    echo "##################################################"
    echo "Prepare the static libraries"
    #LOCAL_STATIC_LIBRARIES=`grep LOCAL_STATIC_LIBRARIES ${ANDROID_MK}|grep -v ^#|cut -d "=" -f2|tail -n 1`
    #echo LOCAL_STATIC_LIBRARIES=$LOCAL_STATIC_LIBRARIES

    HOST_OS=`uname -s`
    BIN_ARMEABI="obj/local/armeabi/"

    echo "HOST_OS=${HOST_OS}"
    #Check the OS,I find Linux and Cygwin has the different directory.
    if [ "${HOST_OS}" = "Linux" ];then
	    BIN_ARMEABI="obj/local/armeabi/"
	    #elif [ "${HOST_OS}" = "CYGWIN*|*_NT-*" ];then
    else
	    BIN_ARMEABI="bin/ndk/local/armeabi/"
    fi	

    mkdir -p ${BIN_ARMEABI}
    STATIC_LIBRARIES=`grep ${MY_STATIC_LIBRARIES} ${ANDROID_MK}|grep -v ^#|cut -d "=" -f2|sed 's/ //g'`
    for i in ${STATIC_LIBRARIES}
do 
    #echo STATIC_LIBRARIES=$i
    cd jni
    echo "cp ${i}  ../${BIN_ARMEABI}"
    cp ${i}  ../${BIN_ARMEABI}
    cd ..
done
echo "##################################################"
}

installLibraries(){
    echo "##################################################"
    echo "Install the Built Libraries."
    FILE_A=`grep LOCAL_MODULE ${ANDROID_MK} |grep -v ^#|cut -d "=" -f2|tail -n 1|sed 's/ //g'|sed 's/^/lib/g'|sed 's/$/.a/g'` 
    FILE_SO=`grep LOCAL_MODULE ${ANDROID_MK} |grep -v ^#|cut -d "=" -f2|tail -n 1|sed 's/ //g'|sed 's/^/lib/g'|sed 's/$/.so/g'` 

    #echo FILE=${FILE}

    DIR=`grep ${INSTALL_DIR} ${ANDROID_MK}|grep -v ^#|cut -d "=" -f2|tail -n 1|sed 's/ //g'`
    #echo DIR=${DIR}

    if ! [ -n ${DIR} ] || [ "${DIR}" = "" ] ;then
	#echo "return"
	echo "Do not set INSTALL_DIR in the Android.mk,so just exit."
	echo "##################################################"
	return
    fi

    if [ -f "${CREATED_LIBRARIES_DIR}/${FILE_A}" ];then
	echo "${FILE_A} exist,copy it to ${DIR}"
	echo "cd jni"
	cd jni
	echo "cp ../${CREATED_LIBRARIES_DIR}/${FILE_A} ${DIR}"
	cp ../${CREATED_LIBRARIES_DIR}/${FILE_A} ${DIR}
	cd ..
    elif [ -f "${CREATED_LIBRARIES_DIR}/${FILE_SO}" ];then
	echo "${FILE_SO} exist,copy it to ${DIR}"
	echo "cd jni"
	cd jni
	echo "cp ../${CREATED_LIBRARIES_DIR}/${FILE_SO} ${DIR}"
	cp ../${CREATED_LIBRARIES_DIR}/${FILE_SO} ${DIR}
	cd ..
    else
	echo "${FILE_A} or ${FILE_SO} doesn't exist,MAYBE BUILD FAILED!"
    fi
    echo "##################################################"
}

clean(){
    if [ "$1" = "clean" ];then
	echo "ndk-build clean"
	ndk-build clean
	#echo "rm bin libs obj -rf"
	rm bin libs obj -rf
	exit 0
    fi
}


check_env(){
    echo "##################################################"
    echo "Start to check the environment..."
    NDK_BUILD=`which ndk-build`
    echo NDK_BUILD=${NDK_BUILD}
    if [ "${NDK_BUILD}" = "" ];then
	echo "Can't find ndk-build,Have you added the ndk-build path to PATH?"
	echo "Please run \"which ndk-build\" to check the it"
	exit 0
    else
	echo "Find ndk-build successfully,continue..."
	HOST_OS=`uname -s`
	echo "HOST_OS=${HOST_OS}"
	echo "PATH=${PATH}"
	if [ "${HOST_OS}" = "Linux" ];then
	    echo ""
	else
	    #In Windows,the make maybe occur conflicts with othe make tool,so set the PATH here.
	    export PATH=/cygdrive/c/cygwin/bin:`dirname ${NDK_BUILD}`
	    #echo "PATH=${PATH}"
	fi
    fi
    if [ ! -f ${ANDROID_MK} ];then
	echo "./${ANDROID_MK} doesn't exist,Please check the project directory."
	exit 0
    fi


    #Append "include $(BUILD_SYSTEM)/install-binary.mk" to build-static-library.mk
    #Add this to support build static library.
    TO_BE_APPEND="include \$(BUILD_SYSTEM)/install-binary.mk"
    build_static_library_mk="`dirname ${NDK_BUILD}`/build/core/build-static-library.mk"
    #echo build_static_library_mk=$build_static_library_mk

    if [ ! -f ${build_static_library_mk} ];then
	echo "Can't find build-static-library.mk,maybe can't create the static library."
    else
	TMP=`grep "${TO_BE_APPEND}" ${build_static_library_mk}|grep -v ^#`

	if [ "${TMP}" = "" ];then
	    echo "echo ${TO_BE_APPEND} >>${build_static_library_mk}"
	    echo ${TO_BE_APPEND} >>${build_static_library_mk}
	fi
    fi
    echo "Check the environment...SUCCESS"
}


#if it is ndk-build -h,just print the help and exit.
echo ${1}
#A=`grep "^-" "${1}"`
if [ "${1}" = "-h" ];then
    ndk-build ${1}
    exit 0
fi

#Check the environment.
check_env

#check whether it is ndk-build clean,and exit.
clean $1


#Prepare the Needed StaticLibraries if need.
prepareStaticLibraries

n="${#}"
#echo parameters number = ${n}

case $# in
    0) ndk-build;;
    1) ndk-build ${1};;
    2) ndk-build ${1} ${2};;
    3) ndk-build ${1} ${2} ${3};;
    *) break;;
esac

#Install the built library.
installLibraries

#Print the start time and finish time.
printTime

/var/www/dokuwiki/wiki/data/pages/android/android-jni/ndkbuild-shell-for-linux.txt · Last modified: 2016/05/05 13:07 by 127.0.0.1