As described here in the Android Studio docs, one can build an Android app from the command line with the gradle wrapper generated by Android Studio. The command line window in Android Studio shows exactly this, i.e. a call to gradlew assembleDebug (or a similar task). However starting a terminal on macOS and calling the gradle wrapper with that exact call sometimes yields other results, i.e. either one is able to build from Android Studio, or the command line, or both (which is ideal, but unfortunately not always true).
Who can explain what other settings the Android Studio IDE sets for their environment and where to find them, how to reproduce these settings easily in a CI environment (e.g. Jenkins, Bamboo, ...) and how one would consistently store these settings in a VCS along an Android project.
In the root of the project you have gradlew.bat, which is how you can run it in the first place. If you open this file, you see the code for building and everything else Gradle does.
Whether you build from the command line or ANdroid Studio, the gradlew.bat file is used. So building and other stuff you do with the gradlew command all use the same file whether it is from the command line or Android Studio's integrated tools.
You can open the file(s, there's gradlew and gradlew.bat in the project root) and see how it works if you wanted too, though these files are usually automatically generated by Android Studio (it is possible to make your own config as well, though there's rarely a need for that). And for including in vcs, make sure the files aren't listed in .gitnore (or whatever vcs ignore extension you have)
Related
Currently, I am getting "Finished with error: Gradle task assembleDebug failed with exit code 1" while trying to build a Flutter project (flutter run). The logs do not help much. Therefore, i want to run "gradlew build" or similar manually with stacktrace option to see what is happening under the hood. What is the command for that ?
For posterity I will post my comment as an answer too and I'll elaborate it a bit.
When you create a flutter project there are two new folders created inside the main folder, one is android and one is ios.
The android folder contains the Android native code and all the android configurations, you can handle it as a native android project.
The ios folder contains the iOS native code and all the ios configurations, it also has the xcworkspace file which can be opened with Xcode like a normal ios project.
Now you can run platform specific commands in each folder, like i said, the folders contain actual native projects.
So for Android you could do:
cd android/
./gradlew clean
./gradlew build
(clean and build the project)
For iOS you could do:
cd ios/
pod repo update
pod install
(update the pod repo and install the pods)
Just a short reminder, if you want to create apk/ipa from the native folders, don't forget to run flutter build in the main folder, otherwise you might get outdated code in your apk/ipa.
Go to the folder where you have gradle installed(the place where your GRADLE_HOME variable points to).
Move inside the wrapper folder
Move inside the dists folder which is inside the wrapper folder
Delete everything that you can find inside the dists folder(cached gradle wrapper)
Run/launch your android flutter project again. It should re-download the gradle wrapper and if you don't have any connection problems your project should run correctly.
Note: I'm having the same problem because of my unstable internet connection. I'm ending up with a corrupted gradle wrapper file and the download doesn't restart.
If the download gets interrupted and fails to completely download and launch your project repeat all of the steps.
I am running Gradle script. At the beginning I print PATH environment variable:
println System.getenv( 'PATH' )
The output is:
/usr/bin:/bin:/usr/sbin:/sbin
But the real PATH environment variable is (printed from terminal):
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sh........
Question is, why that PATH env variable do not math in Gradle script and in terminal?
Thing is, that I am building Android, which use Gradle as build system. And I have c++ code, which is build by CMake. And in CMake i run some python scripts, which executes some external commands. But when Gradle changes PATH variable, those commands are not found and I am getting an error. It is not an option to use full path, when executing those commands, like:
/usr/local/bin/my_mega_command
When I run CMake or python script directly, then everything works as expected.
Android Studio does not pass its environment down to the Gradle daemon that it spawns. Usually, we see this in the form of missing custom environment variables, but apparently it affects PATH changes as well.
I had the exact same issue where running gradle tasks from the Terminal (inside or outside Android Studio) would correctly take the PATH set in the environment (i.e. via .zprofile or any of the other possible methods), but not when running via the Gradle commands from the Android Studio Toolbar. What ended up working for me was changing the JDK version used from the one that came with Android Studio (JDK 11) to JDK 1.8, and suddenly it worked. I don't know why this changes anything, but now the PATH is taken from the environment set in the system and everything works.
Lately I came to know the power of Gradle as a build system and as an Android developer I wanna understand it deeply.
One article said the following:
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
Now I have some doubts which goes as follow:
What is Gradle Wrapper and gradlew.bat?
If I've got Android studio installed and it is using gradle to build my apps (so gradle is already installed on my system), do I still need to install gradle for build purpose from command line? As when i write any commend like gradle, gradlew on my command line I get error saying gradlew is not recognized as internal or external command (the same error for other commands). I may be using it on wrong path, help me on what path do I need to use Gradle related command?
If I need to download and install it, how and where can I find the file? And the other processes?
I am using a Windows machine for this.
The Gradle Wrapper is an optional part of the Gradle build system. It consists of four files that you check into version control system. The Unix start script <your root project>/gradlew, the <your root project>/gradlew.bat Windows start script, <your root project>/gradle/wrapper/gradle-wrapper.jar which contains the class files for the wrapper and is started by the start scripts and <your root project>/gradle/wrapper/gradle-wrapper.properties which contains some configuration for the wrapper, for example which Gradle version to use to build the project.
In my opinion, each and every Gradle project, even the tiniest, should make use of the Gradle wrapper.
The Gradle wrapper makes sure your build is always run with the same Gradle version, no matter who executes the build and where or that Gradle is installed or not, as long as the one uses the Gradle wrapper to build the project. This means you can design your build for that Gradle version and be sure that the build will not fail, just because someone is using a different version of Gradle and thus is also an important step in build reproducibility.
Also, someone wishing to build your project only needs to have Java installed and that's it. He does not need to have any Gradle version installed. Actually any already installed Gradle version is ignored. The Gradle wrapper checks whether in ~/.gradle/ the version that is necessary for the build is already present, because some Gradle wrapper of any project put it there already. If it is present already, it is used, otherwise it is automatically downloaded.
If you type gradlew on the commandline and the command is not found, that means you didn't put your root projects path to the PATH environment variable (I wouldn't recommend doing that either), nor are you currently in your root project's directory. To run a Gradle build, you have to be anywhere inside your project and call Gradle or the Gradle wrapper. But like with any executable file that is not on the path, you have to provide its path of course. So if you are in your root project directory, you can simply do gradlew. if you are in <root project dir>/foo/bar/, you would need to call ../../gradlew.
The Gradle Wrapper files are generated by the implicitly available Gradle task wrapper and then get checked into the VCS of the project in question. If those four files are not present for a project, it does not use the Gradle wrapper and you should post an improvement request to the project to add it.
If some project does not use the Gradle wrapper, but builds with Gradle, you can either install Gradle and use gradle instead of gradlew, or you can even call the Gradle wrapper of any other project that you have available on disk. The build will then be run with the Gradle version that wrapper or Gradle installation is using and thus might not behave as expected, which is why really each and every project should use the wrapper if it uses Gradle.
Edited after comments
Gradle is a build system.
This gradle-wrapper is kind of the primary interface to to build Android projects. It the part of Gradle-build-system and does some primary check if gradle in installed or not.
gradlew.bat - its a batch file used on Windows. You can even open it with a notepad to view the instructions in it. Batch files are like 'commands' written in a file to be executed. You use it (in case of Windows) to execute build commands. It also checks if gradle is installed or not. And in case it is not, it downloads and installs it.
Example : to build android app on Windows:
Open command prompt
Navigate to your project's root directory
execute gradlew.bat assembleDebug
It starts the wrapper, checks if Gradle is installed there and
executes all the 'gradle specific' commands to build your project.
Do you need to install Gradle ?
Actually, no. Its the job of this gradlew script to check for that. If gradle its not already there, it would automatically download it and use it for all later builds.
gradlew.bat IS the Gradle Wrapper (for Windows in this case). Gradle Wrapper is just a small utility that will ensure that Gradle is installed (or install it if necessary) so you can always build the project. Gradle itself allows you to manage dependencies and build configurations for your project.
If you have installed Android Studio, you have Gradle installed and are good to go. (Technically, each project will have it's own wrapper to handle installing/using Gradle)
As I mentioned above, you are good to go.
In the end Gradle is a command line tool that you use to build your project and you could very well use that directly (though you don't have to) since it is exactly what Android Studio uses to build your project.
This is the first Android application I am running. I am reading up the tutorial and following it (as much as I can).
I would like to get myself comfortable with the CLI instead.
I created a project using "android" executable. Although to build the application it says to run the "gradlew" executable which is supposed to be in my project's root. I don't see it there.
What might I be missing ?
Created the project using
android create project --target 1 --name HelloWorld --path HelloWorld --activity HW --package com.developers.helloworld
Indeed, it does not exist, because when you create a project from the command line, the generated project is an Android project without gradle.
You have 3 options however:
Use the official IDE supported by Google - Android Studio - to build and run your apps. (recommended), or
Download and install Ant and
a) Change directory to your project root.
b) Execute from command line ant debug to compile your project.
c) Then adb install YourApp.apk to transfer the apk to your device (once compiled successfully, apk you'll find in the bin directory), or
Manually add Gradle to your project. But keep in mind that Gradle is a build tool that expects a project to have a specific directory structure, if you don't want to configure anything.
In general, is good to know what happens behind a shiny IDE, and know let say, how to generate an android project on your own, from the command line. This also is useful when you want to use an IDE of your preference, or have more control over the Android build.
But, honestly, if you are just getting started with Android, I would highly suggest you use Android Studio. With this option you get an intelligent code editor, implicit support for Gradle, access to a multitude of open source projects from Github that already use the gradle project structure, and other advantages.
UPDATE: how to view logs:
from command line: adb logcat
from GUI: navigate to <android-sdk>/tools, find and open monitor.
Once the window opens, you'll find a tab called LogCat, usually located in the left - bottom of the window, but if it is not there, then go to: Window -> Show View -> Android -> LogCat -> OK, to add it.
note: make sure only one device is connected to adb bridge
for filtering:
check this & this.
The tools/andoird create project has the --gradle flag to specify to use the gradle template instead of ant template.
Use tools/android create project help for more details.
Android Studio “beta” 0.8.6
on 64 bit Ubuntu 14.04 LTS.
Problem:
When importing source code from various sources either gradle based or eclipse based in to Android Studio the source code is missing from the project view.
Screen shot post import “project” in Android Studio.
![enter image description here][1]
Screen shot of files within the project using Nautilus.
Note Nautilus clearly shows the source code, Gradle, IDE files. Android Studio shows the Gradle files only.
The event log event log:
Error running app: Module is not found
So I tried to put the source code into the project using:
Android Studio main menu → File → New Module
AND
Android Studio main menu → File → Import Module
Tried most of the possible combinations in New Module and Import Module.
All the combinations Gave:
“This Location is already imported” within the Module Wizard.
So how do I get the source code in to the project?
These problems with Gradle are beyond annoying and approaching the the R Object disaster in eclipse. Where none of the build sequences repeat with any reasonable consistency.
Thank you in advance for your help, ultra disgusted Mark.
Sorry about lack of images.
Got it to WORK!
Used the command line, terminal described at:
http://developer.android.com/sdk/installing/studio-build.html
Section of that page titled “Build the project from the command line” has instruction for all three operating systems.
C louse Android Studio, for some reason having it open interfers with the gradlew on my system anyway.
On Ubuntu Linux.
In GNOME Terminal.
Navigate to the folder containing the project.
Enter: chmod +x gradlew
gradlew is the gradle wrapper.
If your project is missing the gradlew file use the Wizard in Android Studio to make a separate project and copy the gradlew file to the folder where you are working.
The chmod +x makes the gradlew executable, need to be run once for each project.
The project Should build, if you get errors do your best to look them up and fix them.
Once I did the above and imported in to Android Studio the project worked perfect in with in the IDE and even ran in an AVD.
Beyond me how the command line works fine and the IDE cause nothing but grief and extreme aggravation (~ two days in my case).
SO if Android Studio will not import or open a project try the command line. Give that you have the proper build.gradle files (between the android plug in changing the stuff that goes in build.gradle changes as well so it a Screaming Nightmare to keep up with it).
I hope that helped somebody out. Mark