我如何prepare我.cpp文件为Android NDK构建它们? [英] How do I prepare my .cpp files for the Android ndk to build them?

查看:161
本文介绍了我如何prepare我.cpp文件为Android NDK构建它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我们有一些图像处理软件,用C ++编写它正由我们的IOS应用程序。我试图将这一图像处理code到我创建使用Android NDK的Andr​​oid项目。

我有我的Andr​​oid项目和SDK的所有设置和准备就绪。我也有NDK设置和准备去。

我在本教程通过如下(这是真棒)和我难倒的一部分,他所定义的$ C $下native.c因为它有这样的函数名,

 无效Java_com_mamlambo_sample_ndk1_AndroidNDK1SampleActivity_helloLog(JNIEnv的* ENV,jobject这个,的jstring logThis)
 

这几乎看起来对我来说,我必须要经过我的所有现有的C ++函数,改变code,以使NDK承认它。

因此​​,这里是我的问题,

  1. 请我一定要改变我现有的C ++ code,以便它与NDK建设者的工作?如果是的话,什么是我需要在我的code来改变对这项工作的事?
  2. 有没有办法有Android.mk文件建立一个完整的目录?我有很多的文件,我不希望有列出其中的每一个人,以让他们建造的。
解决方案

1),你应该能够建立的没有改变,但你需要写一些这些JNI包装函数来调用它从Java。希望你有一个少数顶级类,你只需要总结的。例如。下面是我的游戏我(慢)写:

  // android.cpp
#包括game.h
#包括< jni.h>

命名空间{
    游戏* toGame(jlong​​ gamePtr){
        返回reinter pret_cast<游戏*>(gamePtr);
    }
}

为externC{

    jlong​​ Java_com_rarepebble_game3_Game_create(JNIEnv的* ENV,jobject jobj){
        游戏* G =新游戏();
        返回reinter pret_cast< jlong​​>(七);
    }

    无效Java_com_rarepebble_game3_Game_destroy(JNIEnv的* ENV,jobject jobj,jlong​​ gamePtr){
        删除toGame(gamePtr);
    }

    无效Java_com_rarepebble_game3_Game_update(JNIEnv的* ENV,jobject jobj,jlong​​ gamePtr,jboolean isTouching,jfloat touchX,jfloat敏感){
        toGame(gamePtr) -  GT;更新(isTouching,touchX,敏感);
    }

    无效Java_com_rarepebble_game3_Game_render(JNIEnv的* ENV,jobject jobj,jlong​​ gamePtr){
        toGame(gamePtr) - >渲染();
    }

    // ...和其他几个人。只有一类,虽然。
}
 

在Java方面这让我宣布这些功能在我的 com.rarepebble.game3.Game 类,并呼吁他们在应用程序的生命周期的适当时间。 (请注意如何在Java包,类和函数名称用C对应函数名++):

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

//(进口)

公共类游戏{
    静态{
        的System.loadLibrary(game3lib);
    }
    //这是在C定义的函数++
    私人本地长的创建();
    私人本地无效摧毁(长gamePtr);
    私人本地无效更新(长gamePtr,布尔isTouched,浮法X,浮动Y);
    私人本地无效渲染(长gamePtr);

    私人长gamePtr = 0;

    游戏() {
        gamePtr =创建();
    }

    @覆盖
    保护无效的finalize()抛出的Throwable
        如果(gamePtr!= 0){
            销毁(gamePtr);
        }
        super.finalize();
    }

    // 等等...
}
 

2)可以使用

LOCAL_SRC_FILES:= $(通配符*的.cpp)$(通配符子目录/ * CPP)#etc ...

编辑:按照要求,我的C ++游戏类的头,game.h:

  // game.h
#ifndef的GAME_INCLUDED
#定义GAME_INCLUDED

//各种游戏和标准库包含。
// *否* JNI或Android包含。

命名空间游戏{

    类游戏{
    上市:
        游戏();
        〜游戏();

        无效更新(布尔isTouching,浮touchX,浮敏感);
        无效渲染();

        //其他funcs中...

    私人:
        // 等等...
    };

}

#ENDIF //高清GAME_INCLUDED
 

类似地,game.cpp不包括任何JNI东西要么

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 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.

这篇关于我如何prepare我.cpp文件为Android NDK构建它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆