I am developing a dynamic app generator, so I would need to run the command on a runtime basis using Java on a server
From the Android Developer site:
Managing Projects from the Command Line
Building APK from Command Line
You will need Gradle to build from the command line. Once you have that you can create the APK like this:
Windows:
> gradlew.bat assembleRelease
Unix:
$ ./gradlew assembleRelease
This creates your Android application .apk file inside the project bin/ directory, named -unsigned.apk.