博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Studio2 2 配置NDK
阅读量:3950 次
发布时间:2019-05-24

本文共 6393 字,大约阅读时间需要 21 分钟。

分享一下我老师大神的人工智能教程!零基础,通俗易懂!

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       

Android Studio2.2 配置NDK

 

本文博客链接:,作者:jdh,转载请注明.

环境

  • 主机:WIN10
  • 开发环境:Android Studio2.2 Preview 3

步骤

  • 安装NDK
    打开Tools->Android->SDK Manager->SDK Tools选中LLDB和NDK,点击确认,软件会自动安装NDK。
  • 配置环境变量

    • 增加一项:NDK_ROOT,如:C:\soft\adt-bundle-windows-x86-20130911\sdk\ndk-bundle
    • 在path中增加%NDK_ROOT%
  • 在main中新建文件夹jni

  • 新建hello-jni.c
    函数需按照规则命名:Java_包名类名方法名
/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "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. * */#include 
#include
/* This is a trivial JNI example where we use a native method * to return a new VM String. See the corresponding Java source * file located at: * *   apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java *///jstring//Java_com_bazhangkeji_MainActivity_stringFromJNI( JNIEnv* env,//                                                  jobject thiz )//{
//    return (*env)->NewStringUTF(env, "Hello from JNI !");//}JNIEXPORT jstring JNICALLJava_com_bazhangkeji_demo01_MainActivity_stringFromJNI(JNIEnv *env, jobject instance) {
//    // TODO//////    return (*env)->NewStringUTF(env, returnValue);    return (*env)->NewStringUTF(env, "Hello from JNI !");}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  • 新建Android.mk
# Copyright (C) 2009 The Android Open Source Project## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at##      http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "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.#LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := hello-jniLOCAL_SRC_FILES := hello-jni.cinclude $(BUILD_SHARED_LIBRARY)  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  • 在build.gradle中配置
    配置好,make project即可生成.so文件在app\build\intermediates\ndk-build\debug\lib中。
    增加语句:
externalNativeBuild {
        ndkBuild {
            path file("src\\main\\jni\\Android.mk")        }    }
1
2
3
4
5
6
7

完整的代码:

apply plugin: 'com.android.application'android {    compileSdkVersion 24    buildToolsVersion "24.0.0"    defaultConfig {        applicationId "com.bazhangkeji.demo01"        minSdkVersion 15        targetSdkVersion 24        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"//        ndk {
//            moduleName "libspeex"//            cFlags "-std=c++11 -fexceptions"//            ldLibs "log"//            stl "gnustl_shared"//            abiFilter "armeabi-v7a"//        }    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }    externalNativeBuild {        ndkBuild {            path file("src\\main\\jni\\Android.mk")        }    }}dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    compile 'com.android.support:appcompat-v7:24.0.0'    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha3'    compile 'com.android.support:design:24.0.0'    testCompile 'junit:junit:4.12'    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'    androidTestCompile 'com.android.support.test:runner:0.5'    androidTestCompile 'com.androd.support:support-annotations:24.0.0'}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  • 在java层调用
public class MainActivity extends AppCompatActivity {
    private static final int RECORDER_SAMPLERATE = 8000;    private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;    private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;    private AudioRecord recorder = null;    private Thread recordingThread = null;    private boolean isRecording = false;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);        setSupportActionBar(toolbar);        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);        fab.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)                        .setAction("Action", null).show();            }        });        setButtonHandlers();        enableButtons(false);        int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,                RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);        System.out.println(stringFromJNI());    }    public native String stringFromJNI();    public native String unimplementedStringFromJNI();    static {        System.loadLibrary("hello-jni");    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
           

给我老师的人工智能教程打call!

这里写图片描述
你可能感兴趣的文章
DataGridView或 DataTable导出到excel
查看>>
Ilist To DataTable
查看>>
SQL @@IDENTITY, IDENT_CURRENT,SCOPE_IDENTITY
查看>>
簡單工廠模式
查看>>
SQL Server的數據類型
查看>>
php的正则表达式 '/\b\w…
查看>>
ThinkPHP的标签制作及标签调用解析…
查看>>
thrift的lua实现
查看>>
编写高性能的Lua代码
查看>>
Python正则表达式指南
查看>>
LUA--thrift--lib库的创建生成
查看>>
Shell开启扩展模式匹配shopt -s extglob
查看>>
浅谈 URI 及其转义
查看>>
nginx 优化
查看>>
openresty+lua在反向代理服务中的玩法
查看>>
ClickHouse集群搭建从0到1
查看>>
nginx实现请求的负载均衡 + keepalived实现nginx的高可用
查看>>
linux shell 中数组的定义和for循环遍历的方法
查看>>
求1!+2!+3!....+20!(java代码)
查看>>
VMware安装Ubuntu系统无法选择语言
查看>>