commit 65a00df3274d618b66510fc68313f111ab1cb78a
parent 52ddce1682ef459e250a50d12508455235c69ca0
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Sun, 15 Dec 2024 22:38:17 +0530
Add steps in config.mk to create an app that compiles
Works on Windows with SHELL=cmd
Add dependency on mor(requirements.ini)
Diffstat:
8 files changed, 262 insertions(+), 57 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1 +1,4 @@
out/
+tags
+*.apk
+*.keystore
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.thing.quoter">
+
+ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+ <uses-permission android:name="android.permission.INTERNET" />
+
+ <uses-sdk android:minSdkVersion="24"
+ android:targetSdkVersion="29" />
+
+ <application
+ android:allowBackup="true"
+ android:icon="@mipmap/ic_launcher"
+ android:roundIcon="@mipmap/ic_launcher_round"
+ android:label="@string/app_name"
+ android:supportsRtl="true"
+ android:theme="@style/AppTheme">
+
+ <meta-data
+ android:name="firebase_analytics_collection_deactivated"
+ android:value="true" />
+ <meta-data
+ android:name="google_analytics_adid_collection_enabled"
+ android:value="false" />
+
+ <activity android:name=".view.MainActivity">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+
+ <service
+ android:name=".service.QuoterDownloader"
+ android:enabled="true"
+ android:exported="true" />
+ </application>
+</manifest>
+\ No newline at end of file
diff --git a/COPYING b/COPYING
@@ -1,6 +0,0 @@
-nb is in the public domain.
-
-To the extent possible under law, Bharatvaj Hemanth <bharatvaj@getsh.org>
-has waived all copyright and related or neighboring rights to this work.
-
-http://creativecommons.org/publicdomain/zero/1.0/
diff --git a/Makefile b/Makefile
@@ -0,0 +1,31 @@
+.ONESHELL:
+.PHONY: build deps test deploy setup
+
+VERSION = 0.1.0
+SRC ?= $(wildcard src/*.scala)
+DEST ?= .bin
+MAIN ?= Main
+PACKAGE_NAME=com.thing.quoter
+PROGRAM := Quoter
+ANDROID_SDK_VERSION = 29.0.3
+ANDROID_COMPILE_SDK = 29
+ANDROID_MIN_SDK = 24
+ANDROID_KEYSTORE:=debug.keystore
+
+DEPS += com/android/org.osgi/osgi.core\:3.2.0
+
+.DEFAULT_GOAL := all
+
+include config.mk
+APP_SRC_DIR:=$(subst .,/,$(PACKAGE_NAME))
+# out/$(APP_SRC_DIR)/R.java
+
+out/$(PACKAGE_PATH)/%: out/$(PACKAGE_PATH)/%.aar
+ unzip $< -d $@
+
+$(error $(wildcard java/$(PACKAGE_PATH)/view/*.class))
+all: $(wildcard out/$(PACKAGE_PATH)/view/*.class) #$(CLASS_DIR)/$(PACKAGE_PATH)/R.class out/app-debug.apk
+
+# TODO Throw error if the folder $(LIB_DIR) is missing
+clean:
+ $(RMDIR) "$(GEN_DIR)"
diff --git a/Makefile.sample b/Makefile.sample
@@ -1,18 +0,0 @@
-.PHONY: build deps test deploy setup
-
-VERSION = 0.1.0
-SRC ?= $(wildcard src/*.scala)
-DEST ?= .bin
-MAIN ?= Main
-PROGRAM := myjavaproject
-
-DEPS += com/android/org.osgi/osgi.core\:3.2.0
-
-.DEFAULT_GOAL := all
-
-include config.mk
-
-all: $(BIN_DIR)/$(PROGRAM).jar
-
-clean:
- $(RMDIR) $(BIN_DIR)/
diff --git a/README b/README
@@ -2,3 +2,23 @@ make-java
=========
An attempt to build maven projects using plain GNUmakefile
+
+Build Requirements
+------------------
+These tools are expected to be in path
+- unzip (Extracting .aar files)
+- mor (Downloading .aar files)
+- aapt2 (Compiles resource files into .apk)
+- zipalign
+- javac
+- GNUMake
+
+tree
+----
+ | res/
+ | src/
+ | AndroidManifest.xml
+ | Makefile
+ | config.mk
+ | requirements.ini
+
diff --git a/config.mk b/config.mk
@@ -1,35 +1,73 @@
JAVAC ?= javac
SCALAC ?= scalac
+KOTLINC ?= kotlinc
JAR ?= jar
JAVA ?= java
SCALA ?= scala
+SHELL = cmd
-TARGET_MAIN = myjavaprogram.Main
+ROOT_DIR:=$(dir $(realpath $(firstword $(MAKEFILE_LIST))))
+# Directory variables
+ifeq ($(OS),Windows_NT)
+define /
+\\
+endef
+ ANDROID_SDK ?= $(USERPROFILE)/AppData/Local/Android/sdk
+else
+ /:=/
+ ANDROID_SDK ?= /opt/android-sdk
+endif
+
+prepend_slash=$1/
+
+RES_DIR:=$(ROOT_DIR)res
+BIN_DIR?=out/bin
+GEN_DIR:=$(BIN_DIR)/gen
+CLASS_DIR:=$(BIN_DIR)/classes
+PACKAGE_PATH = $(subst .,/,$(PACKAGE_NAME))
+ANDROID_BUILD_TOOLS = $(ANDROID_SDK)/build-tools/$(ANDROID_SDK_VERSION)
+ANDROID_PLATFORMS = $(ANDROID_SDK)/platforms/android-$(ANDROID_COMPILE_SDK)
+
+LIB_DIR?=out/lib
+
+# Absolute Paths
+AGEN_DIR:=$(ROOT_DIR)$(GEN_DIR)
+
+# Tools and flags
+AAPT:=$(ANDROID_BUILD_TOOLS)/aapt
+AAPT2:=$(ANDROID_BUILD_TOOLS)/aapt2
+D8:=$(ANDROID_BUILD_TOOLS)/d8
+
+JAVAC_FLAGS := -source 8 -target 8
+ifeq (BUILD_TYPE,RELEASE)
+D8_BUILD_TYPE:=--release
+endif
+
+TARGET_MAIN = $(PACKAGE_NAME).Main
# Repos
MAVEN = https://repo1.maven.org/maven2
JVM_TARGET_VESRION ?= 1.8
-LIB_DIR ?= out/lib
-BIN_DIR ?= out/bin
+DOWNLOAD = curl -fLs
+RM = rm -rf
+RMDIR = rm -rf
+MKDIR:=mkdir -p
ifeq ($(OS),Windows_NT)
- RM = del /s /q
- # TODO check whether it's working
- RMDIR = rmdir /s /q
- MKDIR = mkdir
-else
- RM = rm -rf
- RMDIR = rm -rf
- MKDIR = mkdir -p
+ ifeq ($(SHELL),cmd)
+ RM:=del /s /q
+ RMDIR:=rmdir /s /q
+ MKDIR:=mkdir
+ endif
endif
-DOWNLOAD = curl -fLs
+
FINAL_PROGRAM = $(PROGRAM)-$(VERSION)
-empty:=
-space := $(empty) $(empty)
-$(space) := $(space)
+define space
+
+endef
LIBS = $(wildcard $(LIB_DIR)/*.jar)
@@ -41,43 +79,123 @@ pkgver = $(lastword $(subst :,$(space), $(1)))
pkgname = $(firstword $(subst :, $(space), $(1)))
-maven_url = $(MAVEN)/$(call pkgname, $(1))/$(call pkgver, $(1))/$(notdir $(call pkgname, $(1)))-$(call pkgver, $(1)).jar
+maven_url = $(MAVEN)/$(call pkgname, $(1))/$(call pkgver, $(1))/$(notdir $(call pkgname, $(1)))-$(call pkgver, $(1))
jarfile = $(notdir $(subst :,-, $(1))).jar
+DEPLOY_BIN = $(BIN_DIR)/$(FINAL_PROGRAM)
-$(LIB_DIR)/%: setup
- $(info > Downloading '$*' into $(LIB_DIR)/)
- @$(DOWNLOAD) $(call maven_url, $*) --output "$(LIB_DIR)/$(call jarfile, $*)"
-
-CLASSPATH := $(subst $(space),:,$(wildcard $(LIB_DIR)/*.jar))
+#$(LIB_DIR)/%: $(BIN_DIR) $(LIB_DIR)
+# $(info > Downloading '$*' into $(LIB_DIR)/)
+# @$(DOWNLOAD) $(call maven_url, $*) --output $(LIB_DIR)/$*
+#
+#CLASSPATH := $(subst $(space),:,$(wildcard $(LIB_DIR)/*.jar))
+#
deps: $(foreach DEP, $(DEPS), $(LIB_DIR)/$(DEP))
%.class: %.scala
$(SCALAC) $< -sourcepath $(SRC) -classpath $(CLASSPATH) -target:jvm-$(JVM_TARGET_VESRION)
-DEPLOY_BIN = $(BIN_DIR)/$(FINAL_PROGRAM)
+%.class: %.kt
+ $(KOTLINC) $< -sourcepath $(SRC) -classpath $(CLASSPATH) -target:jvm-$(JVM_TARGET_VESRION)
+
+
$(DEPLOY_BIN).jar: $(BIN_DIR)/$(PROGRAM).jar
$(info > Building '$(DEPLOY_BIN)')
- # $(MKDIR) $(BIN_DIR)/classes
- # $(foreach JARFILES, $(wildcard $(LIB_DIR)/*.jar), \
- # cd $(BIN_DIR)/classes; $(JAR) xvf $(realpath $(JARFILES)) \
- # )
+ :# $(MKDIR) $(BIN_DIR)/classes
+ :# $(foreach JARFILES, $(wildcard $(LIB_DIR)/*.jar), \
+ :# cd $(BIN_DIR)/classes; $(JAR) xvf $(realpath $(JARFILES)) \
+ :# )
echo "$(SCALA) -classpath "$(CLASSPATH):$(FINAL_PROGRAM).jar" $(TARGET_MAIN) $$*" > $(DEPLOY_BIN)
cd $(BIN_DIR)/classes; $(JAR) xvf $(realpath $(BIN_DIR)/$(PROGRAM)).jar
cd $(BIN_DIR)/classes && $(JAR) cvf $(realpath $(DEPLOY_BIN)).jar *
chmod +x $(DEPLOY_BIN)
-$(BIN_DIR)/$(PROGRAM).jar: $(SRC)
- @make setup
+$(BIN_DIR)/$(PROGRAM).jar: $(SRC) $(BIN_DIR) $(LIB_DIR)
$(info > Building '$(BIN_DIR)/$(PROGRAM)')
@$(SCALAC) -classpath "${CLASSPATH}" $^ -d $@ -target:jvm-$(JVM_TARGET_VESRION)
- @echo "$(SCALA) -classpath "$(CLASSPATH):$(PROGRAM).jar" $(TARGET_MAIN) $$*" > $(BIN_DIR)/$(PROGRAM)
- @chmod +x $(BIN_DIR)/$(PROGRAM)
+ echo "$(SCALA) -classpath "$(CLASSPATH):$(PROGRAM).jar" $(TARGET_MAIN) $$*" > $(BIN_DIR)/$(PROGRAM)
+ ifeq($(OS),Windows_NT)
+ chmod +x $(BIN_DIR)/$(PROGRAM)
+ endif
deploy: $(BIN_DIR)/$(PROGRAM)-$(VERSION).jar
-setup:
- @$(MKDIR) $(BIN_DIR)
- @$(MKDIR) $(LIB_DIR)
+# Create necessary directories
+%/:
+ $(MKDIR) "$*"
+
+STYLE_PATHS+=$(CURDIR)/test
+ANDROID_JAR:=$(ANDROID_PLATFORMS)/android.jar
+JAVAC_FLAGS:=$(JAVAC_FLAGS) -Xbootclasspath:$(ANDROID_JAR)
+
+# Generate R.java
+r-files: $(RES_DIR) $(GEN_DIR)/ $(CLASS_DIR)
+ :# Generate R.java from resources
+ $(AAPT) package --target-sdk-version $(ANDROID_COMPILE_SDK) --auto-add-overlay -f -m -J $(GEN_DIR)/ -S $(RES_DIR) -M AndroidManifest.xml -I $(ANDROID_JAR) $(foreach STYLE_PATH, $(STYLE_PATHS), -S $(STYLE_PATH)/res -I $(STYLE_PATH)/classes.jar) -S $(ANDROID_PLATFORMS)/data/res --custom-package $(PACKAGE_NAME) --generate-dependencies
+
+rwildcard=$(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d))
+
+#RES_SRCS:=$(call rwildcard,.,*.xml)
+#
+#$(GEN_DIR)/%.arsc.flat: $(RES_DIR)/%.xml
+# $(MKDIR) $(dir $@)
+# :# TODO Use speciazied AAPT2 tool to avoid
+# :# "error: bad resource path." error
+# $(AAPT2) compile -o $(dir $@) $(dir $<)$(notdir $<)
+
+RES_SRCS:=appcompat-1.2.0.zip core-1.3.2.zip res.zip
+RES_SRCS:=$(foreach RES_SRC,$(RES_SRCS),out/$(RES_SRC) )
+
+out/%.zip: out/lib/%.aar
+ unzip $^ -d $(basename $^)
+ $(AAPT2) compile -o $@ --dir $(basename $^)/res
+
+out/res.zip: res
+ $(AAPT2) compile -o $@ --dir res
+
+# Generate resources and compile R.class
+out/app-resource.apk: $(RES_SRCS) $(call prepend_slash,$(GEN_DIR)) $(call prepend_slash,$(CLASS_DIR))
+ :# Generate R.java from resources
+ $(AAPT2) link $(RES_SRCS) -I $(ANDROID_JAR) --manifest AndroidManifest.xml -o $@ --java $(CLASS_DIR)
+
+$(CLASS_DIR)/$(PACKAGE_PATH)/R.class: $(CLASS_DIR)/$(PACKAGE_PATH)/R.java
+ :# Compile R.java to R.class
+ $(JAVAC) $(JAVAC_FLAGS) -d $(CLASS_DIR) $<
+
+#out/$(PACKAGE_PATH)/%.class: out/$(PACKAGE_PATH)/%.java
+# @echo # Compile R.java to R.class
+# $(JAVAC) $(JAVAC_FLAGS) $^
+
+LIBS=picasso appcompat
+
+ANDROID_CLASSPATH= $(CLASS_DIR);$(PACKAGE_PATH)$(subst $(space);,;,$(foreach LIB,$(LIBS),;$(CURDIR)/$(LIB_DIR)/$(LIB)/classes.jar))
+
+out/%.class: java/%.java
+ $(JAVAC) $(JAVAC_FLAGS) -classpath $(ANDROID_CLASSPATH) -d $(BIN_DIR) $^
+
+out/classes.dex: out/app-resource.apk $(CLASS_DIR)
+ $(D8) $(D8_BUILD_TYPE) $(wildcard $(CLASS_DIR)/$(PACKAGE_PATH)/*.class) --output $(dir $@) --lib $(ANDROID_JAR) $(foreach LIB,$(LIBS),--lib $(LIB_DIR)/$(LIB)/classes.jar)
+
+# Verify with: $(ANDROID_BUILD_TOOLS)/zipalign -c -v 4 $@
+out/app-aligned.apk: out/app-resource.apk out/classes.dex
+ zip.exe -uj "$<" "out/classes.dex"
+ $(ANDROID_BUILD_TOOLS)/zipalign -p -f -v 4 $< $@
+
+%.keystore:
+ keytool -genkey -v -keystore $@ -storepass android -alias androiddebugkey -keypass android -keyalg RSA -keysize 2048 -validity 10000
+
+out/app-debug.apk: out/app-aligned.apk
+ $(ANDROID_BUILD_TOOLS)/apksigner sign --min-sdk-version $(ANDROID_MIN_SDK) --ks $(ANDROID_KEYSTORE) --ks-pass pass:android --ks-key-alias androiddebugkey --out $@ $<
+
+clean-dex:
+ $(RM) "$(CURDIR)/out$(/)classes.dex"
+
+# Clean generated files
+clean-r:
+ $(RM) $(GEN_DIR)/$(PACKAGE_PATH)/R.java
+ $(RM) $(CLASS_DIR)/$(PACKAGE_PATH)/R.class
+ $(RM) $(CLASS_DIR)/$(PACKAGE_PATH)/R$$*.class
+
+.PHONY: r-files r-files2 clean-r
+\ No newline at end of file
diff --git a/requirements.ini b/requirements.ini
@@ -0,0 +1,16 @@
+[$]
+maven=https://repo1.maven.org/maven2
+google-maven=https://dl.google.com/dl/android/maven2
+# TODO add this feature to mor.cmd
+# inline value extraction
+key=appcompat
+value=1.2.0
+
+[out/lib]
+picasso-2.71828=$maven/com/squareup/picasso/picasso/2.71828/picasso-2.71828.aar
+appcompat-1.2.0=$google-maven/androidx/$key/$key/$value/$key-$value.aar
+
+;[app/build|.jar]
+;android.support.v7.appcompat=28.0.0
+;android.support.v4=28.0.0
+;android.support.design=28.0.0