Is running gradle via android studio different from via Terminal ? - android

When i ran Gradle task via Android Studio' GUI (using IDE‘s own Gradle distribution), no dependencies need to download. But after that, when i did the same via terminal the first time, it would download some dependencies, for example, "lint" . Is it normal ? I don't want to cache two duplicated dependencies.

I don't want to cache two duplicated dependencies.
Gradle (and Maven) dependencies are only contained within your user home folder, in .gradle and .m2 respectively. Unless you have modified those files otherwise, of course.
Gradle just happens to be included in Android Studio. If you are able to run Gradle independently in the terminal, then you have two installations of Gradle.
The gradle wrapper would be downloaded separately of the local system installation.
for example, "lint"
Lint shouldn't be downloading anything. It only inspects your code for errors and warnings.

Related

Convert Gradle/Kotlin project to Android Project

I created a kotlin project with the command gradle init, which I can build and run without issues. I know AndroidStudio uses gradle to build/deploy an android project. With this in mind, can i modify the generated kotlin project to also become an android project? I want that when gradle build is issued, an apk file is generated, instead of the jar being generated right now. And, if possible, being able to deploy the apk to the phone connected to the computer (either via usb or wi-fi).
You actually can, just add the AGP (Android Gradle Plugin) to your build.gradle script.
And from there, structure your project to as Android Project.
Of course it's like manual type of doing it, It's easier just to start an Android Project from Android Studio cause it will generate all the files needed for Android Project.

What is gradle wrapper and the gradlew.bat file?

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.

What is the purpose of gradle-wrapper.properties in Android Studio?

I have updated my gradle-wrapper.properties to 2.10 from 2.8. But I want to know that what its purpose in Android Studio. As we didn't see any gradle-wrapper properties in eclipse.
Gradle Wrapper is a type batch or shell script that downloads and automatically configures Gradle to execute tasks. Imagine that you want to run a Gradle build, well you need to download and install Gradle in your computer, so this concept allows is to distribute our project and build configurations with no need to have Gradle installed.
Also their official gradle webiste says :
Most tools require installation on your computer before you can use
them. If the installation is easy, you may think that’s fine. But it
can be an unnecessary burden on the users of the build. Equally
importantly, will the user install the right version of the tool for
the build? What if they’re building an old version of the software?
The Gradle Wrapper solves both these problems and is the preferred way
of starting a Gradle build.

How to compile backend without android app in Android Studio

So I have an android app with an app engine backend (App-Engine Connected Android Project). Back when I used to work in Eclipse, I could compile the backend, then generate the endpoints for android, and then work on android. But now on Android Studio I don’t seem to be able to build the server without it building the android app as well. The problem with that is my android app is not ready to compile. So how do I work on the server and compile it and deployment apart from the android app? Needless to say I am extremely new to Android Studio. But I know the eclipse workflow, which is
Work on server code (develop, compile, test)
Generate client endpoints for android
Your Android Studio Project is likely to be a multi-module project i.e. it will be having a server side app and an Android app.
Android Studio uses Gradle as its build tool and hence you have the option on working on specific Gradle tasks (clean, compile, build, etc) on each of the projects separately. You need not always launch or run the overall Build process that will try to look at dependencies and then build all the stuff.
Since you want to work on the Server app separately and make that ready, I suggest that you can run the build tasks specific to that project. In the commander view you will see Gradle tasks for each project separately and you can run that. Alternately, you can also go to the terminal and look at the tasks and/or run those separately.
For e.g. let's say that you have an Android Studio project named MyApp and it has two modules in it called app and api. The app is the Android module and api is the Server side module with Cloud Endpoints. If you visit the terminal or command line and go to the root folder you can run Gradle specific tasks for each of the modules via gradle <modulename>:<taskname>.
Hope this helps.
Assuming a project structure of MyApplication (root) with app (android) and backend (appengine)
If you want to build only the backend. You should run the assemble task on that gradle module. Unfortunately running the task directly from the "Gradle" tab doesn't seem to run the task against the module alone, it runs it on everything. So you have two options.
run build from the command line : ./gradlew :backend:assemble
create a run configuration in the IDE for your gradle build, with the values
Name:Backend Build (or something),
Gradle project: MyApplication:backend (pick from dropdown),
Tasks :backend:assemble
and use that to build your backend by itself.
Both seem kind of annoying. You can also try to initiate an individual module rebuild directly when you run.
If you take a look at your backend run configuration (Run -> Edit Configurations), in the section Before launch:, you see a Make which does a project rebuild. You might need to remove that from the run configuration so it doesn't try to rebuild the whole project before deploying. To replace that with a gradle assemble of your module, click the +, and add a new Run Gradle task and configure it as above in "2".
I've filed a bug against this : https://code.google.com/p/android/issues/detail?id=168875&thanks=168875&ts=1429554595 I think the default behavior should be switched (use gradle assemble instead of make).
Generating Client libraries is done with some magic in Gradle. If you look at the build file for your Android App, it should reference a configuration on your backend called android-endpoints so the endpoint are generated as a compile dependency of your app module

Android Logcat files on Hudson/Jenkins

we are using Hudson for CI of an Android project with Android emulator plugin to run UI tests on the emulator. A git project delivers the source code.
Now I see that in the git repo, for each build a logcat file is created and put in the git project folder, which is then pushed back to git. As we are expecting a lot of builds, this could easily spam the project folder. Does anyone know if it is possible to set the destination folder for the emulator logs?
I cannot find any options in the emulator plugin or Hudson anywhere.
The logcat files are named logcat_[some_number].log.
Best regards,
Kim
Upgrade to version 2.0 of the plugin (or newer) and you'll see these logcat files are now written to a temporary directory, rather than your workspace.
If you're using Hudson, possibly you only see the very old version 1.6 in the Update Centre.
Which is just another reason to upgrade to Jenkins! :)
However, if you can't upgrade either the plugin or Jenkins (for some weird reason), just use an "Execute shell" step to delete any logcat_*.log files at the start of each build.
These files are purely temporary anyway and should probably only exist in your workspace after a failed build — otherwise the logcat output is archived automatically as logcat.txt.
Add an ant task to rename the logcat file to your convenience and remove the oldest ones.
add the logs to the .gitignore file, if you want to exclude them from your repository.

Categories

Resources