android - 未配置 NDK。使用 SDK 管理器下载它。首选 NDK 版本为 '21.0.6113669'

标签 android gradle android-ndk android-gradle-plugin build.gradle

我已经安装了 NDK 版本“21.0.6113669”,但是当我尝试使用 Gradle Files 同步项目时,出现以下错误:

NDK not configured. Download it with SDK manager. Preferred NDK version is '21.0.6113669'. 
Update NDK version to 21.0.6113669 and sync project
enter image description here
请在我的 build.gradle 下面找到
buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.0'
    }
}

plugins {
  id "com.android.application"
}

android {
    compileSdkVersion project.properties.compileSdkVersion.toInteger()
    ndkVersion '21.0.6113669'
        
    dependencies {
        implementation "androidx.annotation:annotation:1.1.0"
        implementation "androidx.viewpager:viewpager:1.0.0"
        implementation "androidx.drawerlayout:drawerlayout:1.1.0"
        implementation project(":terminal-view")
    }

    defaultConfig {
        applicationId "com.termux"
        minSdkVersion project.properties.minSdkVersion.toInteger()
        targetSdkVersion project.properties.targetSdkVersion.toInteger()
        versionCode 101
        versionName "0.101"

        externalNativeBuild {
            ndkBuild {
                cFlags "-std=c11", "-Wall", "-Wextra", "-Werror", "-Os", "-fno-stack-protector", "-Wl,--gc-sections"
            }
        }

        ndk {
            abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
        }

    }

    signingConfigs {
        debug {
            storeFile file('dev_keystore.jks')
            keyAlias 'alias'
            storePassword 'xrj45yWGLbsO7W0v'
            keyPassword 'xrj45yWGLbsO7W0v'
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            signingConfig signingConfigs.debug
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    externalNativeBuild {
        ndkBuild {
            path "src/main/cpp/Android.mk"
        }
    }

    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
    buildToolsVersion '27.0.3'
}

dependencies {
    testImplementation 'junit:junit:4.13'
    testImplementation 'org.robolectric:robolectric:4.3.1'
}

task versionName {
  doLast {
    print android.defaultConfig.versionName
  }
}

def downloadBootstrap(String arch, String expectedChecksum, int version) {
    def digest = java.security.MessageDigest.getInstance("SHA-256")

    def localUrl = "src/main/cpp/bootstrap-" + arch + ".zip"
    def file = new File(projectDir, localUrl)
    if (file.exists()) {
        def buffer = new byte[8192]
        def input = new FileInputStream(file)
        while (true) {
            def readBytes = input.read(buffer)
            if (readBytes < 0) break
            digest.update(buffer, 0, readBytes)
        }
        def checksum = new BigInteger(1, digest.digest()).toString(16)
        if (checksum == expectedChecksum) {
            return
        } else {
            logger.quiet("Deleting old local file with wrong hash: " + localUrl)
            file.delete()
        }
    }

    def remoteUrl = "https://bintray.com/termux/bootstrap/download_file?file_path=bootstrap-" + arch + "-v" + version + ".zip"
    logger.quiet("Downloading " + remoteUrl + " ...")

    file.parentFile.mkdirs()
    def out = new BufferedOutputStream(new FileOutputStream(file))

    def connection = new URL(remoteUrl).openConnection()
    connection.setInstanceFollowRedirects(true)
    def digestStream = new java.security.DigestInputStream(connection.inputStream, digest)
    out << digestStream
    out.close()

    def checksum = new BigInteger(1, digest.digest()).toString(16)
    if (checksum != expectedChecksum) {
        file.delete()
        //throw new GradleException("Wrong checksum for " + remoteUrl + ": expected: " + expectedChecksum + ", actual: " + checksum)
        throw new FileNotFoundException("Wrong checksum for " + remoteUrl + ": expected: " + expectedChecksum + ", actual: " + checksum)
    }
}

clean {
    doLast {
        def tree = fileTree(new File(projectDir, 'src/main/cpp'))
        tree.include 'bootstrap-*.zip'
        tree.each { it.delete() }
    }
}

task downloadBootstraps(){
    doLast {
        def version = 30
        downloadBootstrap("aarch64", "7a90034285c614d23fa450547a5e2aec77d4242c9891ad662bf0c6fd3bd7ef4e", version)
        downloadBootstrap("arm",     "f030869ce9a43f84d88560d7ac5153ee4f7e517bca0b37ab01df3e1acba0fe37", version)
        downloadBootstrap("i686",    "1ea9b63f21602231140d58a5545cfbc6bc2ded56ef2b3c31cba2759d913eef00", version)
        downloadBootstrap("x86_64",  "a50eb8a4dd02b7898bbd4a9653a25c14b56c1737409ce7f64110fd33c2c69382", version)
    }
}

//buildscript {
//    dependencies {
//        classpath 'com.android.tools.build:gradle:4.0.0'
//    }
//}

afterEvaluate {
  android.applicationVariants.all { variant ->
    variant.javaCompileProvider.get().dependsOn(downloadBootstraps)
  }
}
我已从笔记本电脑中删除 .gradle 文件夹并重新安装,还尝试了 Android Studio 中的 Invalidate Caches/Restart 选项,但问题仍未解决。
任何建议都将受到欢迎。

最佳答案

这是build.gradleAndroid目录!你必须添加

android {
    ndkVersion '21.3.6528147'
...
build.gradleAndroid/app并删除 ndk version ...来自 Android/build.gradle
我的简单build.gradleAndroid/app代码 :
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdkVersion 30
    ndkVersion '21.0.6113669'
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
        checkReleaseBuilds false
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.test.test"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

关于android - 未配置 NDK。使用 SDK 管理器下载它。首选 NDK 版本为 '21.0.6113669',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64137526/

相关文章:

android - 如何捕捉图像并将其保存在SD卡中

java - 如何记录 Gradle 构建任务的结果文件

android - 如何配置具有自己依赖的外部库?

c++ - 如何测试这个C库?

android - 如何使用 Android 的 NDK 来使用 mkfifo

Android-NDK:从 native 代码创建 GUI 元素

Android 谷歌 API Java 客户端 com.google.api.client.auth.oauth2.draft10

java - 从 3.0.1 更新到 3.1 后,Android Studio 未启动

安卓计费库

JAVA 8没有jrunscript,只有jjs