How to create android project with gradle from command line?

前端 未结 3 524
失恋的感觉
失恋的感觉 2020-12-02 08:28

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

Before it was with android util like below



        
3条回答
  •  鱼传尺愫
    2020-12-02 09:09

    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 :-(

提交回复
热议问题