Windows: Ctrl + Alt + L
Linux: Ctrl + Shift + Alt + L
macOS: Option + Command + L
Reference: Key Commands and here are all of the commands for Windows/ Linux users and for Mac users.
As Rohit faced a problem in Ubuntu with the format code shortcut, this is due to the Ctrl + Alt + L key being used to lock the screen in Ubuntu.
I found that Ubuntu handles this keyboard shortcut first. So you should bind the Ctrl + Alt + L keyboard shortcut to something else so that it doesn't conflict with Ubuntu.
Steps
Go to System Tools → System Settings → Keyboard → Shortcuts tab → System → Lock Screen.
Select the row New Accelerator..., then press any special key with the Alpha key (e.g. Shift + L). You should've successfully changed the keyboard shortcut.
Check if the keyboard shortcut now works in Android Studio.
Alternative method
Ctrl + Alt + Shift + L (to show a dialog)
You can also use Eclipse shortcuts.
- Windows and Linux: Go to menu File → Settings → Keymap
- macOS: Go to menu Preferences → Keymap and choose Eclipse from the drop-down menu.
Using MenuBar
In Android studio 3.5, there is problem with rearranging the xml code, it also rearrange the views as well, so need to use these settings first.
Settings -> Editor -> Code Style -> XML-> Set From -> Predefined Style > Android.
Update for Android Studio 4+
Since Android Studio 4+, you might face issue shortcuts on mac. Shortcuts not working after updating to Android studio 4, in Mac? have an answer for that.
I've been struggling with the same thing for many hours, trying to get the Gson jar to work no less. I finally cracked it – here are the steps I took:
- Put the Gson jar (in my case,
gson-2.2.4.jar
) into the libs
folder
- Right click it and hit 'Add as library'
Ensure that compile files('libs/gson-2.2.4.jar')
is in your build.gradle
file (or compile fileTree(dir: 'libs', include: '*.jar')
if you are using many jar files)
Edit : Use implementation files('libs/gson-2.2.4.jar')
(or implementation fileTree(dir: 'libs', include: '*.jar')
) in Android Studio 3.0+
Do a clean build (you can probably do this fine in Android Studio, but to make sure I navigated in a terminal to the root folder of my app and typed gradlew clean
. I'm on Mac OS X, the command might be different on your system
After I did the above four, it started working fine. I think the 'Add as library' step was the one I'd previously missed, and it didn't work until I cleaned it either.
[Edit - added the build.gradle
step which is also necessary as others have pointed out]
Best Answer
Short Answer
Gradle is a build system.
Long Answer
Before Android Studio you were using Eclipse for your development purposes, and, chances are, you didn't know how to build your Android APK without Eclipse.
You can do this on the command line, but you have to learn what each tool (dx and AAPT) does in the SDK. Eclipse saved us all from these low-level, but important, fundamental details by giving us their own build system.
Now, have you ever wondered why the
res
folder is in the same directory as yoursrc
folder?This is where the build system enters the picture. The build system automatically takes all the source files (
.java
or.xml
), then applies the appropriate tool (e.g., takes.java
class files and converts them to.dex
files), and groups all of them into one compressed file - our beloved APK.This build system uses some conventions: an example of one is to specify the directory containing the source files (in Eclipse it is
\src
folder) or resources files (in Eclipse it is\res
folder).Now, in order to automate all these tasks, there has to be a script; you can write your own build system using shell scripting in Linux or batch files syntax in Windows. Got it?
Gradle is another build system that takes the best features from other build systems and combines them into one. It is improved based off of their shortcomings. It is a JVM-based build system. That means you can write your own script in Java, which Android Studio makes use of.
One cool thing about Gradle is that it is a plugin-based system. This means if you have your own programming language and you want to automate the task of building some package (output like a JAR file for Java) from sources, then you can write a complete plugin in Java or Groovy (or Kotlin, see here), and distribute it to the rest of the world.
Why did Google use it?
Google saw one of the most advanced build systems on the market and realized that you could write scripts of your own with little-to-no learning curve, and without learning Groovy or any other new language. So they wrote the Android plugin for Gradle.
You must have seen
build.gradle
file(s) in your project. That is where you can write scripts to automate your tasks. The code you saw in these files is Groovy code. If you writeSystem.out.println("Hello Gradle!");
then it will print on your console.What can you do in a build script?
A simple example is that you have to copy some files from one directory to another before the actual build process happens. A Gradle build script can do this.