Build Android Studio app via command line

后端 未结 12 1711
梦谈多话
梦谈多话 2020-11-27 10:36

I want to build an Android Studio app (the Gradle build system), but I want to do this via the command line.

相关标签:
12条回答
  • 2020-11-27 11:15

    Adding value to all these answers,

    many have asked the command for running App in AVD after build sucessful.

    adb install -r {path-to-your-bild-folder}/{yourAppName}.apk
    
    0 讨论(0)
  • 2020-11-27 11:16

    Android Studio automatically creates a Gradle wrapper in the root of your project, which is how it invokes Gradle. The wrapper is basically a script that calls through to the actual Gradle binary and allows you to keep Gradle up to date, which makes using version control easier. To run a Gradle command, you can simply use the gradlew script found in the root of your project (or gradlew.bat on Windows) followed by the name of the task you want to run. For instance, to build a debug version of your Android application, you can run ./gradlew assembleDebug from the root of your repository. In a default project setup, the resulting apk can then be found in app/build/outputs/apk/app-debug.apk. On a *nix machine, you can also just run find . -name '*.apk' to find it, if it's not there.

    0 讨论(0)
  • 2020-11-27 11:17

    You're likely here because you want to install it too!

    Build

    gradlew
    

    (On Windows gradlew.bat)

    Then Install

    adb install -r exampleApp.apk
    

    (The -r makes it replace the existing copy, add an -s if installing on an emulator)

    Bonus

    I set up an alias in my ~/.bash_profile, to make it a 2char command.

    alias bi="gradlew && adb install -r exampleApp.apk"
    

    (Short for Build and Install)

    0 讨论(0)
  • 2020-11-27 11:17

    For Mac use this command

      ./gradlew task-name
    
    0 讨论(0)
  • 2020-11-27 11:18

    1. Install Gradle and the Android SDK

    Either

    • Install these however you see fit
    • Run ./gradlew, or gradlew.bat if on Windows
      • chmod +x ./gradlew may be necessary

    From this point onwards, gradle refers to running Gradle whichever way you've chosen. Substitute accordingly.

    2. Setup the Android SDK

    • If you've manually installed the SDK

      • export ANDROID_HOME=<install location>
      • You may want to put that in your ~/.profile if it's not done automatically
    • Accept the licenses: yes | sdkmanager --licenses

      • sdkmanager can be found in $ANDROID_HOME/tools/bin
      • sdkmanager may have to be run as root
    • Try running gradle

      • If there are complaints about licenses or SDKs not being found, fix the directory permissions
        • chown -R user:group $ANDROID_HOME
        • If you're reckless and/or the only user: chmod 777 -R $ANDROID_HOME

    3. Building

    • gradle tasks lists all tasks that can be run
    • :app:[appname] is the prefix of all tasks, which you'll see in the Gradle logs when you're building
      • This can be excluded when running a task

    Some essential tasks

    • gradle assemble: build all variants of your app
      • Resulting .apks are in app/[appname]/build/outputs/apk/[debug/release]
    • gradle assembleDebug or assembleRelease: build just the debug or release versions
    • gradle installDebug or installRelease build and install to an attached device
      • Have adb installed
      • Attach a device with USB debugging and USB file transfer enabled
      • Run adb devices, check that your device is listed and device is beside it

    Automatically build and install upon changes

    This avoids having to continuously run the same commands

    gradle -t --continue installDebug
    
    • -t: aka --continuous, automatically re-runs the task after a file is changed
    • --continue: Continue after errors. Prevents stopping when errors occur

    Run gradle -h for more help

    0 讨论(0)
  • 2020-11-27 11:23

    note, you can also do this within Android Studio by clicking the gradle window, and then the 'elephant' button. This will open a new window called "run anything" (can also be found by searching for that name in 'search everywhere') where you can manually type any gradle command you want in. Not "quite" command line, but often provides more of what I need than windows command line.

    This allows you to give optional params to gradle tasks, etc.

    0 讨论(0)
提交回复
热议问题