Build Android Studio app via command line

2019-01-08 05:50发布

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

9条回答
何必那么认真
2楼-- · 2019-01-08 06:10

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.

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-08 06:11

Try this (OS X only):

brew install homebrew/versions/gradle110
gradle build

You can use gradle tasks to see all tasks available for the current project. No Android Studio is needed here.

查看更多
迷人小祖宗
4楼-- · 2019-01-08 06:15

there are two build types to build your application using the Gradle build settings: one for debugging your application — debug — and one for building your final package for release — release mode.

Building in Debug Mode

  • First Navigate to Android studio project Root folder using CMD enter image description here

  • run this command gradlew.bat assembleDebug

  • Output window look like this enter image description here

Build signed apk in Release Mode

  • Edit the build.gradle file to build your project in release mode:

    android {
    ...
    defaultConfig { ... }
    signingConfigs {
        release {
            storeFile file("myreleasekey.keystore")
            storePassword "password"
            keyAlias "MyReleaseKey"
            keyPassword "password"
        }
    }
    buildTypes {
        release {
            ...
            signingConfig signingConfigs.release
        }
    }}
    

enter image description here

  • run this command gradlew.bat assembleRelease

Done.Good Luck!

查看更多
太酷不给撩
5楼-- · 2019-01-08 06:16

Only for MAC Users

Extending Vji's answer.

Step by step procedure:

  1. Open Terminal
  2. Change your directory to your Project(cd PathOfYourProject)
  3. Copy and paste this command and hit enter:

    chmod +x gradlew
    
  4. As Vji suggested:

    ./gradlew task-name
    

    DON'T FORGOT TO ADD .(DOT) BEFORE /gradlew

查看更多
地球回转人心会变
6楼-- · 2019-01-08 06:20

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
查看更多
Emotional °昔
7楼-- · 2019-01-08 06:21

Cheatsheet for running Gradle from the command line for Android Studio projects on Linux:

cd <project-root>
./gradlew
./gradlew tasks
./gradlew --help

Should get you started..

查看更多
登录 后发表回答