之前一直在使用eclipse调试项目,一直没时间去研究android studio ,今天正好有时间就踩了一下坑。
AS的安装以及相关下载就不在此记录了,直接从项目导入开始。
3.6还没有支持AS,所以在frameworks/runtime-src 下只能使用在eclipse下的proj.android工程导入,打开AS选择倒数第二项Import project去导入3.6项目目录中的proj.android文件夹
接下来连接真机,点击RUN
第一个坑出现
Error:Execution failed for task ':appActivity: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:
http://tools.android.com/tech-docs/new-build-system/gradle-experimental.
根据错误提示发现没有在AS配置NDK
解决方案:点击File->Project Structure打开窗口
在最下方设置你的NDK,就可以填平第一个坑了
接下来继续执行RUN,遇到第二个坑
Error:Execution failed for task ':appActivity: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:
http://tools.android.com/tech-docs/new-build-system/gradle-experimental.
根据提示不难猜测,是由于C++的文件关联的问题产生的,谷歌以后找到了具体的原因,是由于之前在eclipse下是自动通过android.mk编译jni工程,而现在在AS下是生成一个临时文件去做c/c++代码的编译链接,所以会产生诸如路径以及参数等等的错误,所以必须通过修改配置,去停止自动通过android.mk编译jni工程,而是手动引用,具体方案是在项目的build.gradle 下增加
sourceSets.main { jniLibs.srcDir 'src/main/libs' jni.srcDirs = [] }
至此就可以运行到真机上进行调试了
具体详解可参考
http://blog.k-res.net/archives/1788.html
https://stackoverflow.com/questions/21096819/jni-and-gradle-in-android-studio