ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android jni demo

2022-07-21 13:32:41  阅读:334  来源: 互联网

标签:jni log androidx demo library android com netconfig


 

第一步:编写kt代码

class NativeLib {

/**
* A native method that is implemented by the 'netconfig' native library,
* which is packaged with this application.
*/
external fun stringFromJNI(): String

companion object {
// Used to load the 'netconfig' library on application startup.
init {
System.loadLibrary("netconfig")
}
}
}

 

第二步:编写C代码

#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL
Java_com_larack_netconfig_NativeLib_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

 

第三步:编写CMakeLists.txt

cmake_minimum_required(VERSION 3.18.1)


project("netconfig")


add_library( # Sets the name of the library.
             netconfig

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             netconfig.cpp )


find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

target_link_libraries( # Specifies the target library.
                       netconfig

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

  

第四步:编写build.gralde配置

 
plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        minSdk 29
        targetSdk 32

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.18.1"
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

  

第五步:调用jni并运行android程序

    value = NativeLib().stringFromJNI()
Hello from C++"

https://gitee.com/Larack/my-config

搜索

复制

标签:jni,log,androidx,demo,library,android,com,netconfig
来源: https://www.cnblogs.com/larack/p/16501583.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有