美文网首页
Android项目JIN配置

Android项目JIN配置

作者: 戎码虫 | 来源:发表于2023-04-05 19:42 被阅读0次
image.png

新建java类

新建Mp3Encoder.java类,按提示新增c方法

package com.lango.mp3;

/**
 * @author: zhongjian
 * Create on 2023/4/6 16:51
 * @des: 音频转码
 */
class Mp3Encoder {
   public native void encode();
}

添加cpp文件夹

main目录下新增cpp文件夹,新增C/C++文件

//
// Created by zhongjian on 2023/4/6.
//
#include <jni.h>
#include <android/log.h>

#define  LOG_TAG  "test"
#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define LOGD(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)

extern "C"
JNIEXPORT void JNICALL
Java_com_lango_mp3_Mp3Encoder_encode(JNIEnv *env, jobject thiz) {
    LOGE("encoder encode");
}

新增CMakeList.txt文件

Android Studio 2.2之后支持CMakeList.txt编译

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.
#指定 cmake 的最小版本
cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

#// ${PROJECT_SOURCE_DIR}  实际当前module 但是有些AS 会有问题
#设置生成的so动态库最后输出的路径
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI})


#设置项目名称
project(demo)

#add_executable(demo demo.cpp) # 生成可执行文件
#add_library(common STATIC util.cpp) # 生成静态库
#add_library(common SHARED util.cpp) # 生成动态库或共享库
add_library( # Sets the name of the library.
        mp3_encode

        # Sets the library as a shared library.
        SHARED

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

#设置包含的目录
include_directories(src/main/cpp)

add_definitions(-DNO_CRYPTO)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
#查找指定的库文件
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)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
# 设置 target 需要链接的库 本地的库跟第三方的库.这里就是把 mp3_encode库和log库关联起来.
target_link_libraries( # Specifies the target library.
        mp3_encode

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

修改app/build.gradle

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.lango.mp3"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        //新增
        externalNativeBuild {
            cmake {
                //配置参数
                cppFlags ""
                //设置生成指定 ABI 版本的 so 库
                abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildFeatures {
        viewBinding true
    }

    //新增
    externalNativeBuild {
        cmake {
            //配置文件路径
            path "src/main/cpp/CMakeLists.txt"
        }
    }
}

生成 so

image.png
image.png

如果Android Studio不支持指定目录,则在该目录下找到

image.png

相关文章

网友评论

      本文标题:Android项目JIN配置

      本文链接:https://www.haomeiwen.com/subject/vwxyddtx.html