将应用代码由eclipse导入Android studio的方法NDK-Build和Cmake两种方法(以android_serialport_api为例)

归志宁无五亩园,读书本意在元元。这篇文章主要讲述将应用代码由eclipse导入Android studio的方法NDK-Build和Cmake两种方法(以android_serialport_api为例)相关的知识,希望能为你提供帮助。
网上翻了几百篇博客,看了半天,要不就是写的乱七八糟看不懂,要不就是隐藏了一些细节,要不就是实现不了,最后还是在android官网上看明白了,而且说得有条有理,以后遇到不懂的一定要先翻官网。
参考资料:https://developer.android.com/studio/projects/add-native-code.html?utm_source=android-studio#link-gradle
 
1. 导入eclipse到Android studio 1. 导入工程:
点击如下所示:

将应用代码由eclipse导入Android studio的方法NDK-Build和Cmake两种方法(以android_serialport_api为例)

文章图片

 
导入之后会生成错误和一个文件:
1). 生成的错误:
Gradle \'android-serialport-api-master\' project refresh failedError:Minimum supported Gradle version is 4.1. Current version is 2.14.1. If using the gradle wrapper, try editing the distributionUrl in F:\\AndroidWork\\AndroidStudio\\android-serialport-api-master\\gradle\\wrapper\\gradle-wrapper.properties to gradle-4.1-all.zipConsult IDE log for more details (Help | Show Log)

 
从上面可知是gradle版本太低,需要升级gradle
解决办法:修改gradle-wrapper.properties
 
#Fri Nov 10 19:29:01 CST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\\://services.gradle.org/distributions/gradle-4.1-all.zip//修改为警告对应的版本

 
2). 生成了一个import-summary.txt
 
下面对该文件进行说明
ECLIPSE ANDROID PROJECT IMPORT SUMMARY ======================================Ignored Files://忽略掉的文件 -------------- The following files were *not* copied into the new Gradle project; you should evaluate whether these are still needed in your project and if so manually move them:* README.md * run_emulator.shMoved Files://移除掉的文件 ------------

/*因为安卓gradle工程和eclipse的ADT使用的构建工程的方法不同,所以这里对一些工程目录进行了重构*/ Android Gradle projects use a different directory structure than ADT Eclipse projects. Here\'s how the projects were restructured: /*举例:eclipse目录下的jni\\改为Android studio目录下的ManiMenu\\src\\main\\jni\\
*/ * AndroidManifest.xml => MainMenu\\src\\main\\AndroidManifest.xml * jni\\ => MainMenu\\src\\main\\jni\\ * res\\ => MainMenu\\src\\main\\res\\ * src\\ => MainMenu\\src\\main\\java\\Next Steps://接下啦需要做的事情 -----------
/*需要网络去下载一些依赖的库或者对应的SDK之类的东西*/ You can now build the project. The Gradle project needs network connectivity to download dependencies.Bugs: ----- If for some reason your project does not build, and you determine that it is due to a bug or limitation of the Eclipse to Gradle importer, please file a bug at http://b.android.com with category Component-Tools.(This import summary is for your information only, and can be deleted after import once you are satisfied with the results.)

 
2. 修改build.gradle:
 
因为我们的应用只是用在Android4.2.2上面,对应的SDK版本为17。(看了eclipse上全部都是17,所以应该不用考虑兼容其他版本的应用,照着改)
如果不记得,看看这两篇:
Android studio的gradle
Android系统版本、平台版本、SDK版版本、gradle修改
 
android { compileSdkVersion 17//修改这里,编译版本的SDK buildToolsVersion "27.0.1"defaultConfig { applicationId "android_serialport_api.sample" minSdkVersion 17//修改这里,最小的SDK版本 targetSdkVersion 17//修改这里,目标的SDK版本ndk { moduleName "serial_port" } }buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(\'proguard-android.txt\'), \'proguard-rules.txt\' } } }

 
这个时候Android studio会去网上把对应的SDK下载下来,等待几分钟
 
3. 编译不通过(无法构建本地C++系统)
再次编译,发生错误
Information:Gradle tasks [:MainMenu:assembleDebug] Error:Execution failed for task \':MainMenu:compileDebugNdk\'. > Error: Your project contains C++ files but it is not using a supported native build system.//工程无法使用本地C++构建系统 Consider using CMake or ndk-build integration. For more information, go to: https://d.android.com/r/studio-ui/add-native-code.html Alternatively, you can use the experimental plugin: https://developer.android.com/r/tools/experimental-plugin.html Information:BUILD FAILED in 3s Information:1 error Information:0 warnings Information:See complete output in console

 
这个时候下载三个软件:
将应用代码由eclipse导入Android studio的方法NDK-Build和Cmake两种方法(以android_serialport_api为例)

文章图片

其中:
NDK:通过NDK-build方法来使用本地库
CMake:通过CMake方法来使用本地库
LLDB:用来调试C/C++的工具
 
2. 方法一:将gradle通过NDK-build关联到本地库 1). 方法一: 
注意:NKD-build在Android studio3.0版本以后就不能再用了,必须使用CMke,不过我觉得3.0并不是很好用,比如安装的时候没有选择SDK路径,默认C盘,每次安装完又忘记改,搞得C盘越来越大,换回2.3接着用,还可以使用NDK-build
 
1). 将Android studio查看方法改为Android视图。
2). 选择Link C++ Project with Gradle
将应用代码由eclipse导入Android studio的方法NDK-Build和Cmake两种方法(以android_serialport_api为例)

文章图片

3). 选择ndk-build。
4).  Project Path  旁的字段为您的外部 ndk-build 项目指定Android.mk  脚本文件。
将应用代码由eclipse导入Android studio的方法NDK-Build和Cmake两种方法(以android_serialport_api为例)

文章图片

 
编译OK。完成
 
2). 分析总结:
查看android-serialport-api-master\\MainMenu\\build.gradle
apply plugin: \'com.android.application\'android { compileSdkVersion 17 buildToolsVersion "27.0.1"defaultConfig { applicationId "android_serialport_api.sample" minSdkVersion 17 targetSdkVersion 17ndk { moduleName "serial_port"//这里已经自动添加了 } }buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(\'proguard-android.txt\'), \'proguard-rules.txt\' } } externalNativeBuild { ndkBuild { path \'src/main/jni/Android.mk\' } } }

 
首先查看Android.mk
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)TARGET_PLATFORM := android-3 LOCAL_MODULE:= serial_port//生成的库的名字应该为libserial_port.so LOCAL_SRC_FILES := SerialPort.c LOCAL_LDLIBS:= -lloginclude $(BUILD_SHARED_LIBRARY)

 
查看目录android-serialport-api-master\\MainMenu\\build\\intermediates\\ndkBuild\\debug\\obj\\local:已经生成了相应的库libserial_port.so
将应用代码由eclipse导入Android studio的方法NDK-Build和Cmake两种方法(以android_serialport_api为例)

文章图片
将应用代码由eclipse导入Android studio的方法NDK-Build和Cmake两种方法(以android_serialport_api为例)

文章图片

 
 
 
 
 
虚拟机效果:
将应用代码由eclipse导入Android studio的方法NDK-Build和Cmake两种方法(以android_serialport_api为例)

文章图片

 
3. 方法二:将gradle通过CMake关联到本地库【将应用代码由eclipse导入Android studio的方法NDK-Build和Cmake两种方法(以android_serialport_api为例)】使用的时候忘记保存流程了,懒得再去试一遍,跟NDK-build方法差不多,就是选择方法的时候改一下,具体可以看上面贴的官网,要是以后有时间再写上吧。

    推荐阅读