刚开始在网上搜索了网上给的解决办法如下:
Android.mk文件内容:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := tunjni
LOCAL_SRC_FILES := JavaTun.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
.c的头文件
#include <string.h>
#include <jni.h>
#include <android/log.h>`
然而,试过这些还是出现问题“undefined reference to `android_log_print”。后来,看了一份别人成功log的代码,才知道在build.gradle (Module: app)里也要做修改,修改完毕后总算可以了。
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
sourceSets{ main{ jniLibs.srcDirs = ['libs'] } }//change 1
defaultConfig {
applicationId "com.example.sun.tunnel_4over6"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
ndk{//change 2
moduleName "tunjni"
ldLibs "log"
abiFilters "armeabi-v7a"
}
}
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:23.1.1'
}