一、Debug调试效果
二、配置JNI调试环境
2.1 配置build.gradle
只需配置android标签节点下的buildTypes
buildTypes{
debug{
jniDebuggable true
jniDebuggable = true
}
}
2.2 配置AndroidManifest属性
android:debuggable="true"
<application android:label="@string/app_name"
android:debuggable="true">
<activity android:name=".HelloJni"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
2.3 配置Run/Debug configurations
- 选择run下拉item,选Edit Configurations,弹出配置窗体
- 点击➕新建 configuraionts,选android native条目
- 配置下native执行的名称如:hellojniNative.
- 选择真机或模拟器调试.
三、code
源码取的是ndk目录下hellojni demo,可以自行去获取
3.1 java code
public native String stringFromJNI();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Create a TextView and set its content.
* the text is retrieved by calling a native
* function.
*/
final TextView tv = new TextView(this);
tv.setText("点击调用方法");
tv.setTextSize(100);
tv.setClickable(true);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// start(tv);
tv.setText(stringFromJNI());
}
});
setContentView(tv);
}
3.2 natvie code
jstringJava_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject this )
{
#if defined(__arm__)
#if defined(__ARM_ARCH_7A__)
#if defined(__ARM_NEON__)
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a/NEON (hard-float)"
#else
#define ABI "armeabi-v7a/NEON"
#endif
#else
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a (hard-float)"
#else
#define ABI "armeabi-v7a"
#endif
#endif
#else
#define ABI "armeabi"
#endif#elif defined(__i386__)
#define ABI "x86"#elif defined(__x86_64__)
#define ABI "x86_64"#elif defined(__mips64)
/* mips64el-* toolchain defines __mips__ too */
#define ABI "mips64"#elif defined(__mips__)
#define ABI "mips"#elif defined(__aarch64__)
#define ABI "arm64-v8a"#else
#define ABI "unknown"#endif
return (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI " ABI ".");
}