Android Studio to compile and integrate SO file

0x00 paper target

Let Java code that interacts with Native level code layer

Native code is compiled file is SO

SO files into the final document APK

To do this you need Android Studio NDK and suits directly to the official website to download the Baidu search.

0x01 Java layer

To make Java JNI layer interacts with the layers to a simple test code. hello method used to obtain the string returned from native, and displayed TextView, no TextView their new classmates in a layout file on the line.

package com.example.androidtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private TextView txt_jni;

static {
System.loadLibrary("test");
}

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

txt_jni = findViewById(R.id.txt_jni);
txt_jni.setText(hello(1, "2"));
}

private native String hello(int a, String b);
}

The next layer to achieve JNI code to create a new blank project In Android Studio, File-> New-> Folder-> JNI Folder to create a JNI source folder, the default path in the / src / main / jni.

Bloggers another article demonstrates pure Java files generated SO: https://www.cnblogs.com/DXCyber409/p/10854415.html .

Andrews has likewise two ways: Java_ fixed path mode, and JNI_OnLoad dynamic registration mode.

0x02 JNI fixed prefix wording

Essentially function name and parameters to generate a fixed prefix, implement it in the last c / cpp files.

.H files can be generated manually, not demonstration. As used herein, External Tools Android Studio provide to fulfill a key generated .h header file, which will greatly enhance the development efficiency.

File-> Settings-> Tools-> Extenal Tools, point a plus sign to create a new entry. The command instructions can actually execute the command line parameters passed, combined written handwritten:

D:\RTEws\Java\jdk1.8.0_121\bin>javah -d "E:\Workspace\NetBeans\DXCyber409\src\main\java\dxcyber409\jni" -classpath "E:\Workspace\NetBeans\DXCyber409\target\classes" -jni dxcyber409.Test$Cls

Insert Marcros was built environment variable reference window, do not miss:

Final Program path parameter adjusted as follows:

Name:javah
Program:$JDKPath$\bin\javah.exe
Arguments:-d "$ModuleFileDir$\src\main\jni" -classpath "$OutputPath$;$ModuleSdkPath$\platforms\android-28\android.jar;$ModuleSdkPath$\extras\android\android-support-v4.jar;$ModuleSdkPath$\extras\android\android-support-v7.jar" -jni $FileClass$
Working directory:

Arguments outside the -classpath option in addition to their code compiled classes directory, also joined android.jar, android-support-v4.jar, android-support-v7.jar and other dependencies, without implementation class will be missing and error .

Written more than one, bloggers Arguments have been put in all relevant paths stitching done, so Working directory with nothing left blank on the line.

Ps. Blogger Android SDK seemingly incomplete way, can not find in the Android SDK appcompat dependent packages. If you do not want to re-install the Android SDK, then there is little skill, to lower the Maven Repository lack of Han Han, clothed yourself. Download the throw directory path referenced in the line, the position is not tough requirements.

Then / src / main / jni directory to get the .h header files, routinely create a new cpp file that contains this file shining write on the line, complete code is as follows:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_androidtest_MainActivity */

#ifndef _Included_com_example_androidtest_MainActivity
#define _Included_com_example_androidtest_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
#undef com_example_androidtest_MainActivity_BIND_ABOVE_CLIENT
#define com_example_androidtest_MainActivity_BIND_ABOVE_CLIENT 8L
#undef com_example_androidtest_MainActivity_BIND_ADJUST_WITH_ACTIVITY
#define com_example_androidtest_MainActivity_BIND_ADJUST_WITH_ACTIVITY 128L
#undef com_example_androidtest_MainActivity_BIND_ALLOW_OOM_MANAGEMENT
#define com_example_androidtest_MainActivity_BIND_ALLOW_OOM_MANAGEMENT 16L
#undef com_example_androidtest_MainActivity_BIND_AUTO_CREATE
#define com_example_androidtest_MainActivity_BIND_AUTO_CREATE 1L
#undef com_example_androidtest_MainActivity_BIND_DEBUG_UNBIND
#define com_example_androidtest_MainActivity_BIND_DEBUG_UNBIND 2L
#undef com_example_androidtest_MainActivity_BIND_EXTERNAL_SERVICE
#define com_example_androidtest_MainActivity_BIND_EXTERNAL_SERVICE -2147483648L
#undef com_example_androidtest_MainActivity_BIND_IMPORTANT
#define com_example_androidtest_MainActivity_BIND_IMPORTANT 64L
#undef com_example_androidtest_MainActivity_BIND_NOT_FOREGROUND
#define com_example_androidtest_MainActivity_BIND_NOT_FOREGROUND 4L
#undef com_example_androidtest_MainActivity_BIND_WAIVE_PRIORITY
#define com_example_androidtest_MainActivity_BIND_WAIVE_PRIORITY 32L
#undef com_example_androidtest_MainActivity_CONTEXT_IGNORE_SECURITY
#define com_example_androidtest_MainActivity_CONTEXT_IGNORE_SECURITY 2L
#undef com_example_androidtest_MainActivity_CONTEXT_INCLUDE_CODE
#define com_example_androidtest_MainActivity_CONTEXT_INCLUDE_CODE 1L
#undef com_example_androidtest_MainActivity_CONTEXT_RESTRICTED
#define com_example_androidtest_MainActivity_CONTEXT_RESTRICTED 4L
#undef com_example_androidtest_MainActivity_MODE_APPEND
#define com_example_androidtest_MainActivity_MODE_APPEND 32768L
#undef com_example_androidtest_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING
#define com_example_androidtest_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING 8L
#undef com_example_androidtest_MainActivity_MODE_MULTI_PROCESS
#define com_example_androidtest_MainActivity_MODE_MULTI_PROCESS 4L
#undef com_example_androidtest_MainActivity_MODE_NO_LOCALIZED_COLLATORS
#define com_example_androidtest_MainActivity_MODE_NO_LOCALIZED_COLLATORS 16L
#undef com_example_androidtest_MainActivity_MODE_PRIVATE
#define com_example_androidtest_MainActivity_MODE_PRIVATE 0L
#undef com_example_androidtest_MainActivity_MODE_WORLD_READABLE
#define com_example_androidtest_MainActivity_MODE_WORLD_READABLE 1L
#undef com_example_androidtest_MainActivity_MODE_WORLD_WRITEABLE
#define com_example_androidtest_MainActivity_MODE_WORLD_WRITEABLE 2L
#undef com_example_androidtest_MainActivity_RECEIVER_VISIBLE_TO_INSTANT_APPS
#define com_example_androidtest_MainActivity_RECEIVER_VISIBLE_TO_INSTANT_APPS 1L
#undef com_example_androidtest_MainActivity_DEFAULT_KEYS_DIALER
#define com_example_androidtest_MainActivity_DEFAULT_KEYS_DIALER 1L
#undef com_example_androidtest_MainActivity_DEFAULT_KEYS_DISABLE
#define com_example_androidtest_MainActivity_DEFAULT_KEYS_DISABLE 0L
#undef com_example_androidtest_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL
#define com_example_androidtest_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L
#undef com_example_androidtest_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL
#define com_example_androidtest_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L
#undef com_example_androidtest_MainActivity_DEFAULT_KEYS_SHORTCUT
#define com_example_androidtest_MainActivity_DEFAULT_KEYS_SHORTCUT 2L
#undef com_example_androidtest_MainActivity_RESULT_CANCELED
#define com_example_androidtest_MainActivity_RESULT_CANCELED 0L
#undef com_example_androidtest_MainActivity_RESULT_FIRST_USER
#define com_example_androidtest_MainActivity_RESULT_FIRST_USER 1L
#undef com_example_androidtest_MainActivity_RESULT_OK
#define com_example_androidtest_MainActivity_RESULT_OK -1L
#undef com_example_androidtest_MainActivity_HONEYCOMB
#define com_example_androidtest_MainActivity_HONEYCOMB 11L
#undef com_example_androidtest_MainActivity_MSG_REALLY_STOPPED
#define com_example_androidtest_MainActivity_MSG_REALLY_STOPPED 1L
#undef com_example_androidtest_MainActivity_MSG_RESUME_PENDING
#define com_example_androidtest_MainActivity_MSG_RESUME_PENDING 2L
/*
 * Class:     com_example_androidtest_MainActivity
 * Method:    hello
 * Signature: (ILjava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_androidtest_MainActivity_hello
  (JNIEnv *, jobject, jint, jstring);

#ifdef __cplusplus
}
#endif
#endif

 

 

0x03 JNI动态注册写法

本质是向RegisterNatives

 

0x04 编译SO文件

 

0x05 集成SO文件

 

0x06 测试结果

 

NDK官方指定使用Makefile来进行SO文件编译,那么新建一个文件重命名Android.mk。

目前最重要的是编译出一个SO成品来,一切配置最简单化。后续有详细需要各个编译选项的可以参考官方文档:https://developer.android.com/ndk/guides/android_mk.html?hl=zh-cn

 

参考源

https://www.jianshu.com/p/faa3eebbd401

Guess you like

Origin www.cnblogs.com/DXCyber409/p/10992309.html