Android 添加原生支持 - 未解析的 jni.h、android/log.h 等

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12584255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:06:25  来源:igfitidea点击:

Android Add Native support - unresolved jni.h, android/log.h etc

androidcandroid-ndkjava-native-interface

提问by Aristarhys

Before today i use Eclipse 3.8 with Sequoyah pluginfor Android NDK project. But today i decide to freshen Eclipse to Juno release with SDK and NDK.
I was very happy then i see Android Native Toolsin ADT installation, which will do the same job like Sequoyah, but with debug feature.
I created new Android Project to test it.
Adding Android Native Support create jni folder with Android.mk, .cpp file, well same that Sequayah do. Then i get first unresolved to jni.h. I get similar bug with Sequoyah, so i rebuild index and restart Eclipse. After restart it not disappeared.
I go to Paths And Symbolsat C/C++ properties. But there are all that needed built-in includes.
(NDK PATH)/platforms/android-8/arch-arm/usr/include- there are jni.h, log.h etc.
I tryed to add extra dublicate includes to jni.h, clean project, restart, rebuild index, change .ccp to .c, it stays unresolved. I got no errors in error log, but syntax errors in editor on jni functions.

在今天之前,我将 Eclipse 3.8 与Sequoyah 插件用于 Android NDK 项目。但是今天我决定使用 SDK 和 NDK 将 Eclipse 更新为 Juno 版本。
我很高兴看到ADT 安装中的Android Native Tools,它可以像 Sequoyah 一样完成同样的工作,但具有调试功能。
我创建了新的 Android 项目来测试它。
添加 Android Native Support 使用 Android.mk、.cpp 文件创建 jni 文件夹,与 Sequayah 所做的相同。然后我首先无法解析jni.h。我在 Sequoyah 上遇到了类似的错误,所以我重建索引并重新启动 Eclipse。重启后没有消失。
我转到C/C++ 属性中的路径和符号。但是有所有需要的内置包含。
(NDK PATH)/platforms/android-8/arch-arm/usr/include- 有 jni.h、log.h 等。
我尝试向 jni.h、清理项目、重新启动、重建索引添加额外的重复包含,将 .ccp 更改为 .c,它仍未解决。我在错误日志中没有发现错误,但是在 jni 函数的编辑器中出现了语法错误。

NativeLib.java

本地库

package com.aristarhys.glow;

public class NativeLib 
{
private static final String NATIVE_LIB = "glow";
static 
{
    System.loadLibrary(NATIVE_LIB); 
}
  private NativeLib(){};
  public static native void test();
}

glow.h

发光.h

#ifndef GLOW_H_
#define GLOW_H_

#include <jni.h> //unresolved
//syntax error
JNIEXPORT void JNICALL Java_com_aristarhys_glow_NativeLib_test(JNIEnv* env, jclass cls);
#endif /* GLOW_H_ */

log.h

日志文件

#ifndef LOG_H_
#define LOG_H_

#include <android/log.h> //unresolved

#define INFO_TAG "[INFO]"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, INFO_TAG, __VA_ARGS__)

#endif /* LOG_H_ */

glow.c

发光.c

#include "glow.h"
#include "log.h"

//syntax error
JNIEXPORT void JNICALL Java_com_aristarhys_glow_NativeLib_test(JNIEnv* env, jclass cls)
{
LOGI("HI");
}

Android.mk

安卓.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := glow
LOCAL_SRC_FILES := glow.c
LOCAL_LDLIBS    := -llog
include $(BUILD_SHARED_LIBRARY)

回答by ra.

I've tried android-ndk-r8bwith C:\Android\android-ndk-r8bin my PATHvariable. Project compiled without error.
However eclipse "says" that Unresolved inclusion: <jni.h>

我试过的Android NDK,R8BC:\Android\android-ndk-r8b我的PATH变量。项目编译没有错误。
然而日食“说”Unresolved inclusion: <jni.h>

solved:

解决了

NDK Project->New->Folder->Advanced->Link to alternate location(Linked Folder)Browse the path(for example):C:\Android\android-ndk-r8b\platforms\android-8\arch-arm\usr\include

NDK Project->New->Folder->Advanced->Link to alternate location(Linked Folder)浏览路径(例如):C:\Android\android-ndk-r8b\platforms\android-8\arch-arm\usr\include

回答by Sriram Murali

You can do this by choosing Properties for the project

您可以通过为项目选择属性来执行此操作

Properties -> C/C++ General -> Preprocessor Include..-> Entries -> Setting Entries -> CDT User Setting Entries

Properties -> C/C++ General -> Preprocessor Include..-> Entries -> Setting Entries -> CDT User Setting Entries

Add -> Include Directory -> File System Path, and enter the path of the includes

添加->包含目录->文件系统路径,输入包含的路径

ndk/platforms/android-[version]/[arch]/usr/include

回答by Playful Curiosity

I often solving similar issues that is probably occur when you move or rename the working folder of the project.

我经常解决在您移动或重命名项目的工作文件夹时可能发生的类似问题。

  1. No any environment variables required, just pointed NDK location under Preferences > Android > NDK.
  2. Move project outside workspace. Delete from project next files/folders:
  1. 不需要任何环境变量,只需在 Preferences > Android > NDK 下指向 NDK 位置。
  2. 将项目移出工作区。从项目中删除下一个文件/文件夹:

.settings
.classpath
.cproject
.project
project.properties

.settings
.classpath
.cproject
.project
project.properties

  1. Reimport your project. Eclipse > New > Other > Android > Android Project from Existing Code > then point folder with your project, let Eclipse detect it, check "Copy project into workspace" and click Ok/Next, whatever.
  2. Clean project.
  3. Right click on project > Android Tools > Add native support
  4. Rebuild, possibly restart workspace.
  1. 重新导入您的项目。Eclipse > New > Other > Android > Android Project from Existing Code > 然后将文件夹指向您的项目,让 Eclipse 检测它,选中“将项目复制到工作区”并单击“确定”/“下一步”,无论如何。
  2. 清洁项目。
  3. 右键单击项目> Android 工具> 添加本机支持
  4. 重建,可能重新启动工作区。

Also this solved issue with Eclipse 4.3 previously ignored build system and user defined compiler flags. Now macros folding dependent on this flags works fine.

这也解决了 Eclipse 4.3 之前忽略的构建系统和用户定义的编译器标志的问题。现在依赖于这个标志的宏折叠工作正常。

If more general: NDK plugin can properly define for you right includes and anything other required to work fine, but you need to clean your project from broken crap, and easiest way to do so is reimport project.

如果更一般:NDK 插件可以为您正确定义正确的包含和任何其他正常工作所需的内容,但是您需要从破碎的垃圾中清理您的项目,最简单的方法是重新导入项目。

回答by vbmrupp

If you are using Eclipse Kepler the path to add the NDK include

如果您使用的是 Eclipse Kepler,则添加 NDK 的路径包括

Properties->C/C++ General->Paths and Symbols.

属性->C/C++ 常规->路径和符号。

on my version of NDK the include path needed was:

在我的 NDK 版本上,需要的包含路径是:

C:\Program Files (x86)\Android\android-sdk\NDK\android-ndk-r10\platforms\android-L\arch-arm\usr\include.

C:\Program Files (x86)\Android\android-sdk\NDK\android-ndk-r10\platforms\android-L\arch-arm\usr\include。

It will ask you if you want to rebuild. Afterwards the errors go away.

它会询问您是否要重建。之后错误消失。