JNI和摇篮中的Andr​​oid工作室 [英] JNI and Gradle in Android Studio

查看:318
本文介绍了JNI和摇篮中的Andr​​oid工作室的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加入原有的code到我的应用程序。我把一切都在 ../主/ JNI ,因为它在我的Eclipse项目。我已经加入 ndk.dir = ... 我的 local.properties 。我没有做任何东西,但(我不知道实际需要什么,所以如果我错过了一些东西让我知道)。当我尝试建立我得到这个错误:

 执行失败的任务:应用程序:compileDebugNdk。
> com.android.ide.common.internal.LoggedErrorException:无法运行命令:
    /用户/我/的Andr​​oid NDK-R8E / NDK建造NDK_PROJECT_PATH = NULL
APP_BUILD_SCRIPT = /用户/我/项目/应用程序/编译/ NDK /调试/ Android.mk APP_PLATFORM =机器人-19
NDK_OUT = /用户/我/项目/应用程序/编译/ NDK /调试/ OBJ
NDK_LIBS_OUT = /用户/我/项目/应用程序/编译/ NDK /调试/ lib目录APP_ABI =所有

  错误code:
    2
  输出:
    使:***没有规则,使目标`/Users/me/Project/webapp/build/ndk/debug//Users/me/Project/app/src/main/jni/jni_part.cpp',
 需要`/Users/me/Project/app/build/ndk/debug/obj/local/armeabi-v7a/objs/webapp//Users/me/Project/app/src/main/jni/jni_part.o'.
停止。
 

什么是我需要做什么?

Android.mk:

  LOCAL_PATH:= $(叫我-DIR)

包括$(CLEAR_VARS)

#OpenCV的
OPENCV_CAMERA_MODULES:=上
OPENCV_INSTALL_MODULES:=上
包括... / OpenCV的-2.4.5-Android的SDK / SDK /本地/ JNI / OpenCV.mk

LOCAL_MODULE:= native_part
LOCAL_SRC_FILES:= jni_part.cpp
LOCAL_LDLIBS + = -llog -ldl

包括$(BUILD_SHARED_LIBRARY)
 

Application.mk:

  APP_STL:= gnustl_static
APP_CPPFLAGS:= -frtti -fexceptions
APP_ABI:= armeabi armeabi,V7A
APP_PLATFORM:=机器人-8
 

解决方案

Android的工作室清理并生成集成

其他答案做点出正确的方法为prevent自动创建 Android.mk 文件,但他们没有去整合更好的额外步骤与Android工作室。我加的能力,真正干净,从源代码,而无需进入命令行。你的 local.properties 文件需要有 ndk.dir = /路径/要/ NDK

 应用插件:com.android.application

安卓{
    compileSdkVersion 14
    buildToolsVersion20.0.0

    defaultConfig {
        的applicationIDcom.example.application
        的minSdkVersion 14
        targetSdkVersion 14

        NDK {
            MODULENAMEYourModuleName
        }
    }

    sourceSets.main {
        jni.srcDirs = [] //这个prevents自动生成Android.mk的
        jniLibs.srcDir钢骨混凝土/主/库//除非你有你的项目precompiled库,这是没有必要的。
    }

    任务buildNative(类型:执行,说明:通过NDK编译JNI源'){
        高清ndkDir = android.ndkDirectory
        命令行$ ndkDir / NDK建造
                -C,文件(钢骨混凝土/主/ JNI)。absolutePath,//更改的src /主/ JNI的相对路径的JNI源
                -j,Runtime.runtime.availableProcessors()
                '所有',
                'NDK_DEBUG = 1'
    }

    任务cleanNative(类型:执行,说明:清洁JNI对象文件'){
        高清ndkDir = android.ndkDirectory
        命令行$ ndkDir / NDK建造
                -C,文件(钢骨混凝土/主/ JNI)。absolutePath,//更改的src /主/ JNI的相对路径的JNI源
                '清洁'
    }

    clean.dependsOncleanNative

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

依赖{
    编译com.android.support:support-v4:20.0.0
}
 

的src /主/ JNI 目录承担该项目的标准布局。它应该是从这个相对 build.gradle 文件位置到 JNI 目录。

摇篮 - 为那些有问题

另外,请查阅该<一href="http://stackoverflow.com/questions/25769536/how-when-to-generate-gradle-wrapper-files/27982996#27982996">Stack溢出的答案。

这是非常重要的,你的摇篮版本和一般的设置是正确的。如果你有一个旧的项目,我强烈推荐你创建一个新的使用了最新的Andr​​oid工作室,看看谷歌认为该标准项目。此外,使用 gradlew 。这样可以防止从摇篮版本不匹配开发。最后,摇篮插件必须正确配置。

和你问什么是摇篮插件的最新版本? 检查工具页面并相应修改版本。

的最终产品 - /build.gradle

  //顶级构建文件,你可以到各子项目/模块添加常用的配置选项。

//运行摇篮包装将产生gradlew  - 获取摇篮包装工作,并用它会为你节省很多的痛苦。
任务包装(类型:包装){
    gradleVersion ='2.2'
}

//你看谷歌不使用Maven中央,他们使用jcenter现在。
buildscript {
    库{
        jcenter()
    }
    依赖{
        类路径com.android.tools.build:gradle:1.2.0

        //注意:不要在这里放置应用程序依赖;他们属于
        //在单个模块build.gradle文件
    }
}

allprojects {
    库{
        jcenter()
    }
}
 

确认摇篮包装生成 gradlew 文件和摇篮/包装子目录中。这是一个很大的疑难杂症。

ndkDirectory

这又拿出了很多次,但 android.ndkDirectory 是正确的方法1.1后得到的文件夹。 迁移摇篮项目版本1.0.0 。如果您使用的是实验性或古老版本的插件您的情况可能会有所不同。

I'm trying to add native code to my app. I have everything in ../main/jni as it was in my Eclipse project. I have added ndk.dir=... to my local.properties. I haven't done anything else yet (I'm not sure what else is actually required, so if I've missed something let me know). When I try and build I get this error:

Execution failed for task ':app:compileDebugNdk'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    /Users/me/android-ndk-r8e/ndk-build NDK_PROJECT_PATH=null 
APP_BUILD_SCRIPT=/Users/me/Project/app/build/ndk/debug/Android.mk APP_PLATFORM=android-19 
NDK_OUT=/Users/me/Project/app/build/ndk/debug/obj 
NDK_LIBS_OUT=/Users/me/Project/app/build/ndk/debug/lib APP_ABI=all

  Error Code:
    2
  Output:
    make: *** No rule to make target `/Users/me/Project/webapp/build/ndk/debug//Users/me/Project/app/src/main/jni/jni_part.cpp',
 needed by `/Users/me/Project/app/build/ndk/debug/obj/local/armeabi-v7a/objs/webapp//Users/me/Project/app/src/main/jni/jni_part.o'.  
Stop.

What do I need to do?

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

# OpenCV
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
include .../OpenCV-2.4.5-android-sdk/sdk/native/jni/OpenCV.mk

LOCAL_MODULE    := native_part
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_LDLIBS +=  -llog -ldl

include $(BUILD_SHARED_LIBRARY)

Application.mk:

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi armeabi-v7a
APP_PLATFORM := android-8

解决方案

Android Studio Clean and Build Integration

The other answers do point out the correct way to prevent the automatic creation of Android.mk files, but they fail to go the extra step of integrating better with Android Studio. I have added the ability to actually clean and build from source without needing to go to the command-line. Your local.properties file will need to have ndk.dir=/path/to/ndk

apply plugin: 'com.android.application'

android {
    compileSdkVersion 14
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.example.application"
        minSdkVersion 14
        targetSdkVersion 14

        ndk {
            moduleName "YourModuleName"
        }
    }

    sourceSets.main {
        jni.srcDirs = [] // This prevents the auto generation of Android.mk
        jniLibs.srcDir 'src/main/libs' // This is not necessary unless you have precompiled libraries in your project.
    }

    task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
        def ndkDir = android.ndkDirectory
        commandLine "$ndkDir/ndk-build",
                '-C', file('src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source
                '-j', Runtime.runtime.availableProcessors(),
                'all',
                'NDK_DEBUG=1'
    }

    task cleanNative(type: Exec, description: 'Clean JNI object files') {
        def ndkDir = android.ndkDirectory
        commandLine "$ndkDir/ndk-build",
                '-C', file('src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source
                'clean'
    }

    clean.dependsOn 'cleanNative'

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

dependencies {
    compile 'com.android.support:support-v4:20.0.0'
}

The src/main/jni directory assumes a standard layout of the project. It should be the relative from this build.gradle file location to the jni directory.

Gradle - for those having issues

Also check this Stack Overflow answer.

It is really important that your gradle version and general setup are correct. If you have an older project I highly recommend creating a new one with the latest Android Studio and see what Google considers the standard project. Also, use gradlew. This protects the developer from a gradle version mismatch. Finally, the gradle plugin must be configured correctly.

And you ask what is the latest version of the gradle plugin? Check the tools page and edit the version accordingly.

Final product - /build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

// Running 'gradle wrapper' will generate gradlew - Getting gradle wrapper working and using it will save you a lot of pain.
task wrapper(type: Wrapper) {
    gradleVersion = '2.2'
}

// Look Google doesn't use Maven Central, they use jcenter now.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

Make sure gradle wrapper generates the gradlew file and gradle/wrapper subdirectory. This is a big gotcha.

ndkDirectory

This has come up a number of times, but android.ndkDirectory is the correct way to get the folder after 1.1. Migrating Gradle Projects to version 1.0.0. If you're using an experimental or ancient version of the plugin your mileage may vary.

这篇关于JNI和摇篮中的Andr​​oid工作室的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆