Build Android Studio app via command line

匿名 (未验证) 提交于 2019-12-03 02:49:01

问题:

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

回答1:

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.



回答2:

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.



回答3:

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

  • run this command gradlew.bat assembleDebug

  • Output window look like this

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     } }}

  • run this command gradlew.bat assembleRelease

Done.Good Luck!



回答4:

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

cd  ./gradlew ./gradlew tasks ./gradlew --help

Should get you started..



回答5:

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)



回答6:

You can execute all the build tasks available to your Android project using the Gradle wrapper command line tool. It's available as a batch file for Windows (gradlew.bat) and a shell script for Linux and Mac (gradlew.sh), and it's accessible from the root of each project you create with Android Studio.

To run a task with the wrapper, use one of the following commands:

On Windows:

gradlew task-name

On Mac or Linux:

./gradlew task-name

To see a list of all available build tasks for your project, execute tasks:

gradlew tasks

Use the following command to exclude task4 from the above script.

C:\> gradle task4 -x test

If the command is executed successfully, you will get the following output.

The following example shows selecting a project ‘hello’ from myproject.gradle file, which is located in the subdir/.

task hello << {    println "using build file '$buildFile.name' in '$buildFile.parentFile.name'." }

You can use the following command to execute the above script.

C:\> gradle -q -b subdir/myproject.gradle hello

If the command is executed successfully, you will get the following output.

using build file 'myproject.gradle' in 'subdir'.

Obtaining Build Information,

Gradle provides several built-in tasks for retrieving the information details regarding the task and the project. This can be useful to understand the structure and the dependencies of your build and for debugging problems. You can use project report plugin to add tasks to your project, which will generate these reports.

Listing Projects

C:\> gradle -q projects


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!