Online over hundreds of blog, read a half-day, or is to write a mess to see or not understand, or is hidden some details, or can not be realized, and finally in the Android official online to see, and said orderly, after encountering do not understand must first turn the official website.
Reference: Https://developer.android.com/studio/projects/add-native-code.html?utm_source=android-studio#link-gradle
1. Import Eclipse to Android Studio1. Import Project:
Click as follows:
An error and a file are generated after the import:
1). Generated error:
' Android-serialport-api-master ' is4.1is2.14. 1 using Try inch F:\AndroidWork\AndroidStudio\android-serialport-api-master\gradle\wrapper\gradle-wrapper.properties to gradle- 4.1- for more details (Help | Show Log)
From the above, the Gradle version is too low and needs to be upgraded Gradle
Workaround: Modify gradle-wrapper.properties
#Fri Nov 19:29:01 CST 2017distributionbase=gradle_user_homedistributionpath=wrapper/distszipstorebase=gradle_ user_homezipstorepath=wrapper/distsdistributionurl=https\://services.gradle.org/distributions/ Gradle-4.1-all. zip //Modify to a warning corresponding to the version
2). Generates a import-summary.txt
The file is described below
ECLIPSE ANDROID PROJECT IMPORT SUMMARY======================================ignored files: //Missing file --------------The following files were*not*copied into the new Gradle project; youshould evaluate whether these is still neededinchYour project andifSo manually move them:*readme.md* Run_emulator.SHMoved Files: //File removed ------------
/* Because the Android Gradle project and the Eclipse ADT use different methods of building engineering, some engineering catalogs have been reconstructed */Android Gradle projects using a different Directory structure than adteclipse projects. here's how the projects were restructured: /* Example: jni\ in Eclipse directory changed to manimenu\src\main\jni\ under Android Studio directory
*/* Androidmanifest.xml =Mainmenu\src\main\androidmanifest.xml* Jni\ =Mainmenu\src\main\jni* Res\ =Mainmenu\src\main\res* Src\ =mainmenu\src\main\javanext Steps: //Take down the things you need to do -----------
/* Need a network to download some dependent libraries or the corresponding SDK or something like * * Youcan now build the project. The Gradle project needs networkconnectivity to download dependencies. Bugs:-----If forSome reason your project does not build, and you determine thatit are due to a bug or limitation of the Eclipse to Gra Dle importer,pleasefileA bug at http://b.android.com with categorycomponent-Tools. (This import summary is foryour information only, and can is deletedafter import once you is satisfied with the results.)
2. Modify Build.gradle:
Because our application is only used on Android4.2.2, the corresponding SDK version is 17. (See Eclipse All is 17, so should not consider compatible with other version of the application, according to change)
If you don't remember, look at these two articles:
Android Studio's Gradle
Android system version, platform version, SDK version, Gradle modification
Android { compilesdkversion //Modified here, compiled version of SDK buildtoolsversion "27.0.1" defaultconfig { ApplicationID "Android_serialport_api.sample" minsdkversion //Modify here, the smallest SDK version Targetsdkversion //Modify here, Target SDK version ndk { modulename "Serial_port" } } Buildtypes { Release { minifyenabled false proguardfiles getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.txt '}}
This time Android studio will go online to download the corresponding SDK, wait a few minutes
3. Compilation does not pass (unable to build local C + + system)
Compile again, error occurred
Information:gradle tasks [: Mainmenu:assembledebug]error:execution failed for task ': Mainmenu:compiledebugndk ' .> Error:your Project contains C + + files but it's not using a supported native build system. //project cannot build system using local 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, can Use the experimental plugin: Https://developer.android.com/r/tools/experimental-plugin.htmlInformation:BUILD FAILED in 3sinformation:1 errorinformation:0 warningsinformation:see complete output in console
Download three software at this time:
which
NDK: Using the Ndk-build method to use a local library
CMake: Using the local library with the CMake method
LLDB: A tool for debugging C + +
2. Method One: Associate Gradle via Ndk-build to the local library
Note: Nkd-build can no longer be used after the Android studio3.0 version, you must use Cmke, But I think 3.0 is not very easy to use, such as the installation of the time did not choose the SDK path, the default C-drive, each time the installation and forget to change, make C disk more and more big, replace 2.3 and then use, but also using Ndk-build
1). Change the Android Studio view method to Android view.
2). Select Link C + + Project with Gradle
3). Select Ndk-build.
4). The field next to project Path Specifies the android.mk script file for your external Ndk-build project.
Compile OK. Complete
View Android-serialport-api-master\mainmenu\Build.gradle
Apply plugin: ' Com.android.application ' android { compilesdkversion buildtoolsversion "27.0.1" defaultconfig { ApplicationID "android_serialport_api.sample" minsdkversion targetsdkversion 17 NDK { modulename "serial_port" //Here has been automatically added } } buildtypes { Release { minifyenabled false proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' proguard-rules.txt ' } } Externalnativebuild { ndkbuild { path ' src/main/jni/android.mk '}} }
Virtual machine Effects:
3. Method Two: Associate Gradle via CMake to the local library
Use the time to forget to save the process, too lazy to try again, with the Ndk-build method is similar, is the choice of methods when the change, the specific can see the above affixed to the official website, if there is time to write it later.
Methods for importing app code from eclipse into Android studio Ndk-build and CMake two methods (take Android_serialport_api as an example)