First use Android Stuido to create an app project, create a project, the. Gradle directory structure is 2.8 directories. (The note:2.10 directory appears for subsequent update results.) In turn, modify the above red Box Callout section: 1) Modify the contents of the Gradle/wrapper/gradle-wrapper.properties directory: 2) Modify the project directory under the Build.gradle, mainly to modify the Gradle plug-in this part of the content: 3) Modify the App/build_gradle content: Pre-Modified original content:apply plugin: ' Com.android.application ' android { compileSdkVersion 24 buildToolsVersion "24.0.1" defaultConfig { applicationId "Com.example.walkera.serialndkdemo" minSdkVersion 14 targetsdkversion 24 versioncode 1 versionName "1.0" } buildtypes { release { minifyEnabled false proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro ' } }} Dependencies { compile filetree (dir: ' Libs ', include: [' *.jar ' ]) testCompile ' junit:junit:4.12 ' compile ' com.android.support:appcompat-v7:24.1.1 '} Modified content:
Apply plugin: ' Com.android.model.application '
Model {
Android {
Compilesdkversion = 22
Buildtoolsversion = "22.0.1"
Defaultconfig.with {
ApplicationID = "Com.example.walkera.serialndkdemo"
Minsdkversion.apilevel = 7
Targetsdkversion.apilevel = 22
}
}
ANDROID.NDK {
ModuleName = "SerialPort"
Ldlibs.addall (["Android", "Log"])
/*
* Other NDK flags configurable here is
* Cppflags.add ("-fno-rtti")
* Cppflags.add ("-fno-exceptions")
* Ldlibs.addall (["Android", "Log"])
* STL = "System"
*/
}
Android.buildtypes {
Release {
Minifyenabled = False
Proguardfiles.add (File (' Proguard-rules.txt '))
}
}
android.productflavors {
Create ("arm") {
Ndk.abiFilters.add ("Armeabi")
}
Create ("ARM7") {
Ndk.abiFilters.add ("armeabi-v7a")
}
Create ("ARM8") {
Ndk.abiFilters.add ("arm64-v8a")
}
Create ("x86") {
Ndk.abiFilters.add ("x86")
}
Create ("x86-64") {
Ndk.abiFilters.add ("x86_64")
}
Create ("MIPS") {
Ndk.abiFilters.add ("MIPS")
}
Create ("mips-64") {
Ndk.abiFilters.add ("Mips64")
}
To include all CPU architectures, leaves abifilters empty
Create ("All")
}
}
dependencies {
Compile Filetree (dir: ' Libs ', include: [' *.jar '])
Compile ' com.android.support:appcompat-v7:22.1.1 '
}
Above this is the complete configuration file that I compiled successfully, I use the Android studio version is 1.5;
The above process is all the project configuration content, for the source part of the content:
First, in the package directory, create a JNI Java file that invokes the. c file, which is primarily called the content of so files generated by C + + files, the JNI API file:
Next we can compile the whole project directly. (Note: The above JNI was created only after compilation, and at this point there was no C + + file added, no JNI directory)
After the compilation succeeds, the Javah-jni COM.EXAMPLE.WALKERA.SERIALNDKDEMO.SERIALPORTNDK command is run with the terminal of as or the terminal that comes with the window .
Note that this command is fastidious:
1) First, this command executes the directory, must be the project directory under the app\build\intermediates\classes\all\debug\ directory
2) COM.EXAMPLE.WALKERA.SERIALNDKDEMO.SERIALPORTNDK This name, the first half must be the package name [Com.example.walkera.serialndkdemo], The following serialportndk is the name of the class that invokes the follow-C + + library.
After the Javah command runs successfully, a. h header file is generated. And then we're going to create a JNI directory under the src/main/directory, and create a source file for C + +, which is the JNI native we need to develop. Also copy the. h header file to the JNI directory and build and implement the JNI native method that you need based on the. h header file. After the completion, I carried out the whole project compilation, compile no error, according to the internet, I look for so file generation, many articles are introduced in the Libs directory, but I always find, I think I have problems in engineering, which step set error, but compile and normal, strange. Later colleagues let me run a test, the results can be normal operation, it is strange, I did not generate? So I found out: in the new studio version, the intermediate library so file generation, mainly look at the table of contents: the whole process. Author:lihaiping1603email: [Email protected]originating from: http://www.cnblogs.com/lihaiping/p/5829264.html
(formerly) using the Android Studio NDK development process