使用Android Studio进行NDK开发和调试(gradle-experimental之官方文档的翻译说明)

版本更新

版本更新的信息可以通过以下路径进行获取
gradle-experimental版本更新

环境要求

  1. Gradle(参照三里边的版本要求)
  2. Android NDK r10e
  3. Build Tool在19.0.0以上的SDK

Gradle版本要求

不同版本的Experimental Plugin需要不同版本的gradle
gradle版本參照

配置文件的更新

从传统的Android Gradle Plugin迁移到Experimental Plugin,需要進行一下的配置调整(下方的配置以Gradle Version为2.10的为例子,其他版本的配置类似

.
├── 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.properties文件

Gradle版本要求可以知道,每一个新版的plugin都对应一个指定版本的gradle,因此我们需要调整该文件

#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.10-all.zip

根目录build.gradle文件

// 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.7.0-alpha4"

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

allprojects {
   repositories {
       jcenter()
   }
}

将gradle改成gradle-experimental版本

Module下的build.gradle文件

apply plugin: "com.android.model.application"
model {
   android {
       compileSdkVersion 23
       buildToolsVersion "23.0.2"

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

buildConfigFields { create() { type "int" name "VALUE" value "1" }            }
       }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles.add(file("proguard-rules.pro"))
        }
    }
    productFlavors {
        create("flavor1") {
            applicationId "com.app"
        }
    }

    // Configures source set directory.
    sources {
        main {
            java {
                source {
                    srcDir "src"
                }
            }
        }
    }
}   
}

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

DSL发生的变化:

  1. 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.
    使用com.android.model.application代替com.android.application
  2. Configuration is wrapped with the model { } block
    所有的配置需要使用model{}包含起来
  3. Adding elements to a Collection should be done using the add method.
    向集合添加元素时,需要使用add方法

签名配置

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

model {
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"
        buildTypes {
            release {
                signingConfig = $("android.signingConfigs.myConfig")
            }
        }
    }
    android.signingConfigs {
        create("myConfig") {
            storeFile "/path/to/debug.keystore"
            storePassword "android"
            keyAlias "androiddebugkey"
            keyPassword "android"
            storeType "jks"
        }
    }
}

现在的signingConfigs需要配置在android块外

NDK的集成

使用experimental plugin集成NDK开发

  1. 下载NDK
  2. 在local.properties中配置ndk.dir
  3. 在model的build.gradle中配置 android.ndk块
apply plugin: 'com.android.model.application'
model {
   android {
       compileSdkVersion 23
       buildToolsVersion "23.0.2"
    ndk {
        moduleName "native"
    }
}
}

moduleName属性是必须的,它用于指明生成.so库的名字

资源配置

默认构建工具会在src/main/jni 目录下查找C/C++ 的文件(我前边介绍NDK开发的文章都是在src/main/下创建了一个jni的文件夹).通过下边的配置可以改变这个查找的路径

model {
   android {
       compileSdkVersion 23
       buildToolsVersion "23.0.2"
       ndk {
           moduleName "native"
       }
       sources {
           main {
               jni {
                   source {
                       srcDir "src"
                   }
               }
           }
       }
}
}

model {    
android.sources {
       main {
           jni {
               source {
                    include "someFile.txt" // This is ignored.
                    exclude "**/excludeThisFile.c"
               }
           }
       }
   }
}

通过include配置的文件会被忽略
通过exclude配置的文件被排除

其他构建选项

使用android.ndk{}块

model {
   android {
       compileSdkVersion 23
       buildToolsVersion "23.0.2"
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.add("-DCUSTOM_DEFINE")
           cppFlags.add("-DCUSTOM_DEFINE")
           ldFlags.add("-L/custom/lib/path")
           ldLibs.add("log")
           stl "stlport_static"
       }
    buildTypes {
      release {
      ndk {
      debuggable true
     }
     }
     }
       productFlavors {
           create("arm") {
               ndk {
                   // You can customize the NDK configurations for each
                   // productFlavors and buildTypes.
                   abiFilters.add("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 + "\"")
       }
   }
}

NDK多项目构建

  1. 新版的plugin插件支持创建一个native的库
apply plugin: "com.android.model.native"

model {
   android {
       compileSdkVersion 23
    ndk {
        moduleName "hello"
   }
   }
}

这里使用的是com.android.model.native而不是com.android.model.application
上边的配置作用:通过/src/main/jni下的资源生成一个libhello.so文件,
2. NDK依赖

lib/build.gradle:

apply plugin: "com.android.model.native"

model {
   android {
       compileSdkVersion 23
   ndk {
        moduleName "hello"
    }
  sources {
        main {
            jni {
  exportedHeaders {
  srcDir "src/main/headers"
  }
            }
        }
  }
}
}

任何一个依赖JNI库的项目将包含exportedheaders指定的目录的资源

app/build.gradle:

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

model {
   android {
       compileSdkVersion 23
       buildToolsVersion "23.0.2"
    sources {
        main {
            jni {
                dependencies {
                    project ":lib1"
                }
            }
        }
    }
}
}

通过该配置依赖一个JNI库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值