草庐IT

Android gradle ndk jni build with external library & native debugging (ARToolkit)

coder 2023-12-23 原文

我正在尝试让 Android Studio 成为我用于 java 和 c/c++ 代码的主要开发 IDE。我希望能够调试 native 代码。

在这种情况下,我尝试将 ARToolkit5 用作库。

由于 ARToolkit5 中的一些示例,我有这个构建文件。

我有这个 Android.mk 文件

MY_LOCAL_PATH := $(call my-dir)
LOCAL_PATH := $(MY_LOCAL_PATH)

# Pull ARToolKit into the build
include $(CLEAR_VARS)
ARTOOLKIT_DIR := $(MY_LOCAL_PATH)/../../../../../artoolkit5/android
ARTOOLKIT_LIBDIR := $(call host-path, $(ARTOOLKIT_DIR)/obj/local/$(TARGET_ARCH_ABI))
define add_artoolkit_module
    include $(CLEAR_VARS)
    LOCAL_MODULE:=$1
    LOCAL_SRC_FILES:=lib$1.a
    include $(PREBUILT_STATIC_LIBRARY)
endef
ARTOOLKIT_LIBS := ar2 kpm util eden argsub_es armulti ar aricp jpeg arvideo
LOCAL_PATH := $(ARTOOLKIT_LIBDIR)
$(foreach module,$(ARTOOLKIT_LIBS),$(eval $(call add_artoolkit_module,$(module))))

LOCAL_PATH := $(MY_LOCAL_PATH)

# Android arvideo depends on CURL.
CURL_DIR := $(ARTOOLKIT_DIR)/jni/curl
CURL_LIBDIR := $(call host-path, $(CURL_DIR)/libs/$(TARGET_ARCH_ABI))
define add_curl_module
    include $(CLEAR_VARS)
    LOCAL_MODULE:=$1
    #LOCAL_SRC_FILES:=lib$1.so
    #include $(PREBUILT_SHARED_LIBRARY)
    LOCAL_SRC_FILES:=lib$1.a
    include $(PREBUILT_STATIC_LIBRARY)
endef
#CURL_LIBS := curl ssl crypto
CURL_LIBS := curl
LOCAL_PATH := $(CURL_LIBDIR)
$(foreach module,$(CURL_LIBS),$(eval $(call add_curl_module,$(module))))

LOCAL_PATH := $(MY_LOCAL_PATH)
include $(CLEAR_VARS)

# ARToolKit libs use lots of floating point, so don't compile in thumb mode.
LOCAL_ARM_MODE := arm

LOCAL_PATH := $(MY_LOCAL_PATH)
LOCAL_MODULE := ndkDebugModule
LOCAL_SRC_FILES := nftSimple.cpp ARMarkerNFT.c trackingSub.c

# Make sure DEBUG is defined for debug builds. (NDK already defines NDEBUG for release builds.)
ifeq ($(APP_OPTIM),debug)
    LOCAL_CPPFLAGS += -DDEBUG
endif

LOCAL_C_INCLUDES += $(ARTOOLKIT_DIR)/../include/android $(ARTOOLKIT_DIR)/../include
LOCAL_LDLIBS += -llog -lGLESv1_CM -lz
LOCAL_WHOLE_STATIC_LIBRARIES += ar
LOCAL_STATIC_LIBRARIES += ar2 kpm util eden argsub_es armulti aricp jpeg arvideo cpufeatures
#LOCAL_SHARED_LIBRARIES += $(CURL_LIBS)
LOCAL_STATIC_LIBRARIES += $(CURL_LIBS)

include $(BUILD_SHARED_LIBRARY)

$(call import-module,android/cpufeatures)

此版本运行正常。现在我正在尝试将其转换为 android 实验性 gradle 文件以便能够对其进行调试。好吧,现在我处于这种状态:

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.3"

        defaultConfig.with {
            applicationId = "com.nomad5.ndkdebug"
            minSdkVersion.apiLevel = 16
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "0.1"
        }
    }
    /*
    * native build settings
    */
    android.ndk {
        moduleName = "ndkDebugModule"
        cppFlags.add("-I./../../../../../artoolkit5/include/ ")
    }

    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file('proguard-rules.txt'))
        }
        debug {
            debuggable = true
            ndk.with {
                debuggable = true
            }
        }
    }
}

/**
 * The android native sources
 */
android.sources.main {
    jni {
        exportedHeaders {
            srcDirs = [arRoot.absolutePath + "/include",
                       arRoot.absolutePath + "/android/jni/curl/include"]
        }
        source {
            /* we set this to NOT automatically compile everything */
            srcDirs = ["src/main"]
            include "jni/nativeCodeA.cpp"
            include "jni/nativeCodeB.cpp"

        }
        dependencies {
            library "lib_ar2" linkage "static"
            library "lib_kpm" linkage "static"
            library "lib_util" linkage "static"
            library "lib_eden" linkage "static"
            library "lib_argsub_es" linkage "static"
            library "lib_armulti" linkage "static"
            library "lib_ar" linkage "static"
            library "lib_aricp" linkage "static"
            library "lib_jpeg" linkage "static"
            library "lib_arvideo" linkage "static"
            library "lib_cpufeatures" linkage "static"
            library "lib_curl" linkage "static"
        }
    }
    jniLibs {
        source {
            srcDirs = [arRoot.absolutePath + "/include",
                       arRoot.absolutePath + "/android/jni/curl/include"]
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile project(':aRBaseLib')
    compile 'com.android.support:appcompat-v7:23.4.0'
}        

gradle ndk 构建的问题是,在我的文件中(例如 nativeCodeA.cpp)所有包含在 ../../../../../artoolkit5/android 未找到。所以所有的

#include <AR/ar.h>
#include <AR/arMulti.h>
#include <AR/video.h>
...

没有找到。

我如何像 LOCAL_C_INCLUDES 在 makefile 中那样将文件夹添加到 gradle aware ndk 构建中。我如何指定要编译的特定文件,例如 makefile 中的 LOCAL_SRC_FILES(即使没有明确指定这些文件,gradle 如何知道这些文件?)

顺便说一下我正在使用

distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip

'com.android.tools.build:gradle-experimental:0.4.0'

最佳答案

好的,我进行了大量研究并找到了一些有效的示例。首先,你必须在你的 root build.gradle 中使用最新的 gradle experimantal 插件

'com.android.tools.build:gradle-experimental:0.4.0'

然后你的 gradle 文件看起来像这样

apply plugin: 'com.android.model.application'

/**
 * The ar.dir in relative format
 */
def arRoot = new File("../artoolkit5")
def arPath = arRoot.absolutePath + '/android/obj/local/'
def curlPath = arRoot.absolutePath + '/android/jni/curl/libs/'

/**
 * The main experimental model
 */
model {

    /**
     * Android APK values
     */
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.3"

        defaultConfig.with {
            applicationId = "com.nomad5.ndkdebug"
            minSdkVersion.apiLevel = 16
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "0.1"
        }
    }

    /**
     * The build types
     */
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file('proguard-rules.txt'))
        }
        debug {
            debuggable = true
            ndk.with {
                debuggable = true
            }
        }
    }

    /**
     * All statically linked libs
     */
    repositories {
        libs(PrebuiltLibraries) {
            lib_ar2 {
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${arPath}${targetPlatform.getName()}/libar2.a")
                }
            }
            lib_kpm {
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${arPath}${targetPlatform.getName()}/libkpm.a")
                }
            }
            lib_util {
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${arPath}${targetPlatform.getName()}/libutil.a")
                }
            }
            lib_eden {
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${arPath}${targetPlatform.getName()}/libeden.a")
                }
            }
            lib_argsub_es {
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${arPath}${targetPlatform.getName()}/libargsub_es.a")
                }
            }
            lib_armulti {
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${arPath}${targetPlatform.getName()}/libarmulti.a")
                }
            }
            lib_ar {
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${arPath}${targetPlatform.getName()}/libar.a")
                }
            }
            lib_aricp {
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${arPath}${targetPlatform.getName()}/libaricp.a")
                }
            }
            lib_jpeg {
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${arPath}${targetPlatform.getName()}/libjpeg.a")
                }
            }
            lib_arvideo {
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${arPath}${targetPlatform.getName()}/libarvideo.a")
                }
            }
            lib_cpufeatures {
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${arPath}${targetPlatform.getName()}/libcpufeatures.a")
                }
            }
            lib_curl {
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${curlPath}${targetPlatform.getName()}/libcurl.a")
                }
            }
        }
    }

    /*
    * native build settings
    */
    android.ndk {
        moduleName = "ndkDebugModule"
        toolchain = "clang"
        stl = "c++_static"
        platformVersion = 15
        cppFlags.addAll(["-frtti",
                         "-fexceptions",
                         "-I${file(arRoot.absolutePath + "/include")}".toString(),
                         "-I${file(arRoot.absolutePath + "/android/jni/curl/include")}".toString()
        ])
        ldLibs.addAll(['android',
                       'log',
                       'z',
                       'GLESv1_CM'])
        abiFilters.addAll(["armeabi-v7a",
                           /*"arm64-v8a",*/
                           "x86",
                           /*"x86_64"*/])
    }

    /**
     * The android native sources
     */
    android.sources.main {
        jni {
            exportedHeaders {
                srcDirs = [arRoot.absolutePath + "/include",
                           arRoot.absolutePath + "/android/jni/curl/include"]
            }
            source {
                /* we set this to NOT automatically compile everything */
                srcDirs = ["src/main"]
                include "jni/nativeCodeA.cpp"
                include "jni/nativeCodeB.cpp"

            }
            dependencies {
                library "lib_ar2" linkage "static"
                library "lib_kpm" linkage "static"
                library "lib_util" linkage "static"
                library "lib_eden" linkage "static"
                library "lib_argsub_es" linkage "static"
                library "lib_armulti" linkage "static"
                library "lib_ar" linkage "static"
                library "lib_aricp" linkage "static"
                library "lib_jpeg" linkage "static"
                library "lib_arvideo" linkage "static"
                library "lib_cpufeatures" linkage "static"
                library "lib_curl" linkage "static"
            }
        }
        jniLibs {
            source {
                srcDirs = [arRoot.absolutePath + "/include",
                           arRoot.absolutePath + "/android/jni/curl/include"]
            }
        }
    }
}

/**
 * The Java dependencies
 */
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile project(':aRBaseLib')
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
}

/**
 * Dynamically add libs to the linker
 */
class SampleMigrationRuleSource extends RuleSource {

    @Mutate
    void injectArmeabiV7aDebugLinkerFlags(
            @Path('tasks.linkNdkDebugModuleArmeabi-v7aDebugSharedLibrary')
                    Task linkTask) {
        injectLinkerFlags(linkTask, 'armeabi-v7a', 'debug')
    }

    @Mutate
    void injectArmeabiV7aReleaseLinkerFlags(
            @Path('tasks.linkNdkDebugModuleArmeabi-v7aReleaseSharedLibrary')
                    Task linkTask) {
        injectLinkerFlags(linkTask, 'armeabi-v7a', 'release')
    }

    @Mutate
    void injectX86DebugLinkerFlags(
            @Path('tasks.linkNdkDebugModuleX86DebugSharedLibrary')
                    Task linkTask) {
        injectLinkerFlags(linkTask, 'x86', 'debug')
    }

    @Mutate
    void injectX86ReleaseLinkerFlags(
            @Path('tasks.linkNdkDebugModuleX86ReleaseSharedLibrary')
                    Task linkTask) {
        injectLinkerFlags(linkTask, 'x86', 'release')
    }

    private void injectLinkerFlags(linkTask, arch, buildType) {

        def arRoot = new File("../artoolkit5")
        def arPath = arRoot.absolutePath + '/android/obj/local/'
        def curlPath = arRoot.absolutePath + '/android/jni/curl/libs/'

        linkTask.doFirst {
            // We are pretty clueless on this one but it is needed
            if (arch.equals('arm64-v8a')) {
                properties["linkerArgs"].add("-fuse-ld=gold")
            }

            properties["linkerArgs"].addAll([
                    "-l${arPath}/${arch}/libar.a".toString(),
                    "-l${arPath}/${arch}/libar2.a".toString(),
                    "-l${arPath}/${arch}/libutil.a".toString(),
                    "-l${arPath}/${arch}/libkpm.a".toString(),
                    "-l${arPath}/${arch}/libeden.a".toString(),
                    "-l${arPath}/${arch}/libargsub_es.a".toString(),
                    "-l${arPath}/${arch}/libarmulti.a".toString(),
                    "-l${arPath}/${arch}/libaricp.a".toString(),
                    "-l${arPath}/${arch}/libjpeg.a".toString(),
                    "-l${arPath}/${arch}/libarvideo.a".toString(),
                    "-l${arPath}/${arch}/libcpufeatures.a".toString(),
                    "-l${curlPath}/${arch}/libcurl.a".toString(),
            ])
        }
    }
}

apply plugin: SampleMigrationRuleSource

关于Android gradle ndk jni build with external library & native debugging (ARToolkit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39518274/

有关Android gradle ndk jni build with external library & native debugging (ARToolkit)的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  4. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  5. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  6. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  7. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  8. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  9. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  10. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

随机推荐