2016-12-05 80 views
0

我創建了一個允許執行一些iperf命令的Android應用程序。要做到這一點,我得到了iperf的C++項目的源代碼版本3和I交叉編譯使用這些命令是:從Android 7執行IPerf3不起作用

> make clean 
> ./configure --host=arm-linux --prefix=/home/laboPC/Downloads CC=arm-linux-androideabi-gcc CXX=arm-linux-androideabi-g++ CFLAGS="-static" CXXFLAGS="-static" LDFLAGS="-pie -fuse-ld=bfd" 
> make 

的交叉編譯後,我得到了我把一個二進制文件我的android項目中的資產文件夾。

對於使用的iperf於Android,我創建了二進制的這樣一個副本:

private String binariePath = context.getApplicationInfo().dataDir + "/iperf3"; 

private void setupBinaries(){ 
    InputStream in = context.getResources().openRawResource(R.raw.iperf3); 
    OutputStream out = new FileOutputStream(binariePath); 
    byte[] buf = new byte[1024]; 
    int len; 

    while ((len = in.read(buf)) > 0) { 
     out.write(buf, 0, len); 
    } 
    in.close(); 
    out.flush(); 
    out.close(); 
    Runtime.getRuntime().exec("chmod 751 " + binariePath); 
} 

然後,我用一個運行對象來執行這樣的iperf的命令:

public String runClient (String server, String argument) { 
    try { 
     setupBinaries(); 

     process = Runtime.getRuntime().exec(binariePath + " -c " + server + " " + argument); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 

     final StringBuilder result = new StringBuilder(); 
     String line; 
     while ((line = reader.readLine()) != null) { 
      result.append(line + "\n"); 
     } 
     reader.close(); 
     process.destroy(); 
     return result.toString(); 

    } catch (IOException e) { 
     Log.d("IPERF", e.getLocalizedMessage()); 
     return e.getLocalizedMessage(); 
    } 
} 

除了在Android 7.0中,Everythings都能正常工作。當我在Android 7中的Nexus 5X上運行我的應用程序時,iperf命令似乎不會執行,並且我的result變量爲空。

我檢查了Runtime.exec()在Android 7中正常工作,並且二進制文件正確地複製到應用程序數據目錄中。

是不是每個人都有一個想法我的過程中出了什麼問題?我的命令是否正確編譯IPerf項目?

感謝您的幫助。

編輯

我在以下發現線程在Android 6.0及以上可以執行與-fPIC選項編譯的二進制文件:

android ndk: are -fPIC and -pie mututally exclusive?

Position Independent Executables and Android Lollipop

所以我試圖通過使用這個命令行來編譯我的C項目:

./configure --host = arm-linux --prefix =/home/laboPC /下載CC = arm-linux-androideabi-gcc CXX = arm-linux-androideabi -g ++ CFLAGS =「 - static -fPIC」CXXFLAGS = 「-static」LDFLAGS =「 - pie -fuse-ld = bfd」

我認爲我的命令行有些問題,但我不知道是什麼。有人可以幫助我確定我在命令行中的錯誤嗎?

+0

直接從設備shell檢查你的二進制文件:'$ adb push iperf3/data/local/tmp; adb shell「chmod + x/data/local/tmp/iperf3」; adb shell「/ data/local/tmp/iperf3 -c 」'。 – Sergio

+0

感謝您的回答。我這樣做,我得到一個'CAN NOT LINK EXECUTABLE「/ data/local/tmp/iperf3」:/ data/local/tmp/iperf3:有文本重定位。如果我在Nexus 5X以外的設備上執行此操作,則會出現'iperf3:錯誤 - 無法創建新流:Permission denied' – Red

+0

嘗試將'-fpie'添加到'CFLAGS',因爲'-pie'鏈接器選項只有在鏈接代碼與位置無關時纔會執行該操作。同時仔細檢查這些標誌是否傳遞給編譯器和鏈接器。 – Sergio

回答

2

我發現你的問題,它幫助我構建Android的iperf3,但我達成了同樣的問題。
您是否使用Ubuntu存儲庫中的構建工具鏈?如果是的話,構建的二進制文件將無法在Android 7.0上運行,因爲它們使用的是舊版本的構建工具鏈。您將需要使用最新的NDK版本構建二進制文件。 (現在是R13B)

我如何解決它:
-Putted從src文件夾中的所有iperf3來源的JNI文件夾
-Created Android.mk和Application.mk,我將在下面後,在同一個文件夾,以及其他文件。
- 在jni文件夾中,我運行了ndk-build和瞧,/ libs文件夾中的所有二進制文件,甚至可以在Android 7上運行。0(挑二進制文件,把資產的文件夾和實現策略加載正確的二進制文件右ABI,或只得到armeabi二進制和加載到你的應用程序)

提示: iperf3使用一個文件夾來緩存結果Android無法訪問。您需要更改此文件夾的工作:
https://github.com/esnet/iperf/blob/670c18584bcf7a285f3561eb7ea38cc53600d0ab/src/iperf_api.c#L2621

Android.mk:(我認爲這是沒有必要把.h文件這個腳本)
http://pastebin.com/fPsn0wsD

Application.mk:
http://pastebin.com/sgSsGNqB

我推薦使用ndk-build來構建庫,以便爲不同的體系結構構建iperf3,比如x86。