欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

[Android jni开发系列]AndroidStudio jni开发常见问题

程序员文章站 2022-06-22 19:43:30
前面总结过两种方式进行Android ndk开发,今天再介绍一种利用AndroidStudio进行ndk开发的方式,和系列二中的方式差不多,不过不需要编译Android.mk和Ap...

前面总结过两种方式进行Android ndk开发,今天再介绍一种利用AndroidStudio进行ndk开发的方式,和系列二中的方式差不多,不过不需要编译Android.mk和Application.mk,更为简单。

利用《[Android jni开发系列(二)]AndroidStudio移植eclipse NDK项目开发》中配置的javah命令,生成包含native方法java类j对应jni头文件,编写对应的c文件,不清楚地方可查看前一篇文章

此时直接编译会出现下面错误:

Error:Execution failed for task ':app:compileDebugNdk'.
> Error: Your project contains C++ files but it is not using a supported native build system.
Consider using CMake or ndk-build integration with the stable Android Gradle plugin:
 https://developer.android.com/studio/projects/add-native-code.html
or use the experimental plugin:
 https://developer.android.com/studio/build/experimental-plugin.html.

解决方法:在gradle.properties添加如下行

android.useDeprecatedNdk=true

此时就可以直接编译使用了,此时生成的so库名称为libapp.so,如需修改so名称,需要在build.gradle中配置,如下:

    defaultConfig {
        applicationId "com.jason.jni.demo1"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        ndk {
            moduleName "ndk_test"
        }
    }

另外需要注意的是,如果在ndk下配置了abiFilters,还需要考虑当前ndk版本是否支持,否则编译时候会出现下面错误:

Android NDK: The armeabi ABI is no longer supported. Use armeabi-v7a.    
Android NDK: NDK Application 'local' targets unknown ABI(s): armeabi    
Android NDK: Please fix the APP_ABI definition in C:/Users/yinjb/AppData/Local/Android/Sdk/ndk-bundle/build//../build/core/default-application.mk    


C:/Users/yinjb/AppData/Local/Android/Sdk/ndk-bundle/build//../build/core/setup-app.mk:79: *** Android NDK: Aborting    .  Stop.


:app:compileDebugNdk FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugNdk'.
> com.android.ide.common.process.ProcessException: Error while executing process C:\Users\yinjb\AppData\Local\Android\Sdk\ndk-bundle\ndk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=F:\github\JniDemo1\app\build\intermediates\ndk\debug\Android.mk APP_PLATFORM=android-26 NDK_OUT=F:\github\JniDemo1\app\build\intermediates\ndk\debug\obj NDK_LIBS_OUT=F:\github\JniDemo1\app\build\intermediates\ndk\debug\lib APP_ABI=armeabi-v7a,armeabi,x86}

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

完整的build.gradle如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.jason.jni.demo1"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        ndk {
            moduleName "ndk_test"
                       ldLibs "log", "z", "m"
           abiFilters "armeabi", "armeabi-v7a", "x86"  //ndk 版本不能用最新的,本人测试android-ndk-r15c可以,其它的没试
//           abiFilters "armeabi-v7a", "x86" //ndk版本17.0.4640043 rc1不支持armeabi,否则编译会出现上面错误
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

三种方式的jni开发demo下载