You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android编译报错:ndk-libs.jar文件不存在,寻求解决方案

解决编译时ndk-libs.jar缺失的问题

Hey, let's tackle this issue step by step. Looking at your problem description and the provided build files, the core issue is that your Gradle tasks are incorrectly placed inside the dependencies block, plus there's a path compatibility problem on Windows. That's why the ndkBuild and ndkLibsToJar tasks aren't running properly, so the jar file never gets generated. Here's how to fix it:

1. Move Gradle Tasks Outside the Dependencies Block

Right now, your ndkBuild, ndkLibsToJar tasks and the JavaCompile dependency hook are all inside the dependencies block—which is invalid Gradle syntax. Tasks need to be defined outside this block so Gradle can recognize and execute them.

Here's the corrected structure for your build.gradle (module:mavlink):

apply plugin: 'com.android.library'
android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName '1.0'
        ndk{
            moduleName 'mavlink_native'
        }
    }
    sourceSets{
        main{
            jni.srcDirs=[]
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

// Move tasks here, outside dependencies
task ndkBuild (type:Exec,description:'compile JNI source via NDK'){
    // We'll fix the path here next
    commandLine environment['NDK_HOME']+'/ndk-build','NDK_PROJECT_PATH=build','APP_BUILD_SCRIPT=src/main/jni/Android.mk', 'NDK_APPLICATION_MK=src/main/jni/Application.mk'
}

task ndkLibsToJar(type:Zip,dependsOn:'ndkBuild',description:'create a jar of the native libs'){
    destinationDir new File(buildDir,'libs')
    baseName 'ndk-libs'
    extension 'jar'
    from(new File(buildDir,'libs')){
        include '**/*.so'
    }
    into'lib/'
}

tasks.withType(JavaCompile){
    compileTask -> compileTask.dependsOn ndkLibsToJar
}

tasks.withType(Exec){
    environment['PATH']=System.getenv('PATH')+':/usr/local/bin'
    def ndkDir = System.getenv('NDK_HOME')
    if (ndkDir == null){
        def localProperties = new Properties()
        localProperties.load(project.rootProject.file('local.properties').newDataInputStream())
        ndkDir = localProperties.getProperty('ndk.dir')
    }
    if (ndkDir == null){
        throw new GradleException('no NDK_SDK directory specified, Add ndk.dir=<PATH_TO_NDK> to local.properties or define NDK_HOME environment variable')
    }
    environment['NDK_HOME']=ndkDir
}

dependencies{
    implementation fileTree(dir:'libs',include:['*.jar'])
    implementation fileTree(dir:"$buildDir/libs",include:'ndk-libs')
    implementation files("$buildDir/libs/ndk-libs.jar")
    implementation project(':threadwrapper')
    implementation project(':connectionlistener')
}

2. Fix NDK Path Compatibility for Windows

Your current ndkBuild task uses / to concatenate paths, which doesn't work on Windows (it uses \ instead). Let's adjust the command line to use system-compatible path handling:

Update the ndkBuild task like this:

task ndkBuild (type:Exec,description:'compile JNI source via NDK'){
    // Use File.separator to adapt to Windows/macOS/Linux path separators
    def ndkBuildPath = new File(environment['NDK_HOME'], 'ndk-build').absolutePath
    // Also use project.file to get the correct absolute path for mk files
    commandLine ndkBuildPath, 'NDK_PROJECT_PATH=build', 
               'APP_BUILD_SCRIPT=' + project.file('src/main/jni/Android.mk').absolutePath, 
               'NDK_APPLICATION_MK=' + project.file('src/main/jni/Application.mk').absolutePath
}

This ensures the path to ndk-build and your makefiles is correctly formatted for Windows.

3. Verify JNI Makefiles Exist

Double-check that you have Android.mk and Application.mk in the src/main/jni directory. If these files are missing, the ndkBuild task will fail, and no .so files (or the final jar) will be generated.

4. Re-Run the Build

After making these changes, follow these steps:

  • Go to Build > Clean Project to wipe old build artifacts
  • Then run Build > Rebuild Project to trigger a fresh build
  • If you still run into issues, open the Terminal in Android Studio and execute gradlew.bat :mavlink:ndkBuild (Windows) to run the NDK task directly—this will show you any specific error messages from the NDK build, which can help diagnose further problems.

内容的提问来源于stack exchange,提问作者Ismail Can Gures

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:16:51