Giter Site home page Giter Site logo

ndkexample's Introduction

#NDK开发 ##NDK----examplefirst(HelloWord) ####开发配置

  • NDK配置,如图:
  • 成功标志,如图:
  • 在gradle.properties文件中添加代码:android.useDeprecatedNdk=true

####step1 新建一个类JniUtils,里面代码如下:

public class JniUtils {
    //访问C++代码
    public static native String sayHello();
}

MainActivity里面代码如下:

public class MainActivity extends AppCompatActivity {
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.test);
        textView.setText(JniUtils.sayHello());
    }

}

写完之后点击rebuild,然后你会在build的文件夹下发现多了一个类,然后打开as的终端,进入debug目录,输入 javah -jni com.example.examplefirst.JniUtils (后面的路径根据自己的来写)回车运行,你会发现又多了一个.h的文件,如图. ####step2 新建jni文件,如图: 在jni文件下把step1生成的.h文件剪切过来,如图:

并新建一个.cpp文件,我的文件名叫SayHello,里面代码如下:

//把copy的.h文件引进来
#include "com_example_examplefirst_JniUtils.h"
//在实现自己写的C++代码
//jstring 对应String 类型
//这里的代码可以从.h文件里面拷过来,只不过加了参数和return 
JNIEXPORT jstring JNICALL Java_com_example_examplefirst_JniUtils_sayHello
  (JNIEnv * env, jclass o){
  return env->NewStringUTF("sayHello");
  };

####step3 在build.gradle的defaultConfig下面添加如下代码:

    ndk {
            moduleName "jniModule"
            abiFilters "armeabi", "armeabi-v7a", "x86"//支持的平台
        }

在JniUtils类里面加入代码:

  static {
        System.loadLibrary("jniModule");
    }

如图:

####step4 效果:

这里真的有代码 ##NDK----hellojni这个项目是我从NDk自带例子项目里导出来的

ndkexample's People

Contributors

mar-sir avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.