关于Linux配置JavaAndroid,NDK环境模板和linux配置javahome的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于androidndk(Notoolchainsfou
关于Linux配置Java Android, NDK 环境模板和linux配置javahome的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于android ndk (No toolchains found in the NDK toolchains folder for ABI with prefix...)、Android NDK and OpenCV Development With Android Studio、Android NDK GPIO 操作(pcduino 装 Android 系统)、Android NDK r4 san-angeles问题等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- Linux配置Java Android, NDK 环境模板(linux配置javahome)
- android ndk (No toolchains found in the NDK toolchains folder for ABI with prefix...)
- Android NDK and OpenCV Development With Android Studio
- Android NDK GPIO 操作(pcduino 装 Android 系统)
- Android NDK r4 san-angeles问题
Linux配置Java Android, NDK 环境模板(linux配置javahome)
sudo gedit /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "$PS1" ]; then
if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1=''\h:\w\$ ''
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1=''# ''
else
PS1=''$ ''
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
ANDROID_HOME=/media/sfshine/文档/Linux/android/android-sdk-linux
PATH=$PATH:$ANDROID_HOME/platform-tools
PATH=$PATH:$ANDROID_HOME/tools/
export PATH ANDROID_HOME
NDKROOT="/media/sfshine/文档/Linux/android/android-ndk-r13b"
PATH=$NDKROOT:$PATH
export PATH
JAVA_HOME=/media/sfshine/文档/Linux/android/java/jdk1.8.0_121
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATH
android ndk (No toolchains found in the NDK toolchains folder for ABI with prefix...)
今天刚在 android studio (3.0.1)版本搭建 ndk 环境时遇到如上问题
在 r18b 版本中发现了一段话,大概解释了如上问题原因

Android NDK location 即本地 NDK 路径。
Android NDK and OpenCV Development With Android Studio
---------------- If you do NOT know Chinese, you can just skip this part ----------------
一直打算将原来的XFace进行改进,最近终于有了些时间可以动手了,改进计划如下:开发上使用Android Studio作为新的开发环境,配上新的构建系统Gradle;应用上将修改原来的UI设计,内部代码也将有很大的变化,可能会用上ContentProvider和Service等略高级内容;算法上打算让应用扩展性增强以适应不同的算法,并结合强大的Android Studio和Gradle让这个项目变得更加丰富。说了一堆废话,言归正传,本文的重点是介绍如何在Android Studio中进行NDK开发(目前它还不完全支持NDK开发),难点是NDK中还包含OpenCV的动态库。最后的最后,本文剩下部分将使用英文,因为它要成为我在StackOverflow上的处女答,么么哒 ~O(∩_∩)O~
---------------------------- Here is the right stuff you may need --------------------------------
This post shows how to develop an Android NDK application with OpenCV included using Android Studio and Gradle. If you''re working on migrating your original Eclipse Project to Android Studio, you may find this post is what exactly you want!
OK,Let''s start!
Section 1: Three things you must know
1.Firstly, if you are not familiar with Android Studio and Gradle, you may find these links useful. (if you already know these well, skip this part)
①Creating a new Project with Android Studio
②Building Your Project with Gradle
③Gradle Plugin User Guide or you may want to read a Chinese commented version in my blog here.
2.Secondly, if your android ndk project is not that complicated(for example, having no opencv included), you may wanna see ph0b
''s introduction here, it''s quite a nice job with a video recorded! (you can also follow Section 2 in this post to get a simple Android NDK demo application)
ph0b
''s post: ANDROID STUDIO, GRADLE AND NDK INTEGRATION
3.Thirdly, if those above two do not meet your needs, then I think you may want to customize the Android.mk with Gradle in Android Studio. Thanks to Gaku Ueda
, he had made a great job explaining how to achieve that goal. Actually I have found another nicer solution without adding that many codes and also achieve that goal. :-) Find it out in the next sections.
Gaku Ueda
''s post: Using custom Android.mk with Gradle/Android Studio
OK, I will cover all above and give another nice solution in the end, have fun!
Section 2: A simple Android NDK demo application
This section shows creating a simple Android NDK demo application, if you already know, you can directly go the section 3.
1.Create a new Android project named NDKDemo
with a blank Activity in AS(=Android Studio).
2.Give an id
to the TextView
in activity_my.xml
such as android:id="@+id/textview"
, then add these codes in MyActivity.java
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
TextView textView = (TextView) findViewById(R.id.textview);
textView.setText(hello());
}
static {
System.loadLibrary("hello");
}
public native String hello();
3.Create a new directory jni
in folder app/src/main
, then you have java
, jni
and res
in this folder.
4.This step is very important! You can add a external tool to run the javah
command without typing that much code!
Open AS''s Preferences
, then find External Tools
in IDE Settings
, click +
to add one tool with the following configurations. (Make sure you have add JDK tools
in your system path
, if you don''t know how, click here)
With the help of this tool, each time we right click on a class file
, then choose Android Tools -> javah
to run this tool, it will automatically generate a C head file
for us in the target folder $ModuleFileDir$/src/main/jni
, in this case, it is app/src/main/jni
. Try this on MyActivity.java
file now! The console will print out a log like:
/usr/bin/javah -v -jni -d /Users/hujiawei/AndroidStudioProjects/NDKDemo/app/src/main/jni com.android.hacks.ndkdemo.MyActivity
[Creating file RegularFileObject[/Users/hujiawei/AndroidStudioProjects/NDKDemo/app/src/main/jni/
com_android_hacks_ndkdemo_MyActivity.h]]
Then you get a com_android_hacks_ndkdemo_MyActivity.h
file in jni
folder with the following content.
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_android_hacks_ndkdemo_MyActivity */
#ifndef _Included_com_android_hacks_ndkdemo_MyActivity
#define _Included_com_android_hacks_ndkdemo_MyActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_android_hacks_ndkdemo_MyActivity
* Method: hello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_android_hacks_ndkdemo_MyActivity_hello
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
5.Write a simple C
implementation file named main.c
in jni
folder
#include <jni.h>
#include "com_android_hacks_ndkdemo_MyActivity.h"
JNIEXPORT jstring JNICALL Java_com_android_hacks_ndkdemo_MyActivity_hello
(JNIEnv * env, jobject obj){
return (*env)->NewStringUTF(env, "Hello from JNI");
}
6.In the build.gradle
file under app
module, add the following codes to configure ndk
in defaultConfig
element, here we just give the uni module a name hello
, you can find other configurations in Gradle Plugin User Guide.
defaultConfig {
applicationId "com.android.hacks.ndkdemo"
minSdkVersion 16
targetSdkVersion 20
versionCode 1
versionName "1.0"
ndk{
moduleName "hello"
}
}
7.In order to let Gradle run ndk-build
command (in some task, maybe NdkCompile
task), we should configure the ndk.dir
in local.properties
file in Project root.
sdk.dir=/Volumes/hujiawei/Users/hujiawei/Android/android_sdk
ndk.dir=/Volumes/hujiawei/Users/hujiawei/Android/android_ndk
8.OK, everything is ready, click Run
to give it a try, you will see the result like
All right, so what''s happening inside?
Since you have a jni
folder, Gradle will consider it as a default native code folder. When Gradle builds the app
, it will run ndk-build
command(since you have configured ndk.dir
, Gradle knows where to find it) with a generated Android.mk
file(locates in app/build/intermediates/ndk/debug/Android.mk
), after compiling the native codes, it will generate the libs
and obj
folder into folder app/build/intermediates/ndk/debug/
. Gradle will then package the libs
into final apk
file in folder app/build/outputs/apk/app-debug.apk
(you can unarchive this file to check whether libs
is contained)
app/build/intermediates/ndk/debug
(lib
and obj
folders)
app/build/outputs/apk/app-debug.apk
(and files within it)
Secontion 3: Using OpenCV
If your project do not use OpenCV, then the section 2 is just enough. But what if you wanna use OpenCV to do other stuff? Of course, we want to use OpenCV for Android
instead of JavaCV
here, and Of course, we need to package OpenCV library for Android into our application''s APK file (then users who use this app does not have to install OpenCV Manager
). So, how can we achieve these goals?
The simplest way has been posted by TGMCians
on Stack Overflow here, that is, let the main app include the OpenCV library as a dependency, and copy all <abi>/*.so
files in OpenCV for Android SDK to jniLibs
folder under app/src/main/
, Gradle will automatically package these <abi>/*.so
files into libs
folder within the final APK file. Of course, this method will work, but it has a few backwards: (1) Unless you only copy the needed *.so
files, you will always have a large APK due to this reason; (2) How about the building of the jni
files? How to run ndk-build
if these files contain opencv
related codes?
So, here comes to our Using custom Android.mk with Gradle and Android Studio
part. For testing, we first creat an Android.mk
and an Application.mk
file under jni
folder.
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := main.c
LOCAL_LDLIBS += -llog
LOCAL_MODULE := hello
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_ABI := armeabi
APP_PLATFORM := android-16
Thanks to Gaku Ueda
, he had made a great job explaining how to achieve that goal with this post. The core idea of his method is to run ndk-build
command in some task, then zip the <abi>/*.so
files under the output app/build/libs/
folder into a jar
file which is finally put in app/build/libs/
folder, then add a compile dependency to this jar file. The key code for his method listed below
Notice 1: When using custom Android.mk, we should first disable Gradle to build the jni
folder as before, and sourceSets.main.jni.srcDirs = []
just does this job!
Notice 2: The code is not exactly the same with Gaku Ueda''s code: tasks.withType(Compile)
to tasks.withType(JavaCompile)
, because Compile
is deprecated.
Notice 3: You can get $ndkDir
variable with project.plugins.findPlugin(''com.android.application'').getNdkFolder()
or you can define it in grade.properties
file under Project root, so you need to add ndkDir=path/to/your/ndk
in that file, if the file is not created, simply create a new one.
android{
...
sourceSets.main.jni.srcDirs = []
task ndkBuild(type: Exec, description: ''Compile JNI source via NDK'') {
ndkDir = project.plugins.findPlugin(''com.android.application'').getNdkFolder()
commandLine "$ndkDir/ndk-build",
''NDK_PROJECT_PATH=build'',
''APP_BUILD_SCRIPT=src/main/jni/Android.mk'',
''NDK_APPLICATION_MK=src/main/jni/Application.mk''
}
task ndkLibsToJar(type: Zip, dependsOn: ''ndkBuild'', description: ''Create a JAR of the native libs'') {
destinationDir new File(buildDir, ''libs'')
baseName ''ndk-libs''
extension ''jar''
from(new File(buildDir, ''libs'')) { include ''**/*.so'' }
into ''lib/''
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkLibsToJar
}
...
}
dependencies {
compile fileTree(dir: ''libs'', include: [''*.jar''])
// add begin
compile fileTree(dir: new File(buildDir, ''libs''), include: ''*.jar'')
// add end
}
But we can still do a little improvements here. We have already know that Gradle will take jniLibs
folder as its default native libraries folder, so we can simply output the libs/<abi>/*.so
files generated by ndk-build
command into jniLibs
folder, so there''s no need to zip these *.so
files into a jar
file.
The final build.gradle
file under app
module
apply plugin: ''com.android.application''
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.android.hacks.ndkdemo"
minSdkVersion 16
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
// add begin
sourceSets.main.jni.srcDirs = []
task ndkBuild(type: Exec, description: ''Compile JNI source via NDK'') {
ndkDir = project.plugins.findPlugin(''com.android.application'').getNdkFolder()
commandLine "$ndkDir/ndk-build",
''NDK_PROJECT_PATH=build/intermediates/ndk'',
''NDK_LIBS_OUT=src/main/jniLibs'',
''APP_BUILD_SCRIPT=src/main/jni/Android.mk'',
''NDK_APPLICATION_MK=src/main/jni/Application.mk''
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
// add end
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile(''proguard-android.txt''), ''proguard-rules.pro''
}
}
}
dependencies {
compile fileTree(dir: ''libs'', include: [''*.jar''])
}
So simple, right? ''NDK_LIBS_OUT=src/main/jniLibs''
helps us do the right job!
For testing, you can also add some lines relating with OpenCV in your Android.mk
file and some line in your main.c
to check whether everything is readlly working. For example, add #include <opencv2/core/core.hpp>
in main.c
file, and change Android.mk
to
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#opencv
OPENCVROOT:= /Volumes/hujiawei/Users/hujiawei/Android/opencv_sdk
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk
LOCAL_SRC_FILES := main.c
LOCAL_LDLIBS += -llog
LOCAL_MODULE := hello
include $(BUILD_SHARED_LIBRARY)
In Gradle Console window, you can see these similar lines
*.so
files relating with OpenCV has been packaged into the final APK
One More Thing
Of course, maybe you don''t want to change your build.grale
file with that much code, and Of course, you also don''t want to run ndk-build
outside the IDE, then copy the <abi>/*.so
files into jniLibs
folder each time you want to rebuild the native codes!
At last, I came out another nicer solution, if you like, that is to create a ndk-build
external tool in Android Studio, and every time you want to rebuild the native codes, simply run the external tool, then it automatically generates the libs/<abi>/*.so
files into jniLibs
folder, so everything is ready to run this app, :-)
The configuration is simple
Parameters: NDK_PROJECT_PATH=$ModuleFileDir$/build/intermediates/ndk NDK_LIBS_OUT=$ModuleFileDir$/src/main/jniLibs NDK_APPLICATION_MK=$ModuleFileDir$/src/main/jni/Application.mk APP_BUILD_SCRIPT=$ModuleFileDir$/src/main/jni/Android.mk V=1
OK, I hope it is helpful. Let me know if it is really helpful, or tell me what''s your problem. :-)
Android NDK GPIO 操作(pcduino 装 Android 系统)
开发工具: Eclipse
开发语言: Java 和 c++
Pcduino 跑 Android 系统让我兴奋了很长时间,也由此幻想了很多有趣的应用。例如 结合 Yeelink 做远程加电控制;结合微信开放接口与传感器向好友推送信息等等。 当然这些有趣的应用都离不开 PCDUINO 硬件的操作。那么在 Android 系统下怎么来操作硬件呢?我想大概有以下几种方式:
1、利用 Arduino 库自己写 c++ 程序然后编译成可执行文件,然后把可执行文件拷到 PCDUINO 中,添加权限,在模拟中端里运行可执行文件。
优点:Arduino 库函数完善,非常容易操作。
缺点:貌似和 Android 开发的应用不能完美的结合。
2、利用 Android 的 Jni 编程,直接驱动 GPIO。
优点:开发工具简单,利用 Eclipse 进行交叉编译,同时编译 Java 和 c++ 代码。并且把硬件操作函数生成.so 文件直接打包 APK 中。
下面我们利用 ANDROID NDK 来开发一个简单实例: " 光电开关控制 UI 上的灯泡亮灭 "
第一步:开发环境的搭建
1、安装 Eclipse
2、安装 ADT-v20 插件(该版本已经直接支持 Native 编程)
3、安装 CDT(编译 c++ 代码)
3、安装 android-ndk-r8e(该版本支持 Windows,不用在 Cygwin 下编译了)
按照这种模式搭建的开发环境用起来非常上手,java 和 c++ 程序直接交叉编译,然后打包生成 APK.
第二步:实例演练
1、新建 Android 工程 HelloJni,编写 Java 代码如下(定义本地函数)
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setTitle(getHelloJniData());
}
//c++ 中要实现的函数接口
public native String getHelloJniData(); // 从本地函数获得字符串
public native void OpenLed(); // 打开 LED 灯,本例子对应 GPIO3
public native void CloseLed(); // 关闭 LED 灯
public native int ReadGpioState(int i); // 读取某个 GPIO 的状态,然后返回给 Java 层
// 库载入代码
static
{
System.loadLibrary("Hellojni"); //Hellojni 本地 SO 库
}
}
2、实现本地 c++ 代码
(此处注意可以利用 javah 生成头文件,也可以自己按照 Jni 规则写)
#define GPIO_MODE_DIR "/sys/devices/virtual/misc/gpio/mode/"
#define GPIO_PIN_DIR "/sys/devices/virtual/misc/gpio/pin/"
#define GPIO_IF_PREFIX "gpio"
JNIEXPORT jstring JNICALL Java_com_hellojni_MainActivity_getHelloJniData (JNIEnv * env, jobject obj )
{
return (*env)->NewStringUTF(env, "hello jni");
}
JNIEXPORT void JNICALL Java_com_hellojni_MainActivity_OpenLed (JNIEnv * env, jobject obj) // 打开 LED
{
__android_log_print(ANDROID_LOG_INFO,LOG_TAG,"OPEN- LED");
memset(path, 0, sizeof(path));
sprintf(path, "%s%s%d", GPIO_MODE_DIR, GPIO_IF_PREFIX, 3); // 此处对应地址 /sys/devices/virtual/misc/gpio/mode/gpio3 设置为输出
fd = open(path, O_RDWR);
write(fd,"1",1);
close(fd);
memset(path, 0, sizeof(path));
sprintf(path, "%s%s%d", GPIO_PIN_DIR, GPIO_IF_PREFIX, 3); // 此处对应地址 /sys/devices/virtual/misc/gpio/pin/gpio3 设置为高定平
fd = open(path, O_RDWR);
write(fd,"1",1);
close(fd);
}
JNIEXPORT void JNICALL Java_com_hellojni_MainActivity_CloseLed(JNIEnv * env, jobject obj) // 关闭 LED
{
__android_log_print(ANDROID_LOG_INFO,LOG_TAG,"CLOSE LED");
memset(path, 0, sizeof(path));
sprintf(path, "%s%s%d", GPIO_MODE_DIR, GPIO_IF_PREFIX, 3);
fd = open(path, O_RDWR);
write(fd,"1",1);
close(fd);
memset(path, 0, sizeof(path));
sprintf(path, "%s%s%d", GPIO_PIN_DIR, GPIO_IF_PREFIX, 3);
fd = open(path, O_RDWR);
write(fd,"0",1);
close(fd);
}
JNIEXPORT jint JNICALL Java_com_hellojni_MainActivity_ReadGpioState (JNIEnv *env, jobject obj,jint i) // 读取某个 IO 口的状态
{
char buf[4];
int ret = -1;
int fd = -1;
int state;
memset((void *)buf, 0, sizeof(buf));
memset(path, 0, sizeof(path));
sprintf(path, "%s%s%d", GPIO_PIN_DIR, GPIO_IF_PREFIX, i);
fd = open(path, O_RDWR);
ret = read(fd, buf, sizeof(buf));
ret = buf[0] - ''0'';
switch( ret )
{
case 0:
state=0;
// __android_log_print (ANDROID_LOG_INFO,LOG_TAG,"LOW"); 返回 LOG 调试信息
break;
case 1:
state=1;
// __android_log_print(ANDROID_LOG_INFO,LOG_TAG,"HIGHT");
break;
default:
state=-1;
ret = -1;
break;
}
close(fd);
// __android_log_print(ANDROID_LOG_INFO,LOG_TAG,"READ GPIO STATE");
return state;
}
3、到这里关键代码就都准备好了,可以直接在 Java 层调用这些接口函数操作 IO 了。下面最后再配置一下 Eclipse 交叉编译环境。
右击 HelloJni 的工程属性,弹出 窗口,选 Builders --->New---NDK_Builder。
到此开发环境搭建完毕!具体的细节哪里有问题,可以问度娘哦!!!
工程文件见附件!!!
感谢 Lucifer ㊣ (1094294) 大神的指导!!!
hellojni.rar (985.58 KB, 下载次数: 22)
Android NDK r4 san-angeles问题
我开始学习android NDK,并且立即遇到了一个问题。
我已经构建了工具链(花费了比我预期更长的时间!),并且我已经毫无问题地编译了C ++代码,现在我正在尝试构建Java代码。
我立刻遇到一个问题。有一个文件“ main.xml”
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" ><TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World, DemoActivity" /></LinearLayout>
并且出现以下错误:
Description Resource Path Location Typeerror: Error: String types not allowed (at ''layout_height'' with value ''match_parent''). main.xml /DemoActivity/res/layout line 2 Android AAPT Problemerror: Error: String types not allowed (at ''layout_height'' with value ''match_parent''). main.xml /DemoActivity/res/layout line 2 Android AAPT Problemerror: Error: String types not allowed (at ''layout_width'' with value ''match_parent''). main.xml /DemoActivity/res/layout line 2 Android AAPT Problemerror: Error: String types not allowed (at ''layout_width'' with value ''match_parent''). main.xml /DemoActivity/res/layout line 7 Android AAPT Problemerror: Error: String types not allowed (at ''layout_width'' with value ''match_parent''). main.xml /DemoActivity/res/layout line 7 Android AAPT Problem
因此,我可以看到问题出在这些“ match_parent”字符串所在的事实。有人知道怎么修这个东西吗?
答案1
小编典典检查您使用的API级别。
FILL_PARENT``MATCH_PARENT
在API级别8(Android 2.2)中被重命名为。
今天关于Linux配置Java Android, NDK 环境模板和linux配置javahome的介绍到此结束,谢谢您的阅读,有关android ndk (No toolchains found in the NDK toolchains folder for ABI with prefix...)、Android NDK and OpenCV Development With Android Studio、Android NDK GPIO 操作(pcduino 装 Android 系统)、Android NDK r4 san-angeles问题等更多相关知识的信息可以在本站进行查询。
本文标签: