开发者

How do I prepare my .cpp files for the Android ndk to build them?

开发者 https://www.devze.com 2023-04-10 12:34 出处:网络
We currently have some image processing software written in c++ which is being used by our IOS application. I am trying to integrate this image processing code into the android project that I created

We currently have some image processing software written in c++ which is being used by our IOS application. I am trying to integrate this image processing code into the android project that I created using the Android NDK.

I have my android project and the sdk all setup and ready to go. I also have the ndk setup and ready to go.

I was following through on this tutorial (which is awesome), and I got stumped at the part that he defined the code for native.c because it had a function name like this,

void Java_com_mamlambo_sample_ndk1_AndroidNDK1SampleActivity_helloLog(JNIEnv * env, jobject this, jstring logThis) 

It almost looks to me like I have to go through all of my existing c++ functions and alter the code in order for the NDK to recognize it.

So here are my questions,

  1. Do I have to alter my existing c++ code in order for it to work with the ndk builder? And if so, what are the things I need to change in my code for this work?
  2. Is there a way to have the Android.mk file build an entire directory? I开发者_如何学Go have a lot of files and I did not want to have to list out every single one of them in order to get them built.


1) You should be able to build without alteration, but you will need to write some of those JNI wrapper functions to call it from Java. Hopefully you have a small number of top-level classes and you will only need to wrap those. E.g. Here's what I have for a game I'm (slowly) writing:

// android.cpp
#include "game.h"
#include <jni.h>

namespace {
    Game* toGame(jlong gamePtr) {
        return reinterpret_cast<Game*>(gamePtr);
    }
}

extern "C" {

    jlong Java_com_rarepebble_game3_Game_create(JNIEnv* env, jobject jobj) {
        Game* g = new Game();
        return reinterpret_cast<jlong>(g);
    }

    void Java_com_rarepebble_game3_Game_destroy(JNIEnv* env, jobject jobj, jlong gamePtr) {
        delete toGame(gamePtr);
    }

    void Java_com_rarepebble_game3_Game_update(JNIEnv* env, jobject jobj, jlong gamePtr, jboolean isTouching, jfloat touchX, jfloat touchY) {
        toGame(gamePtr)->update(isTouching, touchX, touchY);
    }

    void Java_com_rarepebble_game3_Game_render(JNIEnv* env, jobject jobj, jlong gamePtr) {
        toGame(gamePtr)->render();
    }

    // ... and a few others. Only one class, though.
}

On the Java side this lets me declare those functions in my com.rarepebble.game3.Game class and call them at the appropriate times in the app's lifecycle. (Note how the Java package, class and function names correspond to the function names in C++):

//Game.java
package com.rarepebble.game3;

// (imports)

public class Game {
    static {
        System.loadLibrary("game3lib");
    }
    // These are the functions you defined in C++
    private native long create();
    private native void destroy(long gamePtr);    
    private native void update(long gamePtr, boolean isTouched, float x, float y);
    private native void render(long gamePtr);

    private long gamePtr = 0;

    Game() {
        gamePtr = create();
    }

    @Override
    protected void finalize() throws Throwable {
        if (gamePtr != 0) {
            destroy(gamePtr);
        }
        super.finalize();
    }

    // etc...
}

2) You can use

LOCAL_SRC_FILES := $(wildcard *.cpp) $(wildcard subdirectory/*.cpp) #etc...

Edit: As requested, my C++ Game class header, "game.h":

// game.h
#ifndef GAME_INCLUDED
#define GAME_INCLUDED

// Various game and standard library includes.
// *NO* JNI or Android includes.

namespace game {

    class Game {
    public:
        Game();
        ~Game();

        void update(bool isTouching, float touchX, float touchY);
        void render();

        // other funcs...

    private:
        // etc...
    };

}

#endif //def GAME_INCLUDED  

Similarly, "game.cpp" doesn't include any JNI stuff either.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号