NDK开发与摇篮&放一个Android库项目; Android的工作室 [英] NDK Dev in an Android library project with Gradle & Android Studio

查看:382
本文介绍了NDK开发与摇篮&放一个Android库项目; Android的工作室的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图破解一个摇篮管理使用JNI,我有一点麻烦的Andr​​oid项目。我理解NDK的支持还是比较新的,大多是无证的,但我设法找到里上演它的基本元素融入一个摇篮生成。显然,关键是要包括在所有本地code的的src / main / JNI 拖放按照你的configs之一(例如,在 defaultConfig 块):

  NDK {
    MODULENAMEMYLIB
}

问题是,当我尝试建立我的项目中,NDK插件生成一个Android.mk文件,其中包括对本地源绝对路径。这将导致制作呛,因为它仍然认为路径是相对的。在我来说,我有1 CPP源/头组合一个简单的库项目下的的src / main / JNI ,我用这个 gradle.build

 应用插件:Android的图书​​馆安卓{
    compileSdkVersion 19
    buildToolsVersion19.0.3    defaultConfig {
        9的minSdkVersion
        targetSdkVersion 19
        版本code 1
        的versionName1.0
        NDK {
            MODULENAMEMYLIB
        }
    }
    buildTypes {
        发布 {
            runProguard假
            proguardFiles getDefaultProguardFile('proguard的-android.txt'),'proguard的-rules.txt
        }
    }
}依赖{
    编译文件树(导演:'库',包括:['的* .jar'])
    编译com.android.support:appcompat-v7:19.+
}

运行的版本下构建/ NDK /调试生成此Android.mk:

  LOCAL_PATH:= $(叫我-DIR)
包括$(CLEAR_VARS)LOCAL_MODULE:= MYLIB
LOCAL_SRC_FILES:= \\
    /Users/clifton/dev/Multi/MultiAndroid/lib/src/main/jni/Android.mk \\
    /Users/clifton/dev/Multi/MultiAndroid/lib/src/main/jni/myNativeSectionTextProvider.cpp \\LOCAL_C_INCLUDES + = /用户/克利夫顿的/ dev /多/ MultiAndroid / lib中/ src目录/主/ JNI
LOCAL_C_INCLUDES + = /用户/克利夫顿的/ dev /多/ MultiAndroid / lib中/ src目录/调试/ JNI包括$(BUILD_SHARED_LIBRARY)

...其中,在运行时会产生这个错误:

 使:***没有规则,使目标`/Users/clifton/dev/Multi/MultiAndroid/lib/build/ndk/debug//Users/clifton/dev/Multi/MultiAndroid/lib/src/main/jni/myNativeSectionTextProvider.cpp',由需要`/Users/clifton/dev/Multi/MultiAndroid/lib/build/ndk/debug/obj/local/armeabi-v7a/objs/mylib//Users/clifton/dev/Multi/MultiAndroid/lib/src/main/jni/myNativeSectionTextProvider.o'.停止。

...因为绝对路径错误转化为相对的。如果我手动编辑该文件并更改路径相对,像这样:

  LOCAL_PATH:= $(叫我-DIR)
包括$(CLEAR_VARS)LOCAL_MODULE:= MYLIB
LOCAL_SRC_FILES:= \\
    ../../../src/main/jni/Android.mk \\
    ../../../src/main/jni/myNativeSectionTextProvider.cpp \\LOCAL_C_INCLUDES + = ../../../src/main/jni
LOCAL_C_INCLUDES + = ../../../src/debug/jni包括$(BUILD_SHARED_LIBRARY)

...然后我得到这个错误:

<$p$p><$c$c>/Users/clifton/dev/Multi/MultiAndroid/lib/build/ndk/debug/../../../src/main/jni/com_craig_multiandroid_app_NativeSectionTextProvider.h:2:17:致命错误:jni.h:没有这样的文件或目录

我的问题是我能做些什么来解决这个问题?我开始为.aar建立砍自己的自定义支持的gradle但迷路试图找出哪些摇篮任务是负责产生.aar文件。 (在摇篮文档,而丰富的,让人很难找到一个特定的Andr​​oid摇篮任务的API的细节。)我有这将通过运行CMD线NDK构建一个部分工作gradle.build,生成的.so,但我可以' ŧ弄清楚如何(或者即使我应该)内联.aar内的.so。我使用的Andr​​oid 0.5.7工作室和摇篮1.11。我拉着摇篮源几个月前这是我想出如何内联的.so和gdbserver的文件,在常规的apk项目,但似乎这些规则并不适用于.aar项目。有没有其他人尝试呢?我可以去哪里寻找答案?


解决方案

我理解了它终于来了!您必须使用最新的NDK的新摇篮NDK的支持。我local.properties(和我的〜/ .bashrc)指着Android的NDK-R8E在Android的NDK-r9d来解决破GDB-服务器​​支持但是当我更新到Android的NDK-r9d我gradle这个版本开始工作没有额外的黑客。因此,在总结,上面的例子不工作,只要你local.properties指向NDK的9B版+

I've been trying to hack on a Gradle managed Android project that uses JNI and I'm having a bit of trouble. I understand NDK support is still relatively new and mostly undocumented, but I have managed to find the basic elements for shoe-horning it into a Gradle build. Apparently the trick is to include all of your native code under src/main/jni and drop the following in one of your configs (e.g. in the defaultConfig block):

ndk {
    moduleName "mylib"
}

The problem is that when I try to build my project, the ndk plugin generates an Android.mk file which includes absolute paths to the native source. This causes make to choke as it STILL considers the paths to be relative. In my case I have a simple library project with 1 cpp source/header combo under src/main/jni and I use this gradle.build:

apply plugin: 'android-library'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
        ndk {
            moduleName "mylib"
        }
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
}

Running the build generates this Android.mk under build/ndk/debug:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := mylib
LOCAL_SRC_FILES := \
    /Users/clifton/dev/Multi/MultiAndroid/lib/src/main/jni/Android.mk \
    /Users/clifton/dev/Multi/MultiAndroid/lib/src/main/jni/myNativeSectionTextProvider.cpp \

LOCAL_C_INCLUDES += /Users/clifton/dev/Multi/MultiAndroid/lib/src/main/jni
LOCAL_C_INCLUDES += /Users/clifton/dev/Multi/MultiAndroid/lib/src/debug/jni

include $(BUILD_SHARED_LIBRARY)

…which, when run generates this error:

make: *** No rule to make target `/Users/clifton/dev/Multi/MultiAndroid/lib/build/ndk/debug//Users/clifton/dev/Multi/MultiAndroid/lib/src/main/jni/myNativeSectionTextProvider.cpp', needed by `/Users/clifton/dev/Multi/MultiAndroid/lib/build/ndk/debug/obj/local/armeabi-v7a/objs/mylib//Users/clifton/dev/Multi/MultiAndroid/lib/src/main/jni/myNativeSectionTextProvider.o'.  Stop.

…because the absolute paths are converted mistakenly to relative. If I manually edit the file and change the paths to relative like so:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := mylib
LOCAL_SRC_FILES := \
    ../../../src/main/jni/Android.mk \
    ../../../src/main/jni/myNativeSectionTextProvider.cpp \

LOCAL_C_INCLUDES += ../../../src/main/jni
LOCAL_C_INCLUDES += ../../../src/debug/jni

include $(BUILD_SHARED_LIBRARY)

...I then get this error:

/Users/clifton/dev/Multi/MultiAndroid/lib/build/ndk/debug/../../../src/main/jni/com_craig_multiandroid_app_NativeSectionTextProvider.h:2:17: fatal error: jni.h: No such file or directory

My question is what can I do to fix this? I started to hack my own custom gradle support for .aar builds but got lost trying to figure out which Gradle task is responsible for generating the .aar file. (The Gradle docs, while plentiful, make it difficult to find the details on a specific Android Gradle task API.) I have a partially working gradle.build which will run ndk-build via cmd line, generate the .so but I can't figure out how to (or even if I should) inline the .so inside the .aar. I am using Android Studio 0.5.7 and Gradle 1.11. I've pulled Gradle source a few months ago which is how I figured out how to inline the .so and gdbserver files in a regular .apk project but those rules don't seem to apply to .aar projects. Has anyone else attempted this? Where can I go for answers?

解决方案

I figured it out finally! You have to use the latest NDK for the newer Gradle NDK support. My local.properties (and my ~/.bashrc) was pointing to android-ndk-r8e to work around broken gdb-server support in android-ndk-r9d however when I updated to android-ndk-r9d my gradle build began to work without the extra hacks. So in summary, the above example DOES work so long as your local.properties points to version 9b+ of the NDK.

这篇关于NDK开发与摇篮&放一个Android库项目; Android的工作室的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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