Android JNI stick career .a -> .so (MK mode) (android studio)

g### Reason
I once dreamed of walking across the world with a sword and seeing the prosperity of the world. Unexpectedly, as soon as I entered JNI, it was as deep as the sea, and since then the goddess has been a passerby. Generating so files is the ultimate dream of every JNI person. A few days ago, there were few people in the company, and the cpp implementation files were thrown over. Hehe, I thought I was going to develop in the direction of c scum, so I was so scared that I quickly picked up the red book, C++ from entry to abandonment. A few months have passed, the book is still not popular, cpp is gone, replaced by .a files, what the hell? Take out Du Niang and swipe binggo angrily. With Shadowsocks in one hand, the valley dog ​​barks. Static libraries, in simple terms, are recognized by compilers, but not by linux. The story starts here.

How to use .a

Use it as much as you want, and loudly. I just quietly, quietly watching. One slap, one footprint, slap, does it hurt? No pain, easy three-step walk.

Pop. Positioning

Prescribing the medicine must be right to the mouth, and the location cannot be arbitrary. Come, come, under the jni (JNI Floder) directory. Back to the original starting point, the location of your cpp file in memory (for an advertisement, refer to the article about JNI I wrote earlier). Yes, that's right, it's the jni directory again. The advanced .a is still the same place, with a familiar taste and a different formula. Of course, no pic no bb. Nah~*@……#! #! Holy Summoning — Tututu.
JNI directory map
The sources directory above is blindly written and classified into categories. Here is something to explain, such as arm64-v8a, this refers to the cpu type. At present, armeabi is the base class, which is supported by all mobile phones. There are so many mobile phones, what is the CPU type of Tianya? no, no, nobody I want nobody nobody But You, one word: lazy. so armeabi, you deserve it. x86/x86-64 is generally a computer cpu type, and the simulator can be used. It would be good if each type can be built, after all, each type of cpu has its own optimization.

slap. link

Here comes the point. The process of using .a is linking. Of course not only, the whole process of packaging into so is a linking process. Here we will talk about how to link .a.
mk was born from the moment jni was born. For the time being, let’s talk about the mk method to realize the link, and don’t mention the others for the time being. With a long history and a scripted configuration, it carries the distant places and poems of the older generation. Left hand, right hand, one slow motion. One step, two steps, three or four steps, take you through a cutscene. Remember, it's gratifying, if not, just read it again.
mk version structure diagram

# Android.mk文件,稍作修改,即可食用
LOCAL_PATH := $(call my-dir)

//链接.a块
include $(CLEAR_VARS)
LOCAL_MODULE := libHello
LOCAL_C_INCLUDES := $(LOCAL_PATH)/sources
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/sources
LOCAL_SRC_FILES := sources/$(TARGET_ARCH_ABI)/libHello.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_CPPFLAGS  := -std=c++14
LDFLAGS := -shared
LOCAL_LDFLAGS += -fuse-ld=bfd
LOCAL_MODULE           := JniDemo
LOCAL_SRC_FILES        :=   jni_dynamic.cpp
LOCAL_STATIC_LIBRARIES := libHello
include $(BUILD_SHARED_LIBRARY)
# Application.mk 这个基本不需要改
APP_STL := gnustl_static//使用 STL 静态库
NDK_TOOLCHAIN_VERSION := 4.9//链接器tool版本
/**
 *fexceptions 允许异常功能
 *frtti 运行时类型识别
 *fpermissive 此项有效时表示宽松的编译形式,比如没有用到的代码中有错误也可以通过
 * std=c++14 允许使用c++14的函数等功能
 */
APP_CPPFLAGS := -fexceptions -frtti -fpermissive -std=c++14
APP_ABI := armeabi armeabi-v7a arm64-v8a mips mips64
APP_PLATFORM := android-14
APP_OPTIM := debug

There is nothing special to explain about these two fixed collocations, just copy the bricks. If you hit your own foot, with a speed of fire of 120, it's useless for you to come to me. The mk file is already in place, and of course the support of build.gradle is needed.

android{
    ....
    task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
        commandLine "E:/adt-bundle-windows-x86_64-20140321/android-ndk-r10/ndk-build.cmd",//ndk.build位置
                'NDK_PROJECT_PATH=build/intermediates/ndk',//ndk位置
                'NDK_LIBS_OUT=src/main/jniLibs',//输出目录
                'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
                'NDK_APPLICATION_MK=src/main/jni/Application.mk'
    }
    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }
}

This paragraph, if you don’t explain it, you can’t justify it. To explain, in a word, customize the ndkbuild command (a powerful tool for generating so). At this point, I thought that the journey has come to an end, and it is time to witness the moment of miracles. Unexpectedly, however, an error made me intoxicated, with no reservations and no energy left to grieve:

D:\Study_Code\JniDemo\app\src\main\jni\sources\hello.h
Error:(1, 18) string: No such file or directory
Error:Execution failed for task ':app:compileDebugNdk'.
> com.android.ide.common.process.ProcessException: Error while executing 'E:\adt-bundle-windows-x86_64-20140321\android-ndk-r10\ndk-build.cmd' with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=D:\Study_Code\JniDemo\app\build\intermediates\ndk\debug\Android.mk APP_PLATFORM=android-25 NDK_OUT=D:\Study_Code\JniDemo\app\build\intermediates\ndk\debug\obj NDK_LIBS_OUT=D:\Study_Code\JniDemo\app\build\intermediates\ndk\debug\lib APP_ABI=all}

Obviously, string cannot be found in c++. Yo yo yo, Chuck makes trouble. Bug, bug, bug, startled a puddle of bitter water. Now that customization is enabled, some restrictions need to be made, such as disabling automatic execution of ndk-build. If the ndk-build is executed automatically, then my Application.mk configuration will be invalid. The default is the c environment, which is why.

android{
    ...
    sourceSets {
        main {
            jni.srcDirs = []//禁用自动执行ndk-build 
        }
    }
}

.mk This small boat has landed here.

Papapa. The moment to witness the miracle

The above has been done, then at this moment, the miracle that belongs to you has finally come. biu, biu, build. Next, perhaps even more exciting.
A github ticket is attached to take you to experience the vastness of the world

Guess you like

Origin blog.csdn.net/youyi300200/article/details/73016872