1. 程式人生 > >使用Android Studio進行NDK開發和除錯(gradle-experimental之官方文件的翻譯說明)

使用Android Studio進行NDK開發和除錯(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庫