androidbuild.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/bin/bash
  2. SOURCES=()
  3. MKSOURCES=""
  4. CURDIR=`pwd -P`
  5. # Fetch sources
  6. if [[ $# -ge 2 ]]; then
  7. for src in ${@:2}
  8. do
  9. SOURCES+=($src)
  10. MKSOURCES="$MKSOURCES $(basename $src)"
  11. done
  12. else
  13. if [ -n "$1" ]; then
  14. while read src
  15. do
  16. SOURCES+=($src)
  17. MKSOURCES="$MKSOURCES $(basename $src)"
  18. done
  19. fi
  20. fi
  21. if [ -z "$1" ] || [ -z "$SOURCES" ]; then
  22. echo "Usage: androidbuild.sh com.yourcompany.yourapp < sources.list"
  23. echo "Usage: androidbuild.sh com.yourcompany.yourapp source1.c source2.c ...sourceN.c"
  24. echo "To copy SDL source instead of symlinking: COPYSOURCE=1 androidbuild.sh ... "
  25. exit 1
  26. fi
  27. SDLPATH="$( cd "$(dirname "$0")/.." ; pwd -P )"
  28. NDKBUILD=`which ndk-build`
  29. if [ -z "$NDKBUILD" ];then
  30. echo "Could not find the ndk-build utility, install Android's NDK and add it to the path"
  31. exit 1
  32. fi
  33. ANDROID=`which android`
  34. if [ -z "$ANDROID" ];then
  35. echo "Could not find the android utility, install Android's SDK and add it to the path"
  36. exit 1
  37. fi
  38. ANT=`which ant`
  39. if [ -z "$ANT" ];then
  40. echo "Could not find the ant utility, install Android's SDK and add it to the path"
  41. exit 1
  42. fi
  43. APP="$1"
  44. APPARR=(${APP//./ })
  45. BUILDPATH="$SDLPATH/build/$APP"
  46. # Start Building
  47. rm -rf $BUILDPATH
  48. mkdir -p $BUILDPATH
  49. cp -r $SDLPATH/android-project/* $BUILDPATH
  50. # Copy SDL sources
  51. mkdir -p $BUILDPATH/jni/SDL
  52. if [ -z "$COPYSOURCE" ]; then
  53. ln -s $SDLPATH/src $BUILDPATH/jni/SDL
  54. ln -s $SDLPATH/include $BUILDPATH/jni/SDL
  55. else
  56. cp -r $SDLPATH/src $BUILDPATH/jni/SDL
  57. cp -r $SDLPATH/include $BUILDPATH/jni/SDL
  58. fi
  59. cp -r $SDLPATH/Android.mk $BUILDPATH/jni/SDL
  60. sed -i "s|YourSourceHere.c|$MKSOURCES|g" $BUILDPATH/jni/src/Android.mk
  61. sed -i "s|org\.libsdl\.app|$APP|g" $BUILDPATH/AndroidManifest.xml
  62. # Copy user sources
  63. for src in "${SOURCES[@]}"
  64. do
  65. cp $src $BUILDPATH/jni/src
  66. done
  67. # Create an inherited Activity
  68. cd $BUILDPATH/src
  69. for folder in "${APPARR[@]}"
  70. do
  71. mkdir -p $folder
  72. cd $folder
  73. done
  74. ACTIVITY="${folder}Activity"
  75. sed -i "s|SDLActivity|$ACTIVITY|g" $BUILDPATH/AndroidManifest.xml
  76. sed -i "s|SDLActivity|$APP|g" $BUILDPATH/build.xml
  77. # Fill in a default Activity
  78. echo "package $APP;" > "$ACTIVITY.java"
  79. echo "import org.libsdl.app.SDLActivity;" >> "$ACTIVITY.java"
  80. echo "public class $ACTIVITY extends SDLActivity {}" >> "$ACTIVITY.java"
  81. # Update project and build
  82. cd $BUILDPATH
  83. android update project --path $BUILDPATH
  84. $NDKBUILD
  85. $ANT debug
  86. cd $CURDIR
  87. APK="$BUILDPATH/bin/$APP-debug.apk"
  88. if [ -f "$APK" ]; then
  89. echo "Your APK is ready at $APK"
  90. echo "To install to your device: "
  91. echo "cd $BUILDPATH"
  92. echo "ant debug install"
  93. exit 0
  94. fi
  95. echo "There was an error building the APK"
  96. exit 1