Android-JNI HellWord

Android Studio基本配置:

目录设置
C开发所需

build.gradle:

    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    tasks.withType(JavaCompile) {
        //指定编译JDK版本
        sourceCompatibility = JavaVersion.VERSION_1_7
        targetCompatibility = JavaVersion.VERSION_1_7
    }

    defaultConfig {
        applicationId "chao.jni.com"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            ndk {
                moduleName "_cc_sdk"          //生成的so名字
                //打包进APK的ABI类型 此处决定打包在build-in-ndk目录的文件  用命令在c目录编译将会是所有ABI类型。ndk-build
                abiFilters "armeabi", "armeabi-v7a", "x86", 'x86_64'/*, "mips"*/
            }
        }
        debug {
            ndk {
                ldLibs "log", "z", "m"
                moduleName "_cc_sdk"          //生成的so名字
                //打包进APK的ABI类型 此处决定打包在build-in-ndk目录的文件  用命令在c目录编译将会是所有ABI类型。ndk-build
                abiFilters "armeabi", "armeabi-v7a", "x86", 'x86_64'/*, "mips"*/
            }
        }
    }
    sourceSets {
        main {
            //你的源码目录
            jniLibs.srcDirs 'src/main/libs'//jniLib.srcDirs定义了Gradle在哪里寻找生成的so库文件
            //jni.srcDirs = []   //屏蔽gradle的jni生成过程//禁用自动NDK的构建通话 // 禁用通过Gradle来编译本地c/c++代码
            jni.srcDirs = ['src/main/jni']//指定目录下的c/c++文件编译成指定的模块名.so。
        }
    }
    repositories {
        flatDir {
            dirs 'src/main/libs'
        }
    }
    externalNativeBuild {
//            cmake {
//                cppFlags ""
//                //设置编译工具链
//                arguments "-DANDROID_TOOLCHAIN=clang"
//                //配置CMakeLists.txt的路径
//                path "CMakeLists.txt"
//                //需编译生成的ABI类型
//                abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a'
//            }

//        Link C++ Project with Gradle 右键-目录-配置-上下不能同时设置

        ndkBuild {
            path 'src/main/jni/Android.mk'
        }
    }

gradle.properties:

android.useDeprecatedNdk=true

hello.c:

#include<stdlib.h>
#include<stdio.h>
#include <jni.h>

jstring
Java_chao_jni_com_MainActivity_helloFromC(
        JNIEnv *env,
        jobject thiz) {
    char *cstr = "hello word c";
//    return (*env)->NewStringUTF(env, cstr);
    return (*env)->NewStringUTF(env, cstr);
}

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := _cc_sdk  #要生成的so库的名称,但实际为libhello.so

LOCAL_SRC_FILES := hello.c   #要使用的文件,刚才编写的hello.c文件

include $(BUILD_SHARED_LIBRARY)

application.mk:

#APP_ABI:= all
APP_ABI:= x86 x86_64 armeabi armeabi-v7a
#APP_OPIM :=debug
APP_PLATFORM := android-15 #当前项目支持的最小版本,不写会有警告

MainActivity.java

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("hello");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(helloFromC());
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */

    public native String helloFromC();
}

具体参考:AndroidStudio使用NDK配置方法

发表评论