Android Studio JNI 接口生成
- 在src/下创建java 头文件
2.1
(1)利用Android Studio的Terminal,进入你自己的Android工程文件的app/src/main目录
在Terminal中输入命令
cd app\src\main
这样就进入到了main目录,可以使用dir命令查看当前目录有哪些文件
在Terminal中输入命令
javah -d jni -jni -classpath ….\build\intermediates\classes\debug com.example.lenovo.application.JniClass(不带class后缀)
解释:
-d 输出目录,后面跟上要生成的目录名
-jni 生成Jni样式的标头文件
-classpath 指定加载类的路径,后面跟上你要生成头文件的这个类的路径,例如:
……\build\intermediates\classes\debug(这个是类所在的路径)
com...*.JniClass(类的包名)
注意,要生成.class文件,要Make Project
如果前面没有出错的话,就能看到jni文件和生成的头文件
(2)其实jni目录可以不用命令生成,Android Studio已经为我们准备好了。
然后在Terminal中进入到该目录,输入命令
cd app\src\main\jni
在执行如下命令
javah -d jni -jni -classpath ….\build\intermediates\classes\debug com.example.lenovo.application.JniClass
(3)
利用Windows的cmd生成jni头文件
1.进入到你自己Android工程目录下的app\build\intermediates\classes\debug
2.然后Shift加鼠标右键,点击红色方框的内容
3.出现cmd命令行,输入如下命令
javah -jni com.example.lenovo.application.JniClass
4.会在当前目录生成*.h头文件,把它拷贝进jni目录就可以了。
- gradle
- `import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: ‘com.android.application’
android {
compileSdkVersion 26
buildToolsVersion “27.0.3”
defaultConfig {
applicationId “airmovegesture.com.socketjnitest”
minSdkVersion 17
targetSdkVersion 26
versionCode 1
versionName “1.0”
testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”
}
//
// ndk {
// moduleName “socket_native” //生成的so名字
// abiFilters “armeabi”, “armeabi-v7a”, “arm64-v8a” //输出指定三种abi体系结构下的so库。
// }
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets{
main{
jni.srcDirs=[]
jniLibs.srcDirs=['src/main/jniLibs']
}
}
task ndkBuild(type: org.gradle.api.tasks.Exec) {
workingDir file('src/main')
commandLine getNdkBuildCmd()
}
// task ndkBuild(type: Exec, description: ‘Compile JNI source via NDK’) {
// commandLine “C:/Users/Administrator/Android/sdk/ndk-bundle/build/ndk-build.cmd”,
// ‘NDK_PROJECT_PATH=build/intermediates/ndk’,
// ‘NDK_LIBS_OUT=src/main/libs’,
// ‘APP_BUILD_SCRIPT=src/main/jni/Android.mk’,
// ‘NDK_APPLICATION_MK=src/main/jni/Application.mk’
// }
task copyThirdso(type: Copy) {
from file('src/main/jni/thirdso/libs')
into file('src/main/jniLibs')
}
task copyJniLibs(type: Copy) {
from fileTree('src/main/libs/')
into file('src/main/jniLibs')
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn 'ndkBuild','copyThirdso','copyJniLibs'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
task cleanNative(type: Exec) {
workingDir file('src/main')
commandLine getNdkBuildCmd(), 'clean'
}
clean.dependsOn cleanNative
}
dependencies {
compile fileTree(dir: ‘libs’, include: [‘*.jar’])
androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.2.2’, {
exclude group: ‘com.android.support’, module: ‘support-annotations’
})
compile ‘com.android.support:appcompat-v7:26.+’
compile ‘com.android.support.constraint:constraint-layout:1.0.2’
testCompile ‘junit:junit:4.12’
}
def getNdkDir() {
if (System.env.ANDROID_NDK_ROOT != null)
return System.env.ANDROID_NDK_ROOT
Properties properties = new Properties()
properties.load(project.rootProject.file(‘local.properties’)
.newDataInputStream())
def ndkdir = properties.getProperty(‘ndk.dir’, null)
if (ndkdir == null)
throw new GradleException(“NDK location not found. Define location with ndk.dir in the local.properties file or with an ANDROID_NDK_ROOT environment variable.”)
return ndkdir
}
def getNdkBuildCmd() {
def ndkbuild = getNdkDir() + “/ndk-build”
if (Os.isFamily(Os.FAMILY_WINDOWS))
ndkbuild += “.cmd”
return ndkbuild
}
def SDK_BASENAME = “socket_native”;
def SDK_VERSION = “_V1.0.1”;
def sdkDestinationPath = “build/libs”;
def srcClassDir = [project.buildDir.absolutePath + “/intermediates/classes/release”];
task deleteBuild(type: Delete) {
delete sdkDestinationPath + SDK_BASENAME + SDK_VERSION + “.jar”
}
task makeJar(type: Jar) {
//from zipTree(zipFile)
classifier = “release”
extension = “jar”
from srcClassDir
//from fileTree(dir: ‘src/main’, includes: [‘assets/**’])
exclude(‘airmovegesture/com/socketjnitest/MainActivity.class’)
//exclude(‘com/zienon/airmove/CustomCmdExecuter.class’)
//exclude(‘com/zienon/airmove/CustomCmdMapper.class’)
//exclude ‘/com/zienon/airmove/view’
exclude ‘**/R.class’
exclude ‘*/R$.class’
exclude ‘/android’
baseName = SDK_BASENAME + SDK_VERSION
destinationDir = file(sdkDestinationPath)
//configuration ‘proguard-rules.pro’
}
makeJar.dependsOn(deleteBuild, build)
推荐阅读
-
Android Studio开发之 JNI 篇的简单示例
-
[build.gradle配置系列(一)]android studio根据版本号动态生成apk名
-
android studio 3.4配置Android -jni 开发基础
-
Android Studio 超级简单的打包生成apk的方法
-
Android Studio中导入JNI生成的.so库的实现方法
-
Android studio生成javadoc文件的教程
-
Android Studio修改Apk打包生成名称
-
Android Studio 一键生成Json实体类教程
-
android studio中怎么使用JNI之静态库?
-
Android studio无法创建类和接口问题解决办法。提示 Unable to parse template "Class"