如何使用 gradle 实验插件构建 NDK 模块?

How to build NDK module using gradle experimental plugin?

我正在尝试使用使用 NDK 的模块构建项目。 但是 Gradle build 出错。

这是我的build.gradle(主要项目)

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

   //     classpath 'com.android.tools.build:gradle-experimental:0.1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

当我尝试使用此 gradle:1.5.0 时出现错误 ' 错误:(17, 0) 找不到 ID 为 'com.android.model.application' 的插件。'

另一方面,如果我使用 gradle-experimental:0.1.0,它会给出错误 ' 错误:(17, 0) 找不到 ID 为 'com.android.application' 的插件。

如何解决这个错误?我的项目中有几个模块。如何设置 gradle 满足所有模块?

已编辑:

build.gradle(主要项目)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
//        classpath 'com.android.tools.build:gradle:1.5.0'

        classpath 'com.android.tools.build:gradle-experimental:0.4.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

def isReleaseBuild() {
    return isTag() && !isSnapshot()
}

def isSnapshot() {
    return version.contains("SNAPSHOT")
}

boolean isTravis() {
    return System.getenv('TRAVIS_BUILD_NUMBER') ? true : false
}

boolean isTag() {
    def tag = System.getenv('TRAVIS_TAG')
    return !(tag == null || tag.isEmpty())
}

def buildNumber() {
    return System.getenv('TRAVIS_BUILD_NUMBER') ?: "0"
}

build.gradle(应用程序)

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

repositories {
    maven {
        url "https://oss.sonatype.org/content/repositories/snapshots"
    }
    maven {
        url "https://oss.sonatype.org/content/repositories/releases"
    }
    maven {
        url "https://oss.sonatype.org/content/repositories/staging"
    }
}

model {

    android {
        compileSdkVersion = 22
        buildToolsVersion = "22.0.1"

        defaultConfig.with {
            applicationId = "com.ToxicBakery.viewpager.transforms"
            minSdkVersion.apiLevel = 16
            targetSdkVersion.apiLevel = 22
            versionCode = 1
            versionName = "1.0"
        }
    }
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles  += file('proguard-rules.txt')
        }
    }
    android.lintOptions {
        checkReleaseBuilds = false
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:cardview-v7:22.0.0'
    compile 'info.hoang8f:fbutton:1.0.5'
    compile project(':contextMenu')
    compile project(':library')
    compile project(':library2')
    compile project(':titanic')
    compile files('libs/universal-image-loader-1.9.4.jar')
    compile project(':staggeredview')
    compile project(':min3d')
}

build.gradle(上下文菜单)

apply plugin: 'com.android.model.library'
model {
    android {
        compileSdkVersion = 21
        buildToolsVersion = "21.1.2"

        defaultConfig.with {
            minSdkVersion.apiLevel = 11
            targetSdkVersion.apiLevel = 21
        }

    }
    android.lintOptions {
        checkReleaseBuilds = false
    }
}

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

build.gradle(图书馆)

apply plugin: 'com.android.model.library'
apply plugin: 'signing'

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

        defaultConfig.with {
            minSdkVersion.apiLevel = 13
            targetSdkVersion.apiLevel = 22
            versionCode = 1
            versionName = "1.0"
        }

    }
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles  += file('proguard-rules.txt')
        }
    }
    android.lintOptions {
        checkReleaseBuilds = false
    }
}
dependencies {
    compile 'com.android.support:support-v13:22.1.1'
}

apply from: '../maven_push.gradle'

build.gradle(图书馆 2)​​

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

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

        defaultConfig.with {
            minSdkVersion.apiLevel = 15
            targetSdkVersion.apiLevel = 22
            versionCode = 1
            versionName = "1.0"
        }

    }
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles  += file('proguard-rules.txt')
        }
    }
    android.lintOptions {
        checkReleaseBuilds = false
    }
}

dependencies {
}

build.gradle(min3d)

apply plugin: 'com.android.model.library'
model {
    android {
        compileSdkVersion = 15
        buildToolsVersion = "21.0.2"
        defaultConfig.with {
            minSdkVersion.apiLevel = 15
            targetSdkVersion.apiLevel = 22
            versionCode = 1
            versionName = "1.0"
        }
    }
    android.buildTypes {
        debug {

        }
        release {
            minifyEnabled = false
            proguardFiles  += file('proguard-rules.txt')
        }
    }
    android.productFlavors {
    }
    android.lintOptions {
        checkReleaseBuilds = false
    }
}

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

build.gradle(交错视图)

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

dependencies {
    compile 'com.android.support:support-v4:19.1.+'
}

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

        defaultConfig.with {
            minSdkVersion.apiLevel = 15
            targetSdkVersion.apiLevel = 22
            versionCode = 1
            versionName = "1.0"
        }

    }
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles  += file('proguard-rules.txt')
        }
    }
    android.lintOptions {
        checkReleaseBuilds = false
    }
}

build.gradle(泰坦尼克号)

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

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

        defaultConfig.with {
            minSdkVersion.apiLevel  = 11
            targetSdkVersion.apiLevel  = 19
            versionCode = 1
            versionName = "1.0"
        }
    }

    android.buildTypes{
        release {
            minifyEnabled = false
            proguardFiles  += file('proguard-rules.txt')
        }
    }
    android.lintOptions {
        checkReleaseBuilds = false
    }
}

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

您应该将实验性插件用于 NDK 目的:

因此您的 build.gradle(主项目)将如下所示:

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

allprojects {
    repositories {
        jcenter()
    }
}

和 build.gradle(app) 看起来像:

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

model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            applicationId = "com.sample.codecator"
            minSdkVersion.apiLevel = 19
            targetSdkVersion.apiLevel = 19
        }
    }
}

如果您正在使用任何库模块,那么它的 build.gradle 将如下所示:

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

model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            minSdkVersion.apiLevel = 19
            targetSdkVersion.apiLevel = 19
        }
    }
}

您应该将以下内容放在 android{...} 块之外:

android.buildTypes
android.sources
android.productFlavors

赞:

model {

android {
            compileSdkVersion = 23
            buildToolsVersion = "23.0.2"

            defaultConfig.with {
                minSdkVersion.apiLevel = 19
                targetSdkVersion.apiLevel = 19
            }


   android.buildTypes {
    release {
        minifyEnabled = false
        proguardFiles.add(file("proguard-rules.pro"))
         }
     }
    android.productFlavors {
       //
    }

    android.sources {
        //
    }
}

不要忘记在 local.properties 文件中设置 ndk.dir=ndk_path

更新

像这样更新所有文件中的 android.buildTypes 代码:

android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file("proguard-rules.pro"))
        }
    }