转载自:http://tools.android.com/tech-docs/new-build-system/gradle-experimental

Introduction

The new experimental plugin is based on Gradle’s new component model mechanism, while allows significant reduction in configuration time. It also includes NDK integration for building JNI applications. This user guides provides details on how to use it and highlights the difference between the new plugin and the original plugin.

WARNING: Note that this is plugin is at the experimental stage. The Gradle API for the new component model is not final, which means it’ll only work with a specific version of Gradle until the APIs are final.

Additionally, the DSL is likely change significantly, as APIs to create the DSL are finalized.

This is a very early preview of the plugin for feedback on performance and NDK integration.

Requirements

  • Gradle 2.5 only
  • Android NDK r10e (if you are using NDK)
  • SDK with Build Tools at least version 19.0.0 and we aim to minimize the amount of changes needed for the migration process in the future. Some features may require a more recent version.

Migrating from Traditional Android Gradle Plugin

A typical Android Studio project may have a directory structure as follows. File that needs to be change is highlighted in red:
There are some significant changes in the DSL between the new plugin and the traditional one.

.

├── app/

│   ├── app.iml

│   ├── build.gradle

│   └── src/

├── build.gradle

├── gradle/

│   └── wrapper/

│       ├── gradle-wrapper.jar

│       └── gradle-wrapper.properties

├── gradle.properties

├── gradlew*

├── gradlew.bat

├── local.properties

├── MyApplication.iml

└── settings.gradle

./gradle/wrapper/gradle-wrapper.properties

  • The new plugin supports only gradle-2.5.

#Wed Apr 10 15:27:10 PDT 2013
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip

./build.gradle

  • Classpath for the plugin is com.android.tools.build:gradle-experimental instead of com.android.tools.build:gradle.

  • The current version is 0.2.0.

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
   repositories {
       jcenter()
   }
   dependencies {
       classpath 'com.android.tools.build:gradle-experimental:0.2.0'

// NOTE: Do not place your application dependencies here; they belong
       // in the individual module build.gradle files
   }
}

allprojects {
   repositories {
       jcenter()
   }
}

./app/build.gradle

There are significant changes to the DSL of the plugin.  We understand that many of the changes are frustrating and seem unnecessary, and our goal is to remove some of these current changes to minimize the migration process from the traditional plugin in the future.

DSL Changes:

  • Plugin name is com.android.model.application instead of com.android.application. Or use apply plugin: 'com.android.model.library' if you want to create an Android aar library.

  • Configuration is wrapped with the model { } block

  • Most properties require the = operator

  • Adding elements to a Collection should be done using the += operator.

Current DSL Limitations that will hopefully go away:

  • buildTypes, productFlavors and signingConfigs must be place outside of the  android { } block.

  • Nested options within the android { } block must be configured using the with keyword.

  • Properties are only set with their direct types only, with no way to accept other types and adapting them. For instance:

    • Properties of type File accepts only File instead of File and String objects.

    • minSdkVersion cannot directly receive either an integer or string (for codename).

  • Creating a buildType or productFlavor requires calling the create method.  Modifying an existing one such as the release and debug buildType can be done using the just the name.

  • The DSL for modifying variants and their tasks is very, very limited right now.

apply plugin: 'com.android.model.application'

model {
   android {
       compileSdkVersion = 22
       buildToolsVersion = "22.0.1"

defaultConfig.with {

applicationId =  "com.example.user.myapplication"
           minSdkVersion.apiLevel = 15
           targetSdkVersion.apiLevel = 22
           versionCode = 1
           versionName = "1.0"

buildConfigFields.with {
create() {
type = "int"
name = "VALUE"
value = "1"
}
           }

}

}

android.buildTypes {
       release {
           minifyEnabled = false
           proguardFiles += file('proguard-rules.pro')

}
   }

android.productFlavors {
       create("flavor1") {

applicationId = ‘com.app’
       }
   }

// Configures source set directory.

android.sources {

main {

java {

source {

srcDir 'src'

}

}

}

}

}

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
   compile 'com.android.support:appcompat-v7:22.2.0'
}

Ndk Integration

The experimental plugin comes with NDK integration for creating native applications.  To use the NDK integration:

  • Use the SDK Manager inside Studio to download the NDK.

  • Set ndk.dir in local.properties or the ANDROID_NDK_HOME environment variable to the directory containing the NDK.

  • Add an android.ndk block to the model in build.gradle.

Known Limitations

  • There’s no support for NDK-only modules. The only supported project types are hybrid app projects and hybrid Library Projects.

  • Consumed Library project don’t impact compilation of jni code in the consuming project (ie the AAR so files are simply packaged in the APK)

  • No support for creating and depending on static libraries

  • No support for using a NDK modules like cpu_features

  • No support for integrating external build systems.

The build.gradle of a simple NDK application may look like this:

apply plugin: 'com.android.model.application'

model {
   android {
       compileSdkVersion = 22
       buildToolsVersion = "22.0.1"

}

android.ndk {

moduleName = "native"

}

}

*Note that the moduleName is required.  It determines the name of the resulting native library.

By default, it will look in src/main/jni for C/C++ file.  Configure android.sources to change the source directory.

model {
   android {
       compileSdkVersion = 22
       buildToolsVersion = "22.0.1"

}

android.ndk {

moduleName = "native"

}

android.sources {

main {

jni {

source {

srcDir 'src'

}

}

}

}

}

Various build options can be set within the android.ndk { } block.  For example,

model {
   android {
       compileSdkVersion = 22
       buildToolsVersion = "22.0.1"

}

android.ndk {

// All configurations that can be changed in android.ndk.

moduleName = "native"

toolchain = "clang"

toolchainVersion = "3.5"

// Note that CFlags has a capital C, which is inconsistent with

// the naming convention of other properties.  This is a

// technical limitation that will be resolved

CFlags += "-DCUSTOM_DEFINE"

cppFlags += "-DCUSTOM_DEFINE"

ldFlags += "-L/custom/lib/path"

ldLibs += "log"

stl = "stlport_static"

}

android.buildTypes {

release {

ndk.with {

debuggable = true

}

}

}

android.productFlavors {

create("arm") {

ndk.with {

// You can customize the NDK configurations for each

// productFlavors and buildTypes.

abiFilters += "armeabi-v7a"

}

}

create("fat") {

// If ndk.abiFilters is not configured, the application

// compile and package all suppported ABI.

}

}

// You can modify the NDK configuration for each variant.

components.android {

binaries.afterEach { binary ->

binary.mergedNdkConfig.cppFlags.add(

"-DVARIANT=\"" + binary.name + "\"")

}

}

}

Samples

Additional samples can be found at https://github.com/googlesamples/android-ndk.

使用Android Studio和Gradle编译NDK项目之Experimental Plugin User Guide的更多相关文章

  1. Android Studio工程Gradle编译报错

    一.环境的搭建: 首先搭建好AndroidStudio环境.我使用的是Ubuntu 12.04系统(由于此机器还要运行其他程序,为避免兼容性问题,暂未更新到最新,而继续沿用此稳定版),java和jdk ...

  2. Android 项目利用 Android Studio 和 Gradle 打包多版本APK

    在项目开发过程中,经常会有需要打包不同版本的 APK 的需求. 比如 debug版,release版,dev版等等. 有时候不同的版本中使用到的不同的服务端api域名也不相同. 比如 debug_ap ...

  3. 使用Android Studio自带的NDK编译JNI

    /********************************************************************************** * 使用Android Stud ...

  4. android studio 使用 jni 编译 opencv 完整实例 之 图像边缘检测!从此在andrid中自由使用 图像匹配、识别、检测

    目录: 1,过程感慨: 2,运行环境: 3,准备工作: 4,编译 .so 5,遇到的关键问题及其解决方法 6,实现效果截图. (原创:转载声明出处:http://www.cnblogs.com/lin ...

  5. android studio 使用 jni 编译 opencv 完整实例 之 图像边缘检测!

    目录: 1,过程感慨: 2,运行环境: 3,准备工作: 4,编译 .so 5,遇到的关键问题及其解决方法 6,实现效果截图. ------------------------------------- ...

  6. android studio使用CMake和NDK,实现应用自身被卸载时打开某一网址

    实现应用自身被卸载时打开某一网址的c代码 MyActivity: public class MyActivity extends Activity { /** * Called when the ac ...

  7. Android studio下gradle Robolectric单元测试配置

    android studio下gradle Robolectric单元测试配置 1.Robolectric Robolectric是一个基于junit之上的单元测试框架.它并不依赖于Android提供 ...

  8. Android Studio :Android Studio 与 Gradle 深入【二】

    转载:http://www.apkbus.com/forum.php?mod=viewthread&tid=255063&extra=page%3D2%26filter%3Dautho ...

  9. Android Studio导入GitHub上的项目常见问题(有例子)

    前言:github对开发者而言无疑是个宝藏,但想利用它可不是件简单的事,用Android studio导入开源项目会遇到各种问题,今天我就以github上的一个图片轮播项目为例,解决导入过程中的常见问 ...

随机推荐

  1. orcale 之 数据完整性约束

    数据完整性约束是对数据描述的某种约束的条件,在关系型数据库中一般有:完整性约束,实体完整性约束,参照完整性约束和用户自定义完整性约束. 实体完整性约束 在数据库中一张表一般对应一个现实生活中的实体,比 ...

  2. Log4J 配置文件模板及代码说明

    相对而言,这个日志系统的配置就没那么随意了,而且有些功能用起来也不是那么爽,譬如动态读取配置文件.不过鉴于使用这个日志的系统还是很多,所以也写一个demo贴出来,风格跟log4j2一样,配置的说明全在 ...

  3. whatwg-fetch

    fetch 是什么 XMLHttpRequest的最新替代技术 fetch优点 接口更简单.简洁,更加语义化 基于promise,更加好的流程化控制,可以不断then把参数传递,外加 async/aw ...

  4. HDU 5700——区间交——————【线段树+枚举】

    区间交 Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...

  5. OutSystems学习笔记。

    ew job and new software, new challenge as well. OutSystems这软件挺好上手的.虽然没有中文文档,但英文文档超级详细,堪称傻瓜版SOP 照着步骤写 ...

  6. js&&jQuery实现省市区三级联动

    代码参考了:https://codepen.io/anon/pen/WzyNQB;根据自己的需要修改了一下,挂个出处 1.前端页面代码 <html> <head> <me ...

  7. sqlserver - FOR XML PATH

    FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...

  8. 八、cent OS下tomcat启用APR并发模式

    Tomcat支持三种接收请求的处理方式: BIO.NIO.APR ,本文记录tomcat配置APR模式,也是首选的模式.(Tomcat7 或以下,在 Linux 系统中默认使用BIO方式) 安装依赖库 ...

  9. 从Pc转向H5开发遇到的适配问题思考

    1.首先说滚动条 移动端开发在不设置任何适配和viewport宽度的情况下,以iphone5为例:屏幕界面的逻辑分辨率是320x568,在谷歌浏览器的界面下屏幕的可视宽度是980px(谷歌设置的,每个 ...

  10. 第5章 css与背景相关的样式background

    background-origin 设置元素背景图片的原始起始位置. 语法: background-origin : border-box | padding-box | content-box; 参 ...