Android practices 11: Android Studio, Gradle, and androidgradle
After more than two months of AS experience, I think it is time to migrate the Android development environment to the. Currently, the latest version is 1.0.2. In addition to the occasional crash of the UI control drag and drop (Ubuntu), other functions are very smooth and efficient. What impressed me was the following:
- Smart sensing has a superior experience, comparable to
- Layout preview: the preview page is displayed in real time after handwriting layout, which facilitates layout adjustment and optimization.
- Fast and smooth editing without getting stuck with eclipse
- The layout or source code contains icons and color previews, which are very intuitive.
- Excellent debugging experience
- With the integration of Terminal, partners who like command line operations do not need to start additional terminals.
In a word, it is very easy to use!
Android Studio is originated from the Community version of IntelliJ IDEA. The build tool is Gradle, the next generation build tool. In addition, some tools customized by Google for Android, AS will inevitably become the classic version of Android development tools.
Android Studio Installation
Adnroid is not available on the official website. We can download the AS from other websites and upgrade it to 1.0.2.
AS does not have low requirements on the system, but I still have no pressure on the i7 processor + 8 GB memory.
Windows
Microsoft Windows 8/7/Vista/2003 (32 or 64-bit)2 GB RAM minimum, 4 GB RAM recommended400 MB hard disk spaceAt least 1 GB for Android SDK, emulator system images, and caches1280 x 800 minimum screen resolutionJava Development Kit (JDK) 7Optional for accelerated emulator: Intel processor with support for Intel VT-x, Intel EM64T (Intel 64), and Execute Disable (XD) Bit functionality
Mac OS X
Mac OS X 10.8.5 or higher, up to 10.9 (Mavericks)2 GB RAM minimum, 4 GB RAM recommended400 MB hard disk spaceAt least 1 GB for Android SDK, emulator system images, and caches1280 x 800 minimum screen resolutionJava Runtime Environment (JRE) 6Java Development Kit (JDK) 7Optional for accelerated emulator: Intel processor with support for Intel VT-x, Intel EM64T (Intel 64), and Execute Disable (XD) Bit functionality
On Mac OS, run Android Studio with Java Runtime Environment (JRE) 6 for optimized font rendering. You can then configure your project to use Java Development Kit (JDK) 6 or JDK 7.
Linux
GNOME or KDE desktopGNU C Library (glibc) 2.11 or later2 GB RAM minimum, 4 GB RAM recommended400 MB hard disk spaceAt least 1 GB for Android SDK, emulator system images, and caches1280 x 800 minimum screen resolutionOracle Java Development Kit (JDK) 7
Decompress the downloaded package to the specified path. When I work in Ubuntu, I directly put it under/opt. The extracted content is as follows:
android-studio3$ lsbin build.txt gradle Install-Linux-tar.txt lib license LICENSE.txt NOTICE.txt plugins
It is worth mentioning that gradle is here. Later, we can use gradle for simple compilation. First, we need to execute the binary studio. sh startup as, AS mentioned in install-linux-tar.txt, we can put this bin directory into system variables, and then start AS by entering studio. sh. For example, I add the following content to. bashrc:
export PATH="$PATH:/opt/android-studio3/bin"export PATH="$PATH:/opt/android-studio3/gradle/gradle-2.2.1/bin"
The first startup will detect the sdk and upgrade it to the latest version. If you do not need a proxy, this step will fail and the AS will not start successfully. The solution is to allocate your Adnroid SDK Manager to a domestic image. For details, refer to "Practical Android skills: latest Android Development Environment (Eclipse + ADT + Android 5.0)". after the upgrade succeeds, the AS will start successfully. The following things are simple and the interface is clear, just like you use other ides, and you can get started quickly. However, the project results are greatly changed (compared with Eclipse), and the shortcut keys are also greatly changed, so they must be adapted for a period of time. Download a Keymap and print it out. If you use it, it will soon enter the status.
Tips:
Open the project and modify the sdk and jdk path. The Settings are as follows: File-> Other Settings-> Default project Structure
If you like black themes, switching to the vampire Darcula topic is a good choice: File-> Settings-> Appearance-> Theme
Gradle
The project contains two build. gradle files:
$ find -name build.gradle./app/build.gradle./build.gradle
The build. gradle in the root directory of the project only performs commen configuration. The build. gradle in the app is a more detailed configuration for this app:
apply plugin: 'com.android.application'android { compileSdkVersion 21 buildToolsVersion "21.1.2" defaultConfig { applicationId "com.linc.arrowfall" minSdkVersion 17 targetSdkVersion 21 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.3'}
Make Project (Ctrl + F9), The Gradle Console in the lower right corner will print the following information:
Executing tasks: [:app:compileDebugSources]Configuration on demand is an incubating feature.:app:preBuild:app:preDebugBuild:app:checkDebugManifest:app:preReleaseBuild:app:prepareComAndroidSupportAppcompatV72103Library UP-TO-DATE:app:prepareComAndroidSupportSupportV42103Library UP-TO-DATE:app:prepareDebugDependencies:app:compileDebugAidl UP-TO-DATE:app:compileDebugRenderscript UP-TO-DATE:app:generateDebugBuildConfig UP-TO-DATE:app:generateDebugAssets UP-TO-DATE:app:mergeDebugAssets UP-TO-DATE:app:generateDebugResValues UP-TO-DATE:app:generateDebugResources UP-TO-DATE:app:mergeDebugResources UP-TO-DATE:app:processDebugManifest UP-TO-DATE:app:processDebugResources UP-TO-DATE:app:generateDebugSources UP-TO-DATE:app:compileDebugJava:app:compileDebugNdk:app:compileDebugSourcesBUILD SUCCESSFULTotal time: 10.23 secs
Let's put down the Gradle in AS. Let's start with the Gradle command line. I just mentioned that the Gradle path in AS is under the android-studio3/gradle/gradle-2.2.1/bin, add it to the environment variable (AS above) so that you can use the gradle tool at any location. Here is the simplest use of gradle:
$ gradle -v------------------------------------------------------------Gradle 2.2.1------------------------------------------------------------Build time: 2014-11-24 09:45:35 UTCBuild number: noneRevision: 6fcb59c06f43a4e6b1bcb401f7686a8601a1fb4aGroovy: 2.3.6Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013JVM: 1.7.0_71 (Oracle Corporation 24.71-b01)OS: Linux 3.13.0-45-generic amd64
Gradle is working normally. Let's get a hello world. Create a new build. gradle file and add the following code:
task helloworld << { println 'hello world'}
This task outputs only one log and runs the following command:
$ gradle -q helloworldhello world
The parameter-q only prints logs, and this task is the function.
Execute gradle-gui in this directory to bring up the gradle of the graphic interface and view other information about helloworld.
Compile a Java program
Now I try to compile a simplest Java program. Create a directory and a file in the directory as follows:
$ mkdir -p src/main/java/com/linc; vim src/main/java/com/linc/HelloWorld.java
The Code is as follows:
package com.linc;public class HelloWorld { public static void main(String args[]) { System.out.println("hello, world"); }}
The build. gradle file is at the same level as the src directory, with only one row in it:
apply plugin: 'java'
RunGradle build:
$ gradle build:compileJava:processResources UP-TO-DATE:classes:jar:assemble:compileTestJava UP-TO-DATE:processTestResources UP-TO-DATE:testClasses UP-TO-DATE:test UP-TO-DATE:check UP-TO-DATE:buildBUILD SUCCESSFULTotal time: 2.206 secs
The directory structure becomes as follows:
$ tree -L 6.├── build│ ├── classes│ │ └── main│ │ └── com│ │ └── linc│ │ └── HelloWorld.class│ ├── dependency-cache│ ├── libs│ │ └── helloworld.jar│ └── tmp│ ├── compileJava│ └── jar│ └── MANIFEST.MF├── HelloWorld.java└── src └── main └── java └── com └── linc └── HelloWorld.java
Run the compiled java program:
$ java -cp build/classes/main/ com.linc.HelloWorldhello, world
The initial experience of Gradle is here, and more complex building tasks are still behind. With the powerful AS tool, Android development will become more and more fun!
Refer:
Http://www.gradle.org/documentation
Http://www.android-studio.org/index.php/88-download/