【问题标题】:Could not find method ndk() for arguments找不到参数的方法 ndk()
【发布时间】:2017-03-22 23:15:36
【问题描述】:

我正在关注Create Hello-JNI With Android Studio

MAC OX 10.11.5

Android Studio 2.2 稳定版

java 版本:1.7.0_79

gradle-2.14.1

这是我的 app.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    defaultConfig {
        applicationId "com.chenql.helloandroidjni"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    ndk {
        moduleName "hello-android-jni"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:design:23.3.0'
    testCompile 'junit:junit:4.12'
}

这是错误: The Error Message

Error:(20, 0) Could not find method ndk() for arguments [build_13jh6qtzl4f08f8c1of3mvsys$_run_closure1$_closure5@5b127949] on project ':app' of type org.gradle.api.Project.

打开文件

【问题讨论】:

  • 我还在 MainActivity 类的末尾添加了 JNI 函数 getMsgFromJni() 和 System.loadLibrary()。

标签: android-ndk


【解决方案1】:

原来是这段代码

ndk {
    moduleName "hello-android-jni"
    }

应该放在“defaultConfig”块下:

defaultConfig {
    applicationId "com.chenql.helloandroidjni"
    minSdkVersion 22
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    ndk {
        moduleName "hello-android-jni"
    }
}

而不是在“buildTypes”块之后。

【讨论】:

【解决方案2】:

升级到更新版本的 Gradle 插件解决了

【讨论】:

    【解决方案3】:

    您只需在android/app/build.gradle中添加以下代码

    android {
        ndkVersion rootProject.ext.ndkVersion //<-- add line
        compileSdkVersion rootProject.ext.compileSdkVersion
    

    android/build.gradle

    buildscript {
        ext {
          buildToolsVersion = "29.0.3"
            minSdkVersion = 21
            compileSdkVersion = 31
            targetSdkVersion = 30
            ndkVersion = "22.1.7171670 || {{use your ndk_version}}" //<-- add line
    

    就是这样。重建应用程序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 2018-05-24
      • 2021-11-08
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多