zoukankan      html  css  js  c++  java
  • Android Studio And Gradle

    AS特色:
    • 智能感知体验特好,堪比VS 
    • 布局预览,手写布局后预览页面即时显示,便于布局调整和优化
    • 编辑速度飞快流畅,毫无eclipse的卡顿
    • 布局或源码中有图标和颜色的预览,十分直观
    • 调试时体验极佳
    • 集成了Terminal,喜欢命令行操作的伙伴不用额外启动终端了。

    Android Studio 源于IntelliJ IDEA的社区版,构建工具是Gradle这个下一代的构建工具,再加上Google为Android定制的一些工具,那么AS必然会成为Android开发工具的经典款。

    Android Studio的安装

    Adnroid官网上不去,我们可以到其他网站下载AS,然后再升级到1.0.2。 
    AS对系统的要求不低,不过我这i7处理器+8G内存还是毫无压力啊。

    Windows

    Microsoft® Windows® 8/7/Vista/2003 (32 or 64-bit)
    2 GB RAM minimum, 4 GB RAM recommended
    400 MB hard disk space
    At least 1 GB for Android SDK, emulator system images, and caches
    1280 x 800 minimum screen resolution
    Java Development Kit (JDK) 7
    Optional 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 recommended
    400 MB hard disk space
    At least 1 GB for Android SDK, emulator system images, and caches
    1280 x 800 minimum screen resolution
    Java Runtime Environment (JRE) 6
    Java Development Kit (JDK) 7
    Optional 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 desktop
    GNU C Library (glibc) 2.11 or later
    2 GB RAM minimum, 4 GB RAM recommended
    400 MB hard disk space
    At least 1 GB for Android SDK, emulator system images, and caches
    1280 x 800 minimum screen resolution
    Oracle® Java Development Kit (JDK) 7
    

    下载后将其解压到你指定的路径。我在Ubuntu下工作,就直接将其放到/opt下了。解压后内容如下:

    android-studio3$ ls
    bin  build.txt  gradle  Install-Linux-tar.txt  lib  license  LICENSE.txt  NOTICE.txt  plugins
    

    值得一说的是,gradle就在这里,一会儿我们可以直接用gradle去做简单的编译工作。首先,我们要执行bin下的studio.sh启动AS,就像Install-Linux-tar.txt中说的,我们可以将这个bin目录放到系统变量中,以后启动AS只需输入studio.sh即可。比如我在.bashrc中添加如下内容:

    export PATH="$PATH:/opt/android-studio3/bin"
    export PATH="$PATH:/opt/android-studio3/gradle/gradle-2.2.1/bin"
    

    首次启动会检测sdk并升级到最新,如果不用代理,这一步我们无法通过,AS就不会启动成功。解决办法就是将自己的Adnroid SDK Manager配好代理到国内的镜像,请参照《Android实战技巧之九:最新Android开发环境(Eclipse+ADT+Android 5.0) 》,顺利通过升级后,AS会成功启动。后面的事情就简单了,界面清晰明了,就像你用其他IDE一样,上手很快。但是项目结果变化很大(与Eclipse相比),快捷键变化也很大,都要适应一段时间。下载一份Keymap打印出来,用到了就看看,会很快进入状态。

    Tips: 
    打开项目后修改sdk和jdk路径,设置如下:File –>Other Settings –>Default project Structure 
    如果你喜欢黑色风格的主题,那么切换到吸血鬼Darcula主题是个不错的选择:File–>Settings–>Appearance–>Theme


    Gradle

    项目中有两个build.gradle文件,如下:

    $ find -name build.gradle
    ./app/build.gradle
    ./build.gradle

    项目根目录下的build.gradle只做了比较commen的配置,app下的build.gradle是针对此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)时,右下角Gradle Console就会打印如下信息:

    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:compileDebugSources
    
    BUILD SUCCESSFUL
    
    Total time: 10.23 secs

    先放下AS中的Gradle,我们先从Gradle命令行说起。刚刚提到AS中自带的Gradle路径在android-studio3/gradle/gradle-2.2.1/bin下,将其加入到环境变量(如上),这样在如何位置都可以使用gradle工具了。下面来作一下gradle最简单的使用:

    $ gradle -v
    
    ------------------------------------------------------------
    Gradle 2.2.1
    ------------------------------------------------------------
    
    Build time:   2014-11-24 09:45:35 UTC
    Build number: none
    Revision:     6fcb59c06f43a4e6b1bcb401f7686a8601a1fb4a
    
    Groovy:       2.3.6
    Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
    JVM:          1.7.0_71 (Oracle Corporation 24.71-b01)
    OS:           Linux 3.13.0-45-generic amd64

    gradle是正常工作了,下面来个hello world吧。新建一个build.gradle文件,加入如下代码:

    task helloworld << {
            println 'hello world'
    }

    这个task只输出一条log,执行如下命令:

    $ gradle -q helloworld
    hello world

    参数-q只是打印log,这个task也就是此功能而已。 
    在此目录下执行gradle –gui,调出图形界面的gradle,看看helloworld的其他信息。

    编译Java程序 
    现在尝试编译一个最简单的Java程序,在刚刚的目录下新建目录和文件如下:

    $ mkdir -p src/main/java/com/linc; 
    vim src/main/java/com/linc/HelloWorld.java

    代码内如如下:

    package com.linc;
    
    public class HelloWorld {
        public static void main(String args[]) {
            System.out.println("hello, world");
        }
    }

    build.gradle文件与src目录平级,内如只有一行:

    apply plugin: 'java'

    此时运行gradle 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
    :build
    
    BUILD SUCCESSFUL
    
    Total time: 2.206 secs


    此时的目录结构变为如下所示:
    $ 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
    

    运行编译好的java程序:

    $ java -cp build/classes/main/ com.linc.HelloWorld
    hello, world
  • 相关阅读:
    JSP内置对象Session
    jsp 中对jar 包的引用
    Windows下AndroidStudio 中使用Git(AndroidStudio项目于GitHub关联)
    教你怎么使用Github for Windows?
    cannot run program "git.exe":CreateProcess error=2
    如何在Android Studio上使用Github
    怎么在Android studio配置Github账号
    js下读取input中的value值
    jQuery获取多种input值的方法
    Android studio 导入github工程
  • 原文地址:https://www.cnblogs.com/sytfyf/p/5120356.html
Copyright © 2011-2022 走看看