How to create android project with gradle from command line?

Viewed 29932

How to create android project with gradle from command line (without any IDE)?

Before it was with android util like below

android create project --target 1 --name MyAndroidApp --path ./MyAndroidAppProject --activity MyAndroidAppActivity --package com.example.myandroid
3 Answers

android create project was removed in SDK tools 26.0.1

See https://askubuntu.com/questions/906942/android-update-project-path-target-android-25-on-ubuntu-16-04 for more details

Trying to use it fails with:

*************************************************************************
The "android" command is deprecated.
For manual SDK, AVD, and project management, please use Android Studio.
For command-line tools, use tools/bin/sdkmanager and tools/bin/avdmanager
*************************************************************************
Invalid or unsupported command "project create"

Supported commands are:
android list target
android list avd
android list device
android create avd
android move avd
android delete avd
android list sdk
android update sdk

Workaround: Android Studio template + copy helper script

The best workaround I have is to:

  • create a new Gradle project manually from the Android Studio GUI
  • use a helper script to copy that template around, renaming classes in the process

The following helper script allows you to do:

./template NewAppName AppTemplateProject

to get a new app NewAppName from an existing AppTemplateProject created with Android studio:

#!/usr/bin/env bash
set -ex
new="$1"
shift
if [ $# -gt 0 ]; then
  old="$1"
  shift
else
  old='Min'
fi
old="$(echo "$old" | sed -E 's/\/$//')"
new="$(echo "$new" | sed -E 's/\/$//')"
new_lower="$(echo "$new" | tr 'A-Z' 'a-z')"
old_lower="$(echo "$old" | tr 'A-Z' 'a-z')"
cp -r "$old" "$new"
cd "$new"
find . -type f -print0 | xargs -0 sed -i "s/${old}/${new}/g"
find . -type f -print0 | xargs -0 sed -i "s/${old_lower}/${new_lower}/g"
cd 'app/src/main/java/com/cirosantilli/android_cheat'

GitHub upstream: https://github.com/cirosantilli/android-cheat/blob/0e6b7462705179658b48d0e2e27f0dbce308393c/gradle/template

Yes it is not perfect. But CLI support appears secondary to Google, so what can you do :-(

Here is the solution PHP Android CLI

Just run:

phpandroid create HelloWorld com.example.helloworld

and your project is scaffolded with latest android-studio parameters, like, compile & target SDK is 29, buildToolsVersion is 29.0.1 & minSdk is 16.

If you want to change, like, want to set minSdk to 14:

phpandroid create PROJECT PACKAGE --minSdk=14

PHP Android CLI can also create Variants and modules (application/library):

phpandroid create PROJECT PACKAGE --modules=common:library,admin --variants=free:type,paid:type,php:backend,firebase:backend

this will generate 2 applications, app, admin & a library common. App has 2 dimensions: type & backend 4 variants: free & paid of type dimension; php & firebase variant of backend dimension.

Related