0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【備忘録】AndroidStudio(NDK)で作成したAARで、ライブラリのバッファに追加・クリアしてみる

Last updated at Posted at 2021-02-17

目的

NDKで配布用に作成したAARに内部バッファを持たせて、溜め込んで色々と処理する為に追加とクリアをしてみます。

環境

統合環境:AndroidStudio 3.6.1
TargetSDK:26(Android8.0)

AAR(C++部分)

int配列で追加します。
10個までのバッファを持ち、超えたら先頭から消します。
バッファをクリアする関数も作成します。

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

std::vector<int> PintBuff;      // JNI用バッファ

// バッファクリア
extern "C" JNIEXPORT void JNICALL
Java_s_test_jnibufferlib_JNIBufferLib_JNIClear(
        JNIEnv *env, jobject
) {
    PintBuff.clear();
}

// バッファに配列を加える
extern "C" JNIEXPORT jintArray JNICALL
Java_s_test_jnibufferlib_JNIBufferLib_JNIAdd(
        JNIEnv *env, jobject,
        jintArray intArray ) {

    jint intInputSize = env->GetArrayLength(intArray);              // 入力配列数の取得
    jint *intArrayInput = env->GetIntArrayElements(intArray, 0);    // 入力配列の取得

    // 入力値を追加
    for(int i = 0;i < intInputSize; i++){
        PintBuff.push_back(intArrayInput[i]);
    }

    // バッファが10個を超えたら、先頭から消す
    if(PintBuff.size() > 10){
        int intErase = PintBuff.size() - 10;        // 消す数
        PintBuff.erase(PintBuff.begin(), PintBuff.begin() + intErase);
    }

    // バッファを出力する
    int intOutPutSize = static_cast<int>(PintBuff.size());       // 出力する計測値の数
    int intOuntput[intOutPutSize];

    for(int i=0; i<intOutPutSize;i++){
        intOuntput[i] = PintBuff[i];
    }

    jintArray intArrayOutput = env->NewIntArray(intOutPutSize);  // 出力配列
    env->SetIntArrayRegion(intArrayOutput, 0, intOutPutSize, intOuntput);   // 出力をJNI用に変換

    return intArrayOutput;  // intの配列を返す
}

AAR(Java部分)

呼び出し部分だけです。

package s.test.jnibufferlib;

public class JNIBufferLib {
    static {
        System.loadLibrary("jnibufferlib"); // ライブラリ読み込み
    }

    public native int[] JNIAdd(int[] ints); // JNIへ値を入れ、バッファの中身を返す
    public native void JNIClear();          // JNI内のバッファをクリアする
}

AARをimportして使用

作成したAARをインポートし、配列を追加してみます。

package s.test.jnibuffer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.Arrays;

import s.test.jnibufferlib.JNIBufferLib;        // 作成したJNIライブラリの読み込み

public class MainActivity extends AppCompatActivity {

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


        int[] intA = new int[]{ 1, 2, 3, 4};    // グループA
        int[] intB = new int[]{ 5, 6, 7, 8};    // グループB
        int[] intC = new int[]{ 9,10,11,12};    // グループC

        JNIBufferLib libBuffer = new JNIBufferLib();    // JNIライブラリ(最大10個までのバッファ)
        int[] intArr;

        intArr = libBuffer.JNIAdd(intA);        // グループAを追加
        Log.d("Step1", Arrays.toString(intArr));
        intArr = libBuffer.JNIAdd(intB);        // グループBを追加
        Log.d("Step2", Arrays.toString(intArr));
        intArr = libBuffer.JNIAdd(intC);        // グループCを追加(合計12個になるので先頭2個が消える)
        Log.d("Step3", Arrays.toString(intArr));

        libBuffer.JNIClear();   // ライブラリのバッファをクリアする
        intArr = libBuffer.JNIAdd(intC);        // グループCを追加(クリアされているので、グループCのみ表示)
        Log.d("Step4", Arrays.toString(intArr));
    }
}

出力結果

目的通りバッファに追加とクリアが出来ました。

D/Step1: [1, 2, 3, 4]
D/Step2: [1, 2, 3, 4, 5, 6, 7, 8]
D/Step3: [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
D/Step4: [9, 10, 11, 12]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?