简体   繁体   English

使用MAC OS X将hello-neon(ndk示例)导入Android Studio

[英]Import hello-neon (ndk samples) to Android Studio with MAC OS X

I am new to NDK, gradle, and Android Studio (AS) and meet some problem while importing hello-neon (ndk samples) to AS. 我是NDK,gradle和Android Studio(AS)的新手,在向AS导入hello-neon(ndk示例)时遇到了一些问题。 My operating system is MAC OS X Yosemite. 我的操作系统是MAC OS X Yosemite。

After seting ndk.dir and sdk.dir in local.properties and write android.useDeprecatedNdk=true to gradle.properties , I still cannot make this program because I cannot find the cpu-features.h in helloneon.c . 塞汀后ndk.dirsdk.dirlocal.propertiesandroid.useDeprecatedNdk=truegradle.properties ,我仍然不能让这个计划,因为我无法找到cpu-features.hhelloneon.c

I have searched for several methods. 我搜索了几种方法。 The only valid method is to add 唯一有效的方法是添加

sourceSets.main {
    jni.srcDirs = []
}

to build.gradle . build.gradle

After that, we can Make Project and install it on Android VM. 之后,我们可以Make Project并将其安装在Android VM上。 However, the app will crash when I open it. 但是,当我打开它时,应用程序将崩溃。 To find the reason, I found that public native String stringFromJNI() at HelloNeon.java cannot be linked to its implementation at helloneon.c . 为了找到原因,我发现, public native String stringFromJNI()HelloNeon.java不能链接到其在执行helloneon.c ("cannot resolve corresponding Jni functions") (“无法解析相应的Jni函数”)

Still following the instruction provided at stackoverflow, I use ndk-build to build the source code (need to add environment variable pointing to cpufeatures) and add .so files to this project, like screen shot of the structure of my project 仍然按照stackoverflow提供的指令,我使用ndk-build来构建源代码(需要添加指向cpufeatures的环境变量)并将.so文件添加到这个项目中,就像我的项目结构的屏幕截图一样

After doing so (adding .so files), it is still said "cannot resolve corresponding Jni functions" at stringFromJNI in HelloNeon.java . 在这样做之后(添加.so文件),在HelloNeon.java中的HelloNeon.java仍然说“无法解析相应的Jni函数”。 But it can be "Make Project" and can be successfully run on my VM! 但它可以是“Make Project”,可以在我的VM上成功运行!

I don't know whether it is a good solution because AS still report errors at HelloNeon.java even it can be compile. 我不知道它是否是一个很好的解决方案,因为AS仍然报告HelloNeon.java错误,即使它可以编译。

Then I try add following sentences to build.gradle 然后我尝试将以下句子添加到build.gradle

sourceSets.main {
    jniLibs.srcDir 'src/main/jinLibs' 
}

It does not change any thing. 它不会改变任何事情。 (these code can be delete. --by bullsy) (这些代码可以删除。 - by bullsy)

I also try to copy cpu-features.h and cpu-features.c to /main/jni , then we can find the cpu-features.h but miss other files. 我也尝试将cpu-features.hcpu-features.c复制到/ main / jni,然后我们可以找到cpu-features.h但是错过了其他文件。

As a result, I come here to find some help. 结果,我来这里寻求帮助。 Can you help point out the mistakes we have made and the correct way to solve it? 你能帮我们指出我们所犯的错误和正确的解决方法吗? Although this is a naive problem for experts to this area, I really appreciate your help as a new man to this area. 虽然对于这个领域的专家来说这是一个天真的问题,但我非常感谢你作为这个领域的新人的帮助。

Thank you very much! 非常感谢你!


This is my build.gradle (Module: app) : 这是我的build.gradle (模块:app):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.neon"
        minSdkVersion 4
        targetSdkVersion 4

        ndk {
            moduleName "helloneon"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    sourceSets.main {
        jni.srcDirs = []
    }

    sourceSets.main {
        jniLibs.srcDir 'src/main/jinLibs' // <-- Set your folder here!
    }

}

This is my another build.gradle (Project: hello-neon) : 这是我的另一个build.gradle (Project:hello-neon):

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

This is my settings.gradle : 这是我的settings.gradle

include ':app'
include ':cpufeatures'

My Android Studio version is 1.5.1 我的Android Studio版本是1.5.1

My ndk version ( ./ndk-build -version ) is GNU Make 3.81 我的ndk版本( ./ndk-build -version )是GNU Make 3.81

Thanks again. 再次感谢。


UPDATE UPDATE

Thank you bullsy. 谢谢你。 I added following code in my build.gradle : 我在build.gradle添加了以下代码:

task ndkBuild(type: Exec) {
    commandLine 'ndk-build', '-C', commandLine 'ndk-build', '-C', file('/Users/zhu/AndroidStudioProjects/hello-neon/app/src/main')
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn ndkBuild
}

And got some error information when "Make Project" in ndk-build : ndk-build “Make Project”时得到一些错误信息:

Information:Gradle tasks [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:compileDebugSources, :app:compileDebugAndroidTestSources]
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:prepareDebugAndroidTestDependencies
:app:compileDebugAndroidTestAidl UP-TO-DATE
:app:processDebugAndroidTestManifest UP-TO-DATE
:app:compileDebugAndroidTestRenderscript UP-TO-DATE
:app:generateDebugAndroidTestBuildConfig UP-TO-DATE
:app:generateDebugAndroidTestAssets UP-TO-DATE
:app:mergeDebugAndroidTestAssets UP-TO-DATE
:app:generateDebugAndroidTestResValues UP-TO-DATE
:app:generateDebugAndroidTestResources UP-TO-DATE
:app:mergeDebugAndroidTestResources UP-TO-DATE
:app:processDebugAndroidTestResources UP-TO-DATE
:app:generateDebugAndroidTestSources UP-TO-DATE
:app:ndkBuild
make: *** /Users/zhu/AndroidStudioProjects/hello-neon/app/main: No such file or directory.  Stop.
Error:Execution failed for task ':app:ndkBuild'.
> Process 'command 'ndk-build'' finished with non-zero exit value 2
Information:BUILD FAILED

Then I use /Applications/Android\\ Studio.app/Contents/MacOS/studio to start the AS so that AS can read environment variable from bash, these code works! 然后我使用/Applications/Android\\ Studio.app/Contents/MacOS/studio启动AS,以便AS可以从bash读取环境变量,这些代码有效!

However, this .so files will be store in "/Users/zhu/AndroidStudioProjects/hello-neon/app/src/main/libs" and these .so file cannot be loaded directly (It seems that the project will only load the .so file in jniLibs/). 但是,这个.so文件将存储在“/ Users / zhu / AndroidStudioProjects / hello-neon / app / src / main / libs”中,并且这些.so文件无法直接加载(似乎该项目只会加载。所以jniLibs中的文件/)。

As a result, should I add 结果,我应该补充一下

sourceSets.main {
    jniLibs.srcDir 'src/main/libs' 
}

If not, what should I do? 如果没有,我该怎么办?

Remove this code: 删除此代码:

sourceSets.main {
    jniLibs.srcDir 'src/main/jinLibs' // <-- Set your folder here!
}

Because you're using Android.mk, setting jniLibs.srcDir to empty is correct. 因为您使用的是Android.mk,所以将jniLibs.srcDir设置为空是正确的。 This tells the build system not to override your Android.mk file. 这告诉构建系统不要覆盖您的Android.mk文件。 In order to build properly, you'll need to call ndk-build from your build.gradle and point it to your jni directory. 为了正确构建,您需要从build.gradle调用ndk-build并将其指向您的jni目录。 Setting up the call to ndk-build might look like this: 设置对ndk-build的调用可能如下所示:

// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
    } else {
        commandLine 'ndk-build', '-C', file('src/main').absolutePath
    }
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn ndkBuild
}

These examples are pulled from ph0b's excellent tutorial on working with the older version of the NDK. 这些示例来自ph0b关于使用旧版本NDK的优秀教程 If you are interested in trying the experimental NDK support at some point in the future when you're a little more comfortable, ph0b has posted some great stuff on that too . 如果你有兴趣在未来的某一时刻试图实验NDK的支持,当你舒服些,ph0b已发布了对一些伟大的东西太多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM