如何打包Android NDK的样本?

时间:2015-04-20 13:28:21

标签: android ant android-ndk android-sdk-tools

这是一个通用的问题,但我在stackoverflow中找不到任何类似的问题,讨论Android SDK / NDK最新版本的解决方案。

我的目的是将Android NDK打包为apk文件。

所以:

  1. 我将Android NDK安装到/ opt目录中 - 称为/ opt /.
  2. 和cd / opt // samples / where是任何一个例子。
  3. 然后输入" / opt // ndk-build",它编译好了:
  4. /opt/android-ndk/ndk-build
    [arm64-v8a] Gdbserver      : [aarch64-linux-android-4.9] libs/arm64-v8a/gdbserver
    [arm64-v8a] Gdbsetup       : libs/arm64-v8a/gdb.setup
    [x86_64] Gdbserver      : [x86_64-4.9] libs/x86_64/gdbserver
    [x86_64] Gdbsetup       : libs/x86_64/gdb.setup
    [mips64] Gdbserver      : [mips64el-linux-android-4.9] libs/mips64/gdbserver
    [mips64] Gdbsetup       : libs/mips64/gdb.setup
    [armeabi-v7a] Gdbserver      : [arm-linux-androideabi-4.8] libs/armeabi-v7a/gdbserver
    [armeabi-v7a] Gdbsetup       : libs/armeabi-v7a/gdb.setup
    [armeabi] Gdbserver      : [arm-linux-androideabi-4.8] libs/armeabi/gdbserver
    [armeabi] Gdbsetup       : libs/armeabi/gdb.setup
    [x86] Gdbserver      : [x86-4.8] libs/x86/gdbserver
    [x86] Gdbsetup       : libs/x86/gdb.setup
    [mips] Gdbserver      : [mipsel-linux-android-4.8] libs/mips/gdbserver
    [mips] Gdbsetup       : libs/mips/gdb.setup
    [arm64-v8a] Compile        : hello-jni  libs/arm64-v8a/libhello-jni.so
    [x86_64] Compile        : hello-jni  libs/x86_64/libhello-jni.so
    [mips64] Compile        : hello-jni  libs/mips64/libhello-jni.so
    [armeabi-v7a] Compile thumb  : hello-jni  libs/armeabi-v7a/libhello-jni.so
    [armeabi] Compile thumb  : hello-jni  libs/armeabi/libhello-jni.so
    [x86] Compile        : hello-jni  libs/x86/libhello-jni.so
    [mips] Compile        : hello-jni  libs/mips/libhello-jni.so
    
    1. 要打包到APK,我使用" ant",所以首先生成build.xml:
    2. / opt / android-sdk-linux / tools / android更新项目--path。 --target android-21

      1. " ant debug"
      2. 
            Buildfile: /opt/android-ndk/samples/hello-gl2/build.xml
        
            -set-mode-check:
        
            -set-debug-files:
        
            -check-env:
             [checkenv] Android SDK Tools Revision 24.1.2
             [checkenv] Installed at /opt/android-sdk-linux
        
            -setup:
                 [echo] Project Name: GL2JNIActivity
              [gettype] Project Type: Application
        
            -set-debug-mode:
        
            -debug-obfuscation-check:
        
            -pre-build:
        
            -build-setup:
            [getbuildtools] Using latest Build Tools: 22.0.1
                 [echo] Resolving Build Target for GL2JNIActivity...
            [gettarget] Project Target:   Android 5.0.1
            [gettarget] API level:        21
                 [echo] ----------
                 [echo] Creating output directories if needed...
                [mkdir] Created dir: /opt/android-ndk/samples/hello-gl2/bin/rsObj
                [mkdir] Created dir: /opt/android-ndk/samples/hello-gl2/bin/rsLibs
                 [echo] ----------
                 [echo] Resolving Dependencies for GL2JNIActivity...
            [dependency] Library dependencies:
            [dependency] No Libraries
            [dependency] 
            [dependency] ------------------
                 [echo] ----------
                 [echo] Building Libraries with 'debug'...
               [subant] No sub-builds to iterate on
        
            -code-gen:
            [mergemanifest] No changes in the AndroidManifest files.
                 [echo] Handling aidl files...
                 [aidl] No AIDL files to compile.
                 [echo] ----------
                 [echo] Handling RenderScript files...
                 [echo] ----------
                 [echo] Handling Resources...
                 [aapt] No changed resources. R.java and Manifest.java untouched.
                 [echo] ----------
                 [echo] Handling BuildConfig class...
            [buildconfig] Generating BuildConfig class.
        
            -pre-compile:
        
            -compile:
                [javac] Compiling 6 source files to /opt/android-ndk/samples/hello-gl2/bin/classes
        
            -post-compile:
        
            -obfuscate:
        
            -dex:
                  [dex] input: /opt/android-ndk/samples/hello-gl2/bin/classes
                  [dex] Found modified input file
                  [dex] Converting compiled files and external libraries into /opt/android-ndk/samples/hello-gl2/bin/classes.dex...
        
            -crunch:
               [crunch] Crunching PNG Files in source dir: /opt/android-ndk/samples/hello-gl2/res
               [crunch] To destination dir: /opt/android-ndk/samples/hello-gl2/bin/res
               [crunch] Crunched 0 PNG files to update cache
        
            -package-resources:
                 [aapt] Creating full resource package...
        
            package:
            [apkbuilder] Current build type is different than previous build: forced apkbuilder run.
            [apkbuilder] Creating GL2JNIActivity-debug-unaligned.apk and signing it with a debug key...
            [apkbuilder] Found duplicate file for APK: AndroidManifest.xml
            [apkbuilder] Origin 1: /opt/android-ndk/samples/hello-gl2/bin/GL2JNIActivity.ap_
            [apkbuilder] Origin 2: /opt/android-ndk/samples/hello-gl2/AndroidManifest.xml
        
            BUILD FAILED
            /opt/android-sdk-linux/tools/ant/build.xml:963: The following error occurred while executing this line:
            /opt/android-sdk-linux/tools/ant/build.xml:974: The following error occurred while executing this line:
            /opt/android-sdk-linux/tools/ant/build.xml:313: com.android.sdklib.build.DuplicateFileException: Duplicate files at the same path inside the APK
                at com.android.sdklib.build.ApkBuilder.doAddFile(ApkBuilder.java:821)
                at com.android.sdklib.build.ApkBuilder.addFile(ApkBuilder.java:507)
                at com.android.sdklib.build.ApkBuilder.processFileForResource(ApkBuilder.java:872)
                at com.android.sdklib.build.ApkBuilder.addSourceFolder(ApkBuilder.java:623)
                at com.android.sdklib.build.ApkBuilder.addSourceFolder(ApkBuilder.java:605)
                at com.android.ant.ApkBuilderTask.execute(ApkBuilderTask.java:356)
                at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
                at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:606)
                at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
                at org.apache.tools.ant.Task.perform(Task.java:348)
                at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
                at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
                at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:606)
                at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
                at org.apache.tools.ant.Task.perform(Task.java:348)
                at org.apache.tools.ant.taskdefs.MacroInstance.execute(MacroInstance.java:396)
                at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
                at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:606)
                at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
                at org.apache.tools.ant.Task.perform(Task.java:348)
                at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
                at com.android.ant.IfElseTask.execute(IfElseTask.java:124)
                at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
                at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:606)
                at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
                at org.apache.tools.ant.Task.perform(Task.java:348)
                at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
                at com.android.ant.IfElseTask.execute(IfElseTask.java:124)
                at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
                at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:606)
                at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
                at org.apache.tools.ant.Task.perform(Task.java:348)
                at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
                at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
                at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:606)
                at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
                at org.apache.tools.ant.Task.perform(Task.java:348)
                at org.apache.tools.ant.taskdefs.MacroInstance.execute(MacroInstance.java:396)
                at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
                at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:606)
                at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
                at org.apache.tools.ant.Task.perform(Task.java:348)
                at org.apache.tools.ant.Target.execute(Target.java:435)
                at org.apache.tools.ant.Target.performTasks(Target.java:456)
                at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
                at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
                at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
                at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
                at org.apache.tools.ant.Main.runBuild(Main.java:851)
                at org.apache.tools.ant.Main.startAnt(Main.java:235)
                at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
                at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
        
            Total time: 2 seconds
        
        

        如何克服上述编译错误?

        感谢。

1 个答案:

答案 0 :(得分:0)

首先,看起来您构建了hello-jni的原生端,然后构建并打包了hello-gl2。这是两个不同的样本。你能确定你留在同一个样本文件夹吗?

我还强烈建议您将/opt/android-ndk-r10e/opt/android-sdk-linux/tools/opt/android-sdk-linux/platform-tools添加到PATH环境变量中。只需将此行添加到~/.profile,然后重新启动终端。

export PATH=${PATH}:/opt/android-ndk-r10e;/opt/android-sdk-linux/tools:/opt/android-sdk-linux/platform-tools"
这样,您就可以访问任何Android工具,例如ndk-buildadbandroid,而无需指定其绝对路径。

如果这样做无效,请尝试使用ant clean && ant debug清理并重建样本。如果这不起作用,请尝试删除以下内容:

  • 仓/
  • GEN /
  • 库/
  • OBJ /
  • 的build.xml
  • local.properties
  • proguard的-project.xml中

然后再试一次:

ndk-build NDK_DEBUG=1
android update project --path . --target android-21
ant debug

一切都应该有效。如果它仍然不起作用,我建议启动android(没有参数)来启动Android SDK Manager。确保您是最新的,并确保安装了API Level 21的SDK平台(如果这是您要使用的API级别)。截至今天的最新Android SDK工具应该是版本24.2。确保你也拥有最新的android-ndk。今天最新的应该是r10e。

祝你好运!