使用AndroidStudio编译NDK的方法及错误解决方式
Execution failed for task ':hellojni:compileDebugNdk'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
D:\ndk\ndk-build.cmd NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=F:\androidstudio\test\hellojni\build\ndk\debug\Android.mk APP_PLATFORM=android-19 NDK_OUT=F:\androidstudio\test\hellojni\build\ndk\debug\obj NDK_LIBS_OUT=F:\androidstudio\test\hellojni\build\ndk\debug\lib APP_ABI=armeabi,armeabi-v7a
Error Code:
2
Output:
D:/ndk/build/core/setup-app.mk:63: *** Android NDK: Aborting . Stop.
name”gradle project info的解决的方法”来解决。
.png)
.png)
.png)
.png)
#include <stdio.h>
#include <stdlib.h>
#include <jni.h>
#include <assert.h>
#include <sys/types.h>
#include <android/log.h> #define LOG_TAG "Hellojni"
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) //注冊native api的类#define JNIREG_CLASS "com/example/test9/app/MainActivity" extern "C" {
JNIEXPORT void msg(JNIEnv *env, jobject clazz, jstring str);
}; //jstring to char* char* jstringTostring(JNIEnv* env, jstring jstr)
{
char* rtn = NULL;
jclass clsstring = env->FindClass("java/lang/String");
jstring strencode = env->NewStringUTF("utf-8");
jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode);
jsize alen = env->GetArrayLength(barr);
jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
if (alen > 0)
{
rtn = (char*)malloc(alen + 1);
memcpy(rtn, ba, alen);
rtn[alen] = 0;
}
env->ReleaseByteArrayElements(barr, ba, 0);
return rtn;
} JNIEXPORT void msg(JNIEnv *env, jobject clazz, jstring str)
{
char *pszstr = NULL; pszstr = jstringTostring(env, str);
LOGI("%s", pszstr);
free(pszstr);
} /**
* Table of methods associated with a single class.
*/static JNINativeMethod gMethods[] = {
{ "msg", "(Ljava/lang/String;)V", (void*)msg},
}; /*
* Register native methods for all classes we know about.
*/static int registerNativeMethods(JNIEnv* env)
{
int nError = 0;
jclass clazz = NULL; clazz = env->FindClass(JNIREG_CLASS);
if (clazz == NULL) {
LOGE("clazz is null");
return JNI_FALSE;
} nError = env->RegisterNatives(clazz, gMethods, sizeof(gMethods) / sizeof(gMethods[0]) );
if ( nError < 0 ) {
LOGE("RegisterNatives error: %d num: %d",nError, sizeof(gMethods) / sizeof(gMethods[0]) );
return JNI_FALSE;
} return JNI_TRUE;
} /*
* Set some test stuff up.
*
* Returns the JNI version on success, -1 on failure.
*/
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
jint result = -1; if(vm->GetEnv((void**) &env,JNI_VERSION_1_6) != JNI_OK){
return -1;
}
assert(env != NULL); if (!registerNativeMethods(env)) {
LOGE("registerNativeMethods failed");
return -1;
} /* success -- return valid version number */
result = JNI_VERSION_1_6; return result;
}
这里仅仅导出一个msg函数打印传递进来的字符串,仅作測试。再在jni文件夹下新建一个empty.cpp文件,内容为空,这个是为了解决NDK的bug所作的,以防编译出错。
sdk.dir=D\:/adt20131030/sdk
ndk.dir=D\:/ndk
#Wed Apr 10 15:27:10 PDT 2013
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.9-all.zip
为:
#Wed Apr 10 15:27:10 PDT 2013
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.10-all.zip
并打开项目根文件夹下的build.gradle文件,改动:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
} allprojects {
repositories {
mavenCentral()
}
}
为(指定使用gradle1.10则改动为0.9.+,指定使用gradle1.11则改动为0.9.2):
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
} allprojects {
repositories {
mavenCentral()
}
}
0.7.0
Requires Gradle 1.9
Requires Studio 0.4.0
0.9.0
Compatible with Gradle 1.10 and 1.11
Using Gradle 1.11 requires Android Studio 0.5.0
假设配置的是0.7.+则默认使用gradle1.9,假设设置为0.9.+则默认使用gradle1.10。
assert gradle.gradleVersion >= "1.10"
apply plugin: 'android-library'
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 8
targetSdkVersion 16
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
ndk {
moduleName "hellojni"
abiFilters "armeabi", "armeabi-v7a", "x86"
}
}
debug {
ndk {
moduleName "hellojni"
//stl "stlport_shared"
ldLibs "log", "z", "m"
//cFlags "-Wall -Wextra -I " + projectDir + "/src/main/jni/include"
abiFilters "armeabi", "armeabi-v7a", "x86"
}
}
}
productFlavors {
x86 {
versionCode Integer.parseInt("6" + defaultConfig.versionCode)
ndk {
abiFilter "x86"
}
}
mips {
versionCode Integer.parseInt("4" + defaultConfig.versionCode)
ndk {
abiFilter "mips"
}
}
armv7 {
versionCode Integer.parseInt("2" + defaultConfig.versionCode)
ndk {
abiFilter "armeabi-v7a"
}
}
arm {
versionCode Integer.parseInt("1" + defaultConfig.versionCode)
ndk {
abiFilters "armeabi", "armeabi-v7a"
}
}
fat
}
}
dependencies {
compile 'com.android.support:appcompat-v7:19.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
然后选择hellojni项目右键“Make Module hellojni”,等待一段时间后会在项目下生成build-ndk文件夹,文件夹下会有一些不同版本号的so库文件生成,如图:
.png)
debug {
ndk {
ldLibs "log"
}
}
由gradle依据配置再去生成Android.mk文件,最后再调用ndk进行编译。
.png)
public native void msg(String str);
并加入静态代码载入hellojni库:
static {
System.loadLibrary("hellojni");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
msg("MainActivity onCreate");
}
task copyNativeLibs(type: Copy) {
from fileTree(dir: '../hellojni/build/ndk/arm/debug/lib', include: 'armeabi/*.so') into 'build/lib'
}
tasks.withType(Compile) {
compileTask -> compileTask.dependsOn copyNativeLibs
}
clean.dependsOn 'cleanCopyNativeLibs'
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniFolders = [new File(buildDir, 'lib')]
}
当中copyNativeLibs任务是从相对app的项目路径'../hellojni/build/ndk/arm/debug/lib'下复制全部armeabi子文件夹的so文件到本项目build文件夹下的lib文件夹中,运行效果:
.png)
Execution failed for task ':hellojni:compileDebugNdk'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
D:\ndk\ndk-build.cmd NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=F:\androidstudio\test\hellojni\build\ndk\debug\Android.mk APP_PLATFORM=android-19 NDK_OUT=F:\androidstudio\test\hellojni\build\ndk\debug\obj NDK_LIBS_OUT=F:\androidstudio\test\hellojni\build\ndk\debug\lib APP_ABI=armeabi,armeabi-v7a
Error Code:
2
Output:
make.exe: *** No rule to make target `F:\androidstudio\test\hellojni\build\ndk\debug\obj/local/armeabi/objs/jnimain/F_\androidstudio\test\hellojni\src\main\jni', needed by `F:\androidstudio\test\hellojni\build\ndk\debug\obj/local/armeabi/objs/jnimain/F_\androidstudio\test\hellojni\src\main\jni\hellojni.o'. Stop.
解决方式:
This may come from a current NDK bug on Windows, when there is only one source file to compile. You only need to add one empty source to make it work again.
Could not determine the dependencies of task ':hellojni:compileArmDebugJava'.
> failed to find Build Tools revision 19.0.3
.png)
FAILURE: Build failed with an exception. * What went wrong:
Task 'assembleArmDebug' not found in project ':hellojni'. Some candidates are: 'assembleDebug'. * Try:
Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
productFlavors{
arm {
}
}
若有类似错误能够參考增加对应的标签:
productFlavors {
x86 {
versionCode Integer.parseInt("6" + defaultConfig.versionCode)
ndk {
abiFilter "x86"
}
}
mips {
versionCode Integer.parseInt("4" + defaultConfig.versionCode)
ndk {
abiFilter "mips"
}
}
armv7 {
versionCode Integer.parseInt("2" + defaultConfig.versionCode)
ndk {
abiFilter "armeabi-v7a"
}
}
arm {
versionCode Integer.parseInt("1" + defaultConfig.versionCode)
ndk {
abiFilter "armeabi"
//abiFilters "armeabi", "armeabi-v7a"
}
}
fat
}
Execution failed for task ':hellojni:compileDebugNdk'.
> java.io.IOException: Cannot run program "D:\ndk\ndk-build": CreateProcess error=193, %1 ??????Ч?? Win32 ??ó
解决方式:
A problem occurred evaluating project ':app'.
> Could not create plugin of type 'AppPlugin'.
解决方式:
Execution failed for task ':hellojni:compileDebugNdk'.
> java.io.IOException: Cannot run program "D:\ndk\ndk-build": CreateProcess error=193, %1 ??????Ч?? Win32 ??ó
.png)
.png)
.png)
使用AndroidStudio编译NDK的方法及错误解决方式的更多相关文章
- 使用AndroidStudio编译NDK的方法及错误解决方案
参考资料: [android ndk]macos环境下Android Studio中利用gradle编译jni模块及配置:http://demo.netfoucs.com/ashqal/article ...
- VS2012 编译程序时报无法载入PDB文件错误解决方式
VS2012 编译程序时报无法载入PDB文件错误解决方式 "ConsoleApplication1.exe"(Win32): 已载入"C:\Users\hp\Docume ...
- Delphi - 10.1编译OSX10.12程序遇到错误解决了!
昨天,尝试Delphi的跨平台开发功能,在windows10下,做了一个控制台程序,发布目标平台是OSX10.12,中间配置过程都非常顺利,没有任何错误,但是当编译运行时候出现下面错误: [dccos ...
- 史上最详细 Python第三方库添加方法 and 错误解决方法
(1):如何添加python第三方库(方法一): File ->> Settings... ->> Project Interpreter (2):如何添加python第三方库 ...
- Android加载图片OOM错误解决方式
前几天做项目的时候,甲方要求是PAD (SAMSUNG P600 10.1寸 2560*1600)的PAD上显示高分辨率的大图片. SQLITE採用BOLD方式存储图片,这个存取过程就不说了哈,网上一 ...
- 8000401a错误解决方式(Excel)
前一阵子做开发须要用到Excel和Word编程,本人用的是Vista系统,开发环境是VS2005和Office2007,測试无不论什么问题,但是到部署的时候出现了一些令人非常头痛的问题,老是会出现比如 ...
- redis 创建集群时 出现的错误解决方式
1. 创建集群时报以下错误 (1)错误1 ./redis-trib.rb create --replicas 1 XXXXXX:5301 XXXXXX:5302 XXXXXX:5303 XXXXXX: ...
- apt-get install安装软件时出现依赖错误解决方式
在使用apt-get install安装软件时,常常会遇到如上图所看到的错误.该错误的意思为缺少依赖软件.解决方式为: aptitude install golang-go
- VS编译duilib项目时候的错误解决方法整理(转载)
转载自:http://blog.csdn.net/x356982611/article/details/30217473 @1:找不到Riched20.lib 用everything等软件搜索下磁盘, ...
随机推荐
- 重要BLOG
Cloud http://www.cnblogs.com/CloudMan6/tag/OpenStack/ 算法基础 http://www.cnblogs.com/ECJTUACM-873284962 ...
- Android实现不同Active页面间的跳转
Intent intent = new Intent(); intent.setClass(ErrorPageActive.this, LoginActive.class); startActivit ...
- Metinfo 5.x 管理员密码重置漏洞
前言 在先知看到了一篇分析该漏洞的文章,复现分析一下,漏洞还是比较有趣的. 正文 首先知道是 管理员密码重置时出现的问题,于是抓包,定位到相关的php文件. 首先包含了 ../include/comm ...
- MyBatis -01- 初识 MyBatis + MyBatis 环境搭建
MyBatis -01- 初识 MyBatis + MyBatis 环境搭建 MyBatis 本是 apache 的一个开源项目 iBatis(iBATIS = "internet" ...
- spring shiro 集成
1.向spring项目中添加shiro相关的依赖 <dependency> <groupId>commons-logging</groupId> <artif ...
- spring boot(14)-pom.xml配置
继承spring-boot-starter-parent 要成为一个spring boot项目,首先就必须在pom.xml中继承spring-boot-starter-parent,同时指定其版本 & ...
- AIX解压ZIP文件
AIX系统自身是没有解压ZIP文件的,但在AIX安装oracle数据库服务器的话,在$ORACLE_HOME/bin路径下方却有unzip命令,可以解压ZIP文件. 一.shell脚本 之前的版本 ...
- UpdateServer事务实现机制
UpdateServer(UPS) 是OceanBase的写入单点,一个集群中只有一台UPS服务器,所有的写都写入到这台机器.OceanBase采用基于静动态数据分离的机制,静态数据存储在静态数据服务 ...
- 远程连接MySQL服务器
在CentOS虚拟机上安装好了MySQL服务以后,在windows上用Workbench客户端去连接时碰到很多问题,现在把解决过程记录一下. 1.在Windows上ping CentOS IP是可以p ...
- GONMarkupParser的使用
GONMarkupParser的使用 说明 这是一个写得非常好的富文本工具类,便于你进行简易的封装.本人抛砖引玉,只进行了少量的简化使用封装. 效果 源码 https://github.com/nic ...