2015-04-20 50 views
0

這是一個通用的問題,但我在stackoverflow中找不到任何類似的問題,討論最新版本的Android SDK/NDK的解決方案。如何打包Android NDK的示例?

我的目的是將Android NDK打包成apk文件。

所以:

  1. 我安裝的Android NDK到/ opt目錄 - 稱之爲的/ opt /。
  2. 而cd/opt //樣本/其中是任何示例。
  3. 然後輸入 「/選擇// NDK建造」 時,其正常的編譯:
 
/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 
  • 封裝成APK,我使用的 「螞蟻」,所以首先生成build.xml:
  • /opt/android-sdk-linux/tools/android update project --path。 --target機器人-21

  • 「螞蟻調試」
  •  
    
        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 
    
    

    如何克服上述編譯錯誤?

    謝謝。

    回答

    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清理並重建您的示例。如果還是不行,請嘗試刪除如下:

    • 斌/
    • 根/
    • 庫/
    • OBJ/
    • 的build.xml
    • local.properties
    • ProGuard的項目。XML

    ,然後再試一次:

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

    一切都應該工作。如果它仍然不起作用,我會建議啓動android(不帶參數)來啓動Android SDK Manager。確保你是最新的,並確保你已經安裝了API平臺的API平臺21,如果這是你想使用的API級別。截至今日的最新Android SDK工具應爲修訂版24.2。確保你有最新的android-ndk。今天最新的應該是r10e。

    祝你好運!