Makefile 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # based on https://www.hanshq.net/command-line-android.html
  2. PKGDIR := org/libsdl/app
  3. SRCDIR := ./src
  4. BLDDIR := ./build
  5. RSCDIR := ./res
  6. OBJDIR := $(BLDDIR)/obj
  7. RSCSRC := $(SRCDIR)/$(PKGDIR)/R.java
  8. SOURCES := $(shell find -wholename "$(SRCDIR)/*.java") $(RSCSRC)
  9. OBJECTS := $(SOURCES:$(SRCDIR)/%.java=$(OBJDIR)/%.class)
  10. JNILIB = ./libs
  11. JNILIBS := $(shell find -wholename "$(JNILIB)/*.so")
  12. APKDIR := $(BLDDIR)/apk
  13. APKLIB := $(APKDIR)/lib
  14. APKLIBS := $(JNILIBS:$(JNILIB)/%=$(APKLIB)/%)
  15. DLVKBC := $(APKDIR)/classes.dex
  16. UNALIGN := $(BLDDIR)/unaligned.apk
  17. UNSIGN := $(BLDDIR)/unsigned.apk
  18. KEYFILE := ./keystore.jks
  19. TARGET := $(BLDDIR)/main.apk
  20. # We will use shell variables to more conveniently refer to the SDK we installed before.
  21. SDK = $(HOME)/dev/android_sdk
  22. BUILD_TOOLS = $(SDK)/build-tools/28.0.2
  23. PLATFORM = $(SDK)/platforms/android-28
  24. AAPT = $(BUILD_TOOLS)/aapt
  25. DX = $(BUILD_TOOLS)/dx
  26. ZIPALGN = $(BUILD_TOOLS)/zipalign
  27. APKSIGN = $(BUILD_TOOLS)/apksigner
  28. apk: $(TARGET)
  29. $(RSCSRC) : $(RSCDIR) AndroidManifest.xml
  30. # The firs)t build step is to generate the R.java file, which is used for referring to resources (such as R.id.my_text above). This is done using the Android Asset Packaging Tool, aapt:
  31. $(AAPT) package -f -m -J $(SRCDIR) -S $(RSCDIR) \
  32. -M AndroidManifest.xml -I "$(PLATFORM)/android.jar"
  33. # which creates R.java.
  34. # The -f flag causes aapt to overwrite any existing output file, -m causes it to create package directories under the output directory, and -J makes it generate the R.java file and sets the output directory. -S points out the resource directory, -M specifies the manifest, and -I adds the platform .jar as an "include file".
  35. $(OBJECTS): $(SOURCES) | $(OBJDIR)
  36. # Now that all the Java code is ready, we can compile it using javac:
  37. javac -source 1.7 -target 1.7 -bootclasspath "$(JAVA_HOME)/jre/lib/rt.jar" \
  38. -classpath "$(PLATFORM)/android.jar" -d $(OBJDIR) \
  39. $(SOURCES)
  40. # (The 1.7 and -bootclasspath ure used to emit Java 7 byte code, which the Android tools expect, despite using JDK version 8.)
  41. $(DLVKBC) : $(OBJECTS) | $(OBJDIR) $(APKDIR)
  42. # The Java compiler created .class files containing byte code for the Java Virtual Machine. That then has to be translated to Dalvik byte code using the dx tool:
  43. $(DX) --dex --output=$(DLVKBC) $(OBJDIR)
  44. $(UNALIGN): $(APKLIBS) $(DLVKBC) AndroidManifest.xml | $(APKDIR)
  45. # We then package the contents of the build/apk/ directory together with the manifest and resources into an Android Application Package (APK) file, again using the aapt tool:
  46. $(AAPT) package -f -M AndroidManifest.xml -S res/ \
  47. -I "$(PLATFORM)/android.jar" \
  48. -F $(UNALIGN) $(APKDIR)
  49. # The application has now been built, but the APK file needs to be signed before any device will allow running it, even in debug mode, and zipaligned if we ever want to publish it in the Play Store.
  50. $(UNSIGN): $(UNALIGN)
  51. # First, we run the zipalign tool, which aligns uncompressed files in the APK on 4-byte boundaries for easier memory mapping:
  52. $(ZIPALGN) -f -p 4 \
  53. $(UNALIGN) $(UNSIGN)
  54. $(KEYFILE):
  55. # Then we use the Java keytool to create a key store and key for signing:
  56. keytool -genkeypair -keystore $(KEYFILE) -alias androidkey \
  57. -validity 10000 -keyalg RSA -keysize 2048 \
  58. -storepass android -keypass android
  59. $(TARGET): $(if $(wildcard $(KEYFILE)),,$(KEYFILE)) $(UNSIGN) | $(OBJDIR) $(APKDIR) $(APKLIB)
  60. # and use that key to sign our application with apksigner:
  61. $(APKSIGN) sign --ks $(KEYFILE) \
  62. --ks-key-alias androidkey --ks-pass pass:android \
  63. --key-pass pass:android --out $(TARGET) \
  64. $(UNSIGN)
  65. # Et voilà, we have successfully built an Android application.
  66. # All build artifacts will be put in subdirectories of the build directory which we create here.
  67. $(OBJDIR):
  68. mkdir -p $(OBJDIR)
  69. $(APKDIR):
  70. mkdir -p $(APKDIR)
  71. $(APKLIB):
  72. mkdir -p $(APKLIB)
  73. ndk:
  74. ndk-build
  75. $(APKLIB)/%.so: $(JNILIB)/%.so
  76. @mkdir -p $(@D)
  77. cp $< $@
  78. clean:
  79. ndk-build clean distclean
  80. # lot of dangerous rm -rf cleaning
  81. rm -f $(RSCSRC)
  82. rm -rf $(BLDDIR)
  83. rm -rf $(JNILIB)
  84. rm -rf obj # where does this come from??
  85. .PHONY : ndk $(KEYFILE) claen