[Android jni開発シリーズ(3)]Android Studio jni開発でよくある質問

22050 ワード

先に2つの方式をまとめてAndroid ndk開発を行いましたが、今日はAndroid Studioを利用してndk開発を行う方式を紹介します.シリーズ2の方式と差は多くありませんが、Androidをコンパイルする必要はありません.MkとApplication.mk、もっと簡単です.
『[Android jni開発シリーズ(二)]Android Studio移植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
dk-bundle
dk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=F:\github\JniDemo1\app\build\intermediates
dk\debug\Android.mk APP_PLATFORM=android-26 NDK_OUT=F:\github\JniDemo1\app\build\intermediates
dk\debug\obj NDK_LIBS_OUT=F:\github\JniDemo1\app\build\intermediates
dk\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'
}

また、ndkプロジェクトをコンパイルする場合、コンパイルエラーが発生し、具体的なコンパイルエラーが表示されない場合は、Android StudioのTerminalウィンドウでgradlewコマンドを入力してプロジェクトをコンパイルし、具体的なエラーを表示することができます.
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:compileDebugNdk'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
    at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)
    at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:60)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:97)
    at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:87)
    at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:626)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:581)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.gradle.internal.UncheckedException: com.android.ide.common.process.ProcessException: Error while executing process D:\adt-bundle-windows-x86_64-20140702\android-ndk-r15c
dk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=F:\github\AndroidSerialPortApi\app\build\intermediates
dk\debug\Android.mk APP_PLATFORM=android-26 NDK_OUT=F:\github\AndroidSerialPortApi\app\build\intermediates
dk\debug\obj NDK_LIBS_OUT=F:\github\AndroidSerialPortApi\app\build\intermediates
dk\debug\lib APP_ABI=armeabi-v7a,x86} at org.gradle.internal.UncheckedException.throwAsUncheckedException(UncheckedException.java:63) at org.gradle.internal.UncheckedException.throwAsUncheckedException(UncheckedException.java:40) at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:76) at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:46) at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39) at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:121) at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336) at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328) at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199) at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:110) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92) ... 32 more Caused by: com.android.ide.common.process.ProcessException: Error while executing process D:\adt-bundle-windows-x86_64-20140702\android-ndk-r15c
dk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=F:\github\AndroidSerialPortApi\app\build\intermediates
dk\debug\Android.mk APP_PLATFORM=android-26 NDK_OUT=F:\github\AndroidSerialPortApi\app\build\intermediates
dk\debug\obj NDK_LIBS_OUT=F:\github\AndroidSerialPortApi\app\build\intermediates
dk\debug\lib APP_ABI=armeabi-v7a,x86} at com.android.build.gradle.internal.process.GradleProcessResult.buildProcessException(GradleProcessResult.java:74) at com.android.build.gradle.internal.process.GradleProcessResult.assertNormalExitValue(GradleProcessResult.java:49) at com.android.build.gradle.tasks.NdkCompile.runNdkBuild(NdkCompile.java:397) at com.android.build.gradle.tasks.NdkCompile.taskAction(NdkCompile.java:272) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73) ... 42 more Caused by: org.gradle.process.internal.ExecException: Process 'command 'D:\adt-bundle-windows-x86_64-20140702\android-ndk-r15c
dk-build.cmd'' finished with non-zero exit value 2 at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:382) at com.android.build.gradle.internal.process.GradleProcessResult.assertNormalExitValue(GradleProcessResult.java:47) ... 49 more
//windows
gradlew.bat build -d
//linux
gradlew build -d

3つの方法のjni開発demoダウンロード