I want to build an Android Studio app (the Gradle build system), but I want to do this via the command line.
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.
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.
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!
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)
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..
This is an attempt at a full guide
1. Install Gradle and the Android SDK
Either
- Install these however you see fit
- Run
./gradlew
, orgradlew.bat
if on Windowschmod +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
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
- If there are complaints about licenses or SDKs not being found, fix the
directory permissions
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]
- Resulting .apks are in
gradle assembleDebug
orassembleRelease
: build just the debug or release versionsgradle installDebug
orinstallRelease
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
For Mac use this command
./gradlew task-name
enter code here
Create script file with below gradle and adb command, Execute script file
./gradlew clean
./gradlew assembleDebug ./gradlew installDebug
adb shell am start -n applicationID/full path of launcher activity
Official Documentation is here:
To build a debug APK, open a command line and navigate to the root of your project directory. To initiate a debug build, invoke the assembleDebug
task:
gradlew assembleDebug
This creates an APK named module_name-debug.apk
in project_name/module_name/build/outputs/apk/
.
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
来源:https://stackoverflow.com/questions/24398041/build-android-studio-app-via-command-line