Using Gradle to compile the C + + code Step Declaration NDK tool class, internally defined native method
Com.coca.firstdemo;
/**
* Created by Administrator on 2016/6/6.
*/
jnishareutils {
Getlogcount (String params) ;
}
Navigate to the project's app folder and invoke the Javah command to generate the. h file:
javah com.coca.firstdemo.JniShareUtils
After execution, the Com_coca_firstdemo_jnishareutils.h file is generated in the same directory as the. java file, with the following content:
/* Don't EDIT this file-it are machine generated */
#include<jni.h>
/ * Header for class Com_coca_firstdemo_jnishareutils */
#ifndef_included_com_coca_firstdemo_jnishareutils
#define_included_com_coca_firstdemo_jnishareutils
#ifdef__cplusplus
extern"C" {
#endif
/*
* Class:com_coca_firstdemo_jnishareutils
* Method:getlogcount
* Signature: (ljava/lang/string;) ljava/lang/string;
*/
JniexportjstringJnicallJava_com_coca_firstdemo_jnishareutils_getlogcount
(jnienv*, Jobject, jstring);
#ifdef__cplusplus
}
#endif
#endif
Create a new JNI directory under the app folder to hold the generated. h file. Create a new. c file with the following name:
# Include "Com_coca_firstdemo_jnishareutils.h"
jniexport jnicall java_com_coca_firstdemo_jnishareutils_getlogcount
(jnienv *env, jobject obj, jstring jstr) {
return (*env)->newstringutf (Env Span style= "color: #6a8759;" > "This just a test for Android Studio NDK JNI developer!" )
}
Add settings to the app's Build.gradle (ABI supported 7 platforms: Armeabi, ARMEABI-V7A, x86, X86_64, MIPS, MIPS64, arm64-v8a
Defaultconfig {
//...
ndk{
"Wangjun_so_jni" //generated so name, the name is arbitrary
"Armeabi" "armeabi-v7a" //output specifies the so library under three ABI architectures. Currently dispensable.
}
}
To add static initialization code in Jnishareutils:
{
System. loadLibrary ("Wangjun_so_jni") ;//corresponds to the modulename defined in the above Build.gradle
}
Why not create a android.mk file? In the app's Build.gradle, hold down CTRL and click the modelname of the NDK code block to jump to the Ndkoptions.java file.
PackageCom.android.build.gradle.internal.dsl;
/**
* DSL object for ndk-related settings.
*/
Public classNdkoptionsImplementsCorendkoptions, Serializable {
private static final longSerialversionuid =1L;
PrivateString ModuleName;
PrivateString CFlags;
PrivateList<string> Ldlibs;
PrivateSet<string> abifilters;
PrivateString STL;
PrivateInteger Jobs;
}
That is, there are 5 parameters that can be set in the NDK code block, instead of the parameter settings in ANDROID.MK. Little tips:1. Hint Error: ERROR:NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin. For details, see Http://tools.android.com/tech-docs/new-build-system/gradle-experimental. Set "Android.usedeprecatedndk=true" in gradle.properties to continue using the current NDK Integration. FIX: Add Android.usedeprecatedndk=true in Gradle.properties
From for notes (Wiz)
Android jni:android Studio uses Gradle to compile the C + + source code