a person who has not used Java and Android to develop the Android application use case with C + + so library using the hello-jni of the NDK (for example), for those unfamiliar with Java and Android, it takes time and hopefully this article helps people like me, welcome to reprint:
1. Download install Android SDK and NDK,NDK R10 (currently up-to-date) can be compiled independently of C + + without Cygwin.
2. Install Android Studio.
3. Compile the Hello-jni in sample with the Ndk-build command to generate the so library.
4. Create a new project in Android Studio and copy all the resulting so files together with the processor folder into the project's libs file. Modify the Build.gradle file to add Sourcesets, as follows:
Apply plugin: ' Com.android.application 'Android {compilesdkversion21stbuildtoolsversion"21.1.2"Defaultconfig {ApplicationID"Com.sample.hello"minsdkversion15targetsdkversion21stVersioncode1Versionname"1.0"} buildtypes {release {minifyenabledfalseproguardfiles Getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.pro '}} sourcesets {main {jnilibs.srcdirs = [' Libs ' ]}}}dependencies {compile filetree (dir:' Libs ', include: [' *.jar ']) Compile' com.android.support:appcompat-v7:21.0.3 '}
5. Add Java file Hellojni file (Note package):Basic copy of the Hellojni file in sample,The code is as follows (This code is slightly different, I give Stringfromjni string parameter, you can refer to the Hellojni file in the NDK sample/src ):
/** Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License "); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable or agreed to writing, software * Distributed under the License is distribute D on ' As is ' BASIS, * without warranties or CONDITIONS of any KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ PackageCom.example.hellojni; Public classhellojni{/*A native method that's implemented by the * ' hello-jni ' native library, which are packaged * with this applic ation. */ Public nativestring Stringfromjni (string msg); /*This is another native method declaration, which is *not* * implemented by ' Hello-jni '. This was simply to show that * you can declare as many native methods in your Java code * as you want, their implem entation is searched in the * currently loaded native libraries only the first time * you are call them. * * Trying to call this function would result in a * Java.lang.UnsatisfiedLinkError exception! */ Public nativeString Unimplementedstringfromjni (); /*This was used to load the ' Hello-jni ' Library on application * startup. The library has already been unpacked to */data/data/com.example.hellojni/lib/libhello-jni.so at * installation Time by the package manager. */ Static{system.loadlibrary ("Hello-jni"); }}
6. Call the so library function Stringfromjni in the project and add the reference first:
import com.example.hellojni.HelloJni;
Call code again:
Finaledittext EditText ==newhellojni (); Edittext.settext (Hj.stringfromjni ("This is java!"));
7. Packaging and Publishing:
8. Other:NDK does not support C99 (C + +) By default, if C99 is required, modify the Android.mk file, add local_cflags: =-std=c99
Local_cflags: =-std=c99
Android Studio 1.0 Development NDK calls C + + so library