Gradle use in Android Studio

Source: Internet
Author: User
Tags jcenter

A) Basic configuration
  1. Build configuration

    buildscript { repositories {     jcenter()  } dependencies {     ‘com.android.tools.build:gradle:1.2.3‘ } }

    Android script

    apply plugin: ‘com.android.application‘

    Android configuration

    22 buildToolsVersion "22.0.1"}

    Project structure

    myapp├──build.gradle├──settings.gradle└── app├──build.gradle├── build├──libs└──src└── main├──java│└──com .package.myapp└──res├──drawable├──layout└──etc.      
  2. Gradle wrapper structure (these new items are added to the user and do not need to be added again)

    myapp/ ├── gradlew  ├── gradlew.bat └── gradle/wrapper/     ├── gradle-wrapper.jar     └── gradle-wrapper.properties

    Run Build task-list all available tasks

    $ ./gradlew tasks

    Generating app-debug.apk Tasks

    $ ./gradlew assembleDebug# Apk路径: MyApp/app/build/ outputs/apk
  3. Manually import the Eclipse-android project (Auto Import please click "Next" for consecutive points)
    Create the Build.gradle file under the project path:

    Buildscript {repositories {jcenter ()} dependencies {Classpath ' com.android.tools.build:gradle:1.2.3 '}} Apply plugin:  ' com.android.application ' Android {compilesdkversion 22 buildtoolsversion  "22.0.1"  Sourcesets {main {manifest.srcfile  ' androidmanifest.xml ' java.srcdirs = [ ' src '] resources.srcdirs = [ ' src '] aidl.srcdirs = [ ' src '] renderscript.srcdirs = [ ' src '] res.srcdirs = [ ' Res '] Assets.srcdirs = [ ' assets ']} androidtest.setroot ( ' tests ')}} dependencies {Compile Filetree (dir:  ' Libs ', include: [ ' *.jar ']) }

    PS can also copy and paste the source code of the Eclipse-android project into the project in Android Studio

II) Custom Configuration
  1. Gradle ALL file Structures

    MyApp├── build.gradle├── settings.gradle└── app └── build.gradle

    Settings.gradle

    include ‘:app‘

    Myapp/build.gradle

    buildscript { repositories {     jcenter() } dependencies {     ‘com.android.tools.build:gradle:1.2.3‘ } }allprojects { repositories {     jcenter()  }}

    Myapp/app/build.gradle

    ApplyPlugin  ' com.android.application ' Android {compilesdkversion 22 buildtoolsversion  "22.0.1" Defaultconfig {ApplicationID  "com.gradleforandroid.gettingstarted" minsdkversion 14 targetsdkversion Span class= "Hljs-number" >22 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:22.2.0 '}     
  2. Basic tasks

    $ ./gradlew assemble -为所有构建类型创建apk$ ./gradlew check 运行所有的检查,比如说Android Lint,如果发现问题可终止任务$ ./gradlew build 运行以上两个任务$ ./gradlew clean -清除生成的apk++++$ ./gradlew connectedCheck - 在设备上运行测试$ ./gradlew deviceCheck - 远程设备运行测试$ ./gradlew installDebug/installRelease - 在设备商安装指定版本$ ./gradlew uninstall - 卸载

  3. Build types parameter settings for different versions-Buildconfig/resource Value

    Android {buildtypes {debug {Buildconfigfield "string",  "Api_url",  "\"/http Test.example.com/api\ "" Buildconfigfield  "boolean",  "LOG_HTTP _calls ", " true "Resvalue " string ", " App_name ", " Example DEBUG "} release {Buildconfigfield " string ",  "Api_url",  "\" Http://example.com/api\ "" Buildconfigfield " Span class= "Hljs-string" > "boolean",  "Log_http_calls",  "string",  "App_name",                
  4. Global Settings (Build.gradle of the project root directory)

    allprojects {  ‘com.android.application‘  android {      compileSdkVersion 22      buildToolsVersion "22.0.1"  } }

    Setting Global parameters

    ext {  22  buildToolsVersion = "22.0.1"}

    Using Parameters in Myapp/app/build.gradle

    {  compileSdkVersion rootProject.ext.compileSdkVersion  buildToolsVersion rootProject.ext.buildToolsVersion}
  5. Default task (Myapp/build.gradle)

    ‘clean‘, ‘assembleDebug‘
III) Dependency Management
  1. Warehouse
    Preset Configuration Warehouse

    { mavenCentral() jcenter() mavenLocal()}

    Remote Warehouse

    repositories { maven {     "http://repo.acmecorp.com/maven2"     credentials {         username ‘user‘        password ‘secretpassword‘     } } ivy {     url "http://repo.acmecorp.com/repo" }}

    Local Warehouse

    repositories { maven {     "../repo" }}
  2. Local dependency
    Project file dependencies

    dependencies {  ‘libs‘, include: [‘*.jar‘])}

    The structure and configuration of the native library

    # 结构:app ├── AndroidManifest.xml └── jniLibs     ├── armeabi     │   └── nativelib.so     ├── armeabi-v7a     │   └── nativelib.so     ├── mips     │   └── nativelib.so     └── x86         └── nativelib.so# 配置:android {  sourceSets.main {      jniLibs.srcDir ‘src/main/libs‘  }}

    Libray Project

    # 修改Android插件:apply plugin: ‘com.android.library‘# settings.gradle新增libray项目:include ‘:app‘, ‘:library‘# app内引用library项目:dependencies { compile project(‘:library‘)}
  3. Dependency concept

    <待续>
  4. Add dependencies within Android Studio


IV) Build variants
    • <待续>
Five) Multi-module construction management
    1. Accelerate Build
      在gradle.properties里面添加:org.gradle.parallel=true
VI) Testing
  1. Unit Test
    Using JUnit

    # 结构:app└─── src├─── main│ ├─── java     │    │    └─── com.example.app     │    └───res     └─── test          └─── java               └─── com.example.app# 依赖:dependencies { testCompile ‘junit:junit:4.12‘}

    Using robolectric

    # dependency: ApplyPlugin' Org.robolectric ' dependencies {compile filetree (Dir' Libs ',Include: [' *.jar ']) compile' com.android.support:appcompat-v7:22.2.0 ' Testcompile' Junit:junit:4.12 ' Testcompile' org.robolectric:robolectric:3.0 ' Testcompile' org.robolectric:shadows-support:3.0 '}# Demo:@RunWith (Robolectrictestrunner.Class@Config (manifest ="App/src/main/androidmanifest.xml", SDK =18) public class mainactivitytest { @Test public void Clickingbuttonshouldchangetext () {appcompatactivity activity = robolectric.buildactivity (mainactivity.class). Create (). get (); button button = (button) Activity.findviewbyid (r.id.button); textview TextView = (textview) Activity.findviewbyid (r.id.label); Button.performclick (); Assertthat (Textview.gettext (). toString (), Equalto (activity.getstring (R.string.hello (_robolectric))); } }
  2. Functional Testing
    Using Espresso

    <待续>
  3. Test coverage level
    Using Jacoco

    <待续>
VII) Create tasks and plugins
    1. <待续>
Eight) Configure CI
    1. <待续>
IX) Custom configuration-Advanced
  1. Reduce apk file size
    Using Proguard
    android { buildTypes {     release {         true         proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘     } } }
    Shrink resource file-auto (< manual to Be continued >)
    android { buildTypes {     release {         true         shrinkResources true     } } }
  2. Accelerate Build

    org.gradle.parallel=true # 并行构建org.gradle.daemon=true # 开启Gradle守护进程org.gradle.jvmargs=-Xms256m -Xmx1024m # 配置JVM<参照>


    Using Profiling

    <待续>

    Using Jack (Java Android Compiler Kit) and Jill (Jack Intermediate Library Linker)

    <待续>
  3. Ignore Lint

    android {  lintOptions {      false  }}
  4. Using Ant

    <待续>
  5. App Pack-Advanced
    Split apk

    android {  splits {      density {          enable true          exclude ‘ldpi‘, ‘mdpi‘          compatibleScreens ‘normal‘, ‘large‘, ‘xlarge‘ } } }生成结果:app-hdpi-release.apkapp-universal-release.apkapp-xhdpi-release.apkapp-xxhdpi-release.apkapp-xxxhdpi-release.apk



Wen/Tai Tanhao _dockone (Jane book author)
Original link: http://www.jianshu.com/p/02cb9a0eb2a0
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

Gradle use in Android Studio

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.