如何使用Android的创建DLL如何使用、Android、DLL

2023-09-06 13:09:54 作者:Glu TtoNy 沉沦

我是新来的Andr​​oid应用程序development.I要制定一个 DLL 使用机器人。是否有可能开发和集成到Android应用程序。请告诉我的解决方案。如果可能的话,请告诉我解决一个接一个。

I am new to Android application development.I want develop a dll using android. Is it possible to develop and integrate to android app. Please tell me the solution. If it is possible please tell me the solution one by one.

推荐答案

至于我,我曾经做了说明为自己有关NDK。在这里,它是:

As for me I once made a note for myself about NDK. Here it is:

Required applicaitions:
1. Eclipse
2. CDT+Sequoyah plug-ins
3. Android ADT
4. Android NDK


Configuration:
1. Install Eclipse, ADT, CDT and Sequoyah plug-ins
2. In the Eclipse -> Window -> Preferences -> Android -> Native Development put NDK location


Steps:
1. Create new Android Project
2. Create Java class for working with native libraries (NativeLibrary.java)
3. In the class NativeLibrary.java define interface for native methods
4. Right click on Project -> Android Tools -> Add Native Support. Define name of the library.
5. Build the project
6. Go to PROJECT_HOME/bin
7. Create C header file with the command javah -jni <packagename>.NativeLibrary
8. Move this file to PROJECT_HOME/jni folder
9. Implement methods from the header file in the generated cpp file. Do not forget to include the moved header in this file.
10. In java classes create new object of NativeLibrary class and call its methods.
11. Build project.

更新:一步一步没有插件

所需的应用程序 - 这就是你需要开发本地应用程序。在我来说,我使用Eclipse + Android的ADT插件+ Android的CDT插件+塞阔亚插件。你可以将它们安装使用Eclipse - >安装新的软件

Required applications - this is what you need to develop native applications. In my case I use Eclipse + Android ADT plugin + Android CDT plugin + Sequoyah plugin. You can install them using Eclipse - > Install new software

那么你应该下载Android NDK。你也应该导出路径。

Then you should download Android NDK. Also you should export PATH to it.

有关配置:你应该定义唯一路径到你的NDK在Eclipse - >窗口 - > preferences - > Android的 - >本地发展

For the configuration: you should define only path to your NDK in Eclipse -> Window -> Preferences -> Android -> Native Development

您不一定要使用这些插件,但它更容易发展他们。然而,塞阔亚包含错误(或者它有时不正确的配置我的电脑)

之后,你可以创建新的Andr​​oid项目。然后,您可以创建一个定义本地方法的Java类。在我而言,这是NativeLibrary.java。在这里,它是:

After that you can create new Android project. Then you can create java class that defines native methods. In my case this is NativeLibrary.java. Here it is:

package com.testpack.nativetest;

public class NativeLibrary {
public native static int add(int a, int b);

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

之后,建立自己的Andr​​oid项目。之后去到你的bin /类(我不知道,但在此之前它只是bin目录下)目录:

After that build your Android project. After that go to your bin/classes (I don't know but before it was just bin directory) directory:

cd ~/programming/android/workspace/NativeTest/bin/classes

和运行以下命令:

javah -jni com.testpack.nativetest.NativeLibrary

该命令应产生com_testpack_nativetest_NativeLibrary.h头在你的bin / classes目录下。它应该是这样的:

This command should produce com_testpack_nativetest_NativeLibrary.h header in your bin/classes directory. It should look like:

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

#ifndef _Included_com_testpack_nativetest_NativeLibrary
#define _Included_com_testpack_nativetest_NativeLibrary
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_testpack_nativetest_NativeLibrary
 * Method:    add
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_com_testpack_nativetest_NativeLibrary_add
  (JNIEnv *, jclass, jint, jint);

#ifdef __cplusplus
}
#endif

#endif

在你的项目中创建JNI目录并运行以下命令。它将这个头移动到JNI目录。

Create jni directory in your project and Run the following command. It will move this header to jni directory.

mv com_testpack_nativetest_NativeLibrary.h ../../jni

在在JNI目录中创建.c文件。在我的情况下,它是nativ.c,从.h文件中复制函数的定义和生成code:

After that in jni directory create .c file. In my case it is nativ.c, copy the definition of the function from .h file and generate code:

#include "com_testpack_nativetest_NativeLibrary.h"

JNIEXPORT jint JNICALL Java_com_testpack_nativetest_NativeLibrary_add
  (JNIEnv *env, jclass obj, jint a, jint b) {
return a+b;
  }

然后,在JNI目录下,你应该创建一个make文件Android.mk这。简单地更改源(nativ.c)和您的图书馆(NATIV)的名称。

Then in jni directory you should create a make file Android.mk Here it is. Simply change the source (nativ.c) and the name of your library (nativ).

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := nativ
LOCAL_SRC_FILES := nativ.c

include $(BUILD_SHARED_LIBRARY)

转至PROJECT_HOME目录。在我而言,这是

Go to the PROJECT_HOME directory. In my case this is

cd ~/programming/android/workspace/NativeTest

和运行NDK建造。就这样。之后,你可以在你的活动测试:

and run ndk-build. That's all. After that you can test it in your activity:

package com.testpack.nativetest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class NativeTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d("TEST:", "Result 5+4=" + NativeLibrary.add(5, 4));
    }
}

使用插件实在是有点更容易开发。但我认为你应该自己测试它是如何做到这一点。

With plugins it is a bit easier to develop. But I think you should test it by yourself how to do this.