Android項(xiàng)目JIN配置

image.png

新建java類

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

package com.lango.mp3;

/**
 * @author: zhongjian
 * Create on 2023/4/6 16:51
 * @des: 音頻轉(zhuǎn)碼
 */
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}  實(shí)際當(dāng)前module 但是有些AS 會(huì)有問題
#設(shè)置生成的so動(dòng)態(tài)庫最后輸出的路徑
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI})


#設(shè)置項(xiàng)目名稱
project(demo)

#add_executable(demo demo.cpp) # 生成可執(zhí)行文件
#add_library(common STATIC util.cpp) # 生成靜態(tài)庫
#add_library(common SHARED util.cpp) # 生成動(dòng)態(tài)庫或共享庫
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
        )

#設(shè)置包含的目錄
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.
# 設(shè)置 target 需要鏈接的庫 本地的庫跟第三方的庫.這里就是把 mp3_encode庫和log庫關(guān)聯(lián)起來.
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 {
                //配置參數(shù)
                cppFlags ""
                //設(shè)置生成指定 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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容