How can I compile simple app for android in linux terminal from text file (source codes)? [duplicate]

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

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