使用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.
#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; }
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
// 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() } }
// 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
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‘]) }
debug { ndk { ldLibs "log" } }
public native void msg(String str);
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‘)] }
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
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 ??ó
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。