自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
設定 Eclipse
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
請按照下列步驟,使用 Eclipse 設定 Tradefed。
請建立專用的工作區來開發 Trade Federation,不要重複使用已用於 Android 裝置開發的工作區。
如有需要,您可以從 eclipse.org/downloads 下載 Eclipse IDE for Java developers。
建立專案
- 透過指令列執行 make 一次。這會建構下列專案所依附的外部程式庫。
- 在
Window > Preferences > Java > Build
Path> Classpath Variables
中設定 TRADEFED_ROOT 類別路徑變數,並將其指向 tradefed 來源根目錄
- 在
Window > Preferences > General >
Workspace > Linked Resources
中設定 TRADEFED_ROOT 路徑變數,並將其指向 tradefed 來源根目錄
使用 File > Import...-> General > Existing Projects into
workspace"
精靈,在下列路徑下匯入這些開放原始碼 Java 專案:
prebuilts/misc/common/ddmlib\*
tools/loganalysis
tools/loganalysis/tests
tools/tradefederation/core
tools/tradefederation/core/tests
tools/tradefederation/contrib
tools/tradefederation/core/remote
platform_testing/libraries/health/runners/longevity/host
platform_testing/libraries/annotations
platform_testing/libraries/health/composers/host
或者,如果您想查看 ddmlib
原始碼,請附加未內含工具分支的原始碼,例如 /platform/tools/base/tools_r22/ddmlib/src/main/java/com/android/ddmlib/IDevice.java。
您也可以選擇匯入 CTS 束帶專案:
test/suite_harness/common/util
test/suite_harness/common/host-side/util
test/suite_harness/common/host-side/tradefed
注意:必要檔案會位於完整平台來源樹狀結構中的 development/ide/eclipse
內。因此,您需要檢出 android-latest-release
等平台分支,才能取得這些檔案:/development/android16-release/ide/eclipse/
使用 Eclipse 中的偏好設定檔,自動將格式化工具設為 Android 樣式指南。如要在 Studio 中執行此操作,請按照下列步驟操作:
- 依序前往「Window」>「Preferences」>「Java」>「Code Style」。
- 在「格式化工具」下方,匯入
android-formatting.xml
檔案。
- 在「整理」>「匯入」下方,匯入
android.importorder
檔案。
移除結尾的空白字元
如要強制 Eclipse 移除所有尾隨空格,請按照下列步驟操作:
- 依序前往「Window」>「Preferences」->「Java」->「Editor」->「Save Actions」。
- 然後依序點選「Additional Actions」->「Configure」->「Code」->「Organizing」分頁標籤->「Formatter」。
- 勾選「移除結尾空格」。
- 按一下「套用並關閉」。
檢查程式碼樣式
提交變更清單時,系統會執行自動預上傳鉤子,檢查程式碼格式:google-java-format
這有助於將程式碼格式化為常見標準。
偵錯 Eclipse
如果您想透過 Eclipse 中的偵錯工具執行 TF 程式碼,建議您先為問題程式碼建立單元測試,因為這是執行功能最簡單且最快速的方式。
如要對 TF 單元測試進行偵錯,只要在該測試上按一下滑鼠右鍵,然後依序選取「Debug As」>「JUnit test」即可。
如要對 TF 功能測試進行偵錯,請按照上一節的操作說明執行功能測試,但請使用「Run」>「Debug Configurations」選單。
如要對 TF 程式本身進行偵錯,請在執行任何設定時,按照前一個章節中執行功能測試的操作說明操作,但請為您想在步驟 4 中執行的設定提供指令列引數。因此,如要對「instrument」設定進行偵錯,請前往「Run」>「Debug configuration」選單,並將 Eclipse 偵錯設定中的「Arguments」分頁設為 -- package <package to run> instrument
。
使用 Eclipse 進行遠端偵錯
請按照下列步驟,透過遠端方式對透過 tradefed.sh
指令列啟動的 Tradefed 工作階段進行偵錯:
- 使用偵錯旗標啟動 tradefed.sh:
TF_DEBUG=1 tradefed.sh
- 請等到您看到 JVM 的提示訊息:
Listening for transport
dt_socket at address: 10088
這表示 JVM 正在等待偵錯工具連結至 10088
通訊埠。
- 從主選單中選取 Eclipse 的遠端偵錯功能:依序選取「Run」>「Debug Configurations...」。
- 在彈出式對話方塊中,從左選單中選取「遠端 Java 應用程式」。
- 按一下動作列上的「New launch configuration」圖示。
- 為設定命名,並選取「tradefederation」做為專案。
- 使用先前提供的地址填入埠。
- 切換至「Source」分頁,然後將「tradefederation」和「google-tradefed」專案新增至「Source Lookup Path」。
- 按一下「Debug」即可開始偵錯工作階段。
偵錯工具會附加至偵聽 JVM 程序,而執行 tradefed.sh
的終端機會顯示 tf>
提示。
如要在偵錯模式下逐步執行程式碼,請在 Eclipse 中設定中斷點,然後在終端機中叫用 Tradefed 指令 (即 run <test>
)。如要在 TF 啟動期間對任何內容進行偵錯,您可以先設定中斷點,然後附加 Eclipse 偵錯工具。
提示:如要使用其他連接埠,請在上述步驟 1 的指令中加入 TF_DEBUG_PORT=nnn
。如果您有需要調查的神秘掛起錯誤,也可以在實際工作環境中使用這個方法:在 tradefed.sh
中將 suspend=y
變更為 suspend=n
,並以偵錯標記開始。JVM 不會等待偵錯工具附加,但只要程序仍在執行,您隨時可以附加偵錯工具。
使用 JDB 進行遠端偵錯
如要使用 Java Debugger JDB,請按照 Eclipse 的步驟操作:
- 使用偵錯標記啟動
tradefed.sh
:TF_DEBUG=1 tradefed.sh
- 等到您看到 JVM 提示訊息:
Listening for transport dt_socket
at address: 10088
。
連結 jdb
。例如,從 croot 執行:
jdb -attach 10088 \
-sourcepath tools/tradefederation/core/src:vendor/google_tradefederation/core/src
等待連線並進行偵錯!如需進一步協助,請執行 man jdb
。
檢查程式碼涵蓋率
- 安裝 Eclemma 外掛程式。
- 依序前往「Help」>「Install New Software」,並將精靈指向以下網址:http://update.eclemma.org/
- 安裝完成後,請選取「Coverage As > JUnit」測試選項,執行程式碼涵蓋率。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Set up Eclipse\n\nFollow these steps to set up Tradefed using Eclipse.\n\nCreate a separate workspace to develop Trade Federation, do not reuse a\nworkspace already employed for Android-device development.\n\nIf needed, you can download the *Eclipse IDE for Java developers* from:\n[eclipse.org/downloads](https://www.eclipse.org/downloads/)\n\nCreate projects\n---------------\n\n1. Run make once from the command line. This will build external libraries depended by the projects below.\n2. Set TRADEFED_ROOT classpath variable in `Window \u003e Preferences \u003e Java \u003e Build\n Path\u003e Classpath Variables` and point it to your tradefed source root\n3. Set TRADEFED_ROOT path variable in `Window \u003e Preferences \u003e General \u003e\n Workspace \u003e Linked Resources` and point it to your tradefed source root\n4. Use the `File \u003e Import...-\u003e General \u003e Existing Projects into\n workspace\"`wizard to bring in these open source Java projects under the\n following paths:\n\n prebuilts/misc/common/ddmlib\\*\n tools/loganalysis\n tools/loganalysis/tests\n tools/tradefederation/core\n tools/tradefederation/core/tests\n tools/tradefederation/contrib\n tools/tradefederation/core/remote\n platform_testing/libraries/health/runners/longevity/host\n platform_testing/libraries/annotations\n platform_testing/libraries/health/composers/host\n\n5. Optionally, if you want to see `ddmlib` source code, attach the source code\n from an unbundled tools branch, such as [/platform/tools/base/tools_r22/ddmlib/src/main/java/com/android/ddmlib/IDevice.java](https://android.googlesource.com/platform/tools/base/+/tools_r22/ddmlib/src/main/java/com/android/ddmlib/IDevice.java).\n\n6. Optionally, if you also want the CTS harness projects loaded, import:\n\n test/suite_harness/common/util\n test/suite_harness/common/host-side/util\n test/suite_harness/common/host-side/tradefed\n\nAuto format\n-----------\n\nNOTE: Requisite files live within `development/ide/eclipse` in the full platform\nsource tree. So you will need to check out a platform branch such as\n`android-latest-release` to get these files:\n[/development/android16-release/ide/eclipse/](https://android.googlesource.com/platform/development/+/refs/heads/android16-release/ide/eclipse/)\n\nUse preference files in Eclipse to automatically set the formatter to the\nAndroid style guide. To do this in Studio:\n\n1. Go to *Window \\\u003e Preferences \\\u003e Java \\\u003e Code Style*.\n2. Under *Formatter* , import the file `android-formatting.xml`.\n3. Under *Organize \\\u003e Imports* , import the file `android.importorder`.\n\n### Remove trailing whitespaces\n\nTo force Eclipse to remove all trailing whitespace:\n\n1. Go to *Window \\\u003e Preferences -\\\u003e Java -\\\u003e Editor -\\\u003e Save Actions*.\n2. Then *Additional Actions -\\\u003e Configure -\\\u003e Code \\\u003e Organizing tab -\\\u003e\n Formatter*.\n3. Check **Remove Trailing Whitespace**.\n4. Click **Apply and Close**.\n\n### Check code style\n\nWhen submitting a changelist, an automatic preupload hook will run to check your\ncode format: `google-java-format`\n\nThis helps formatting your code to the common standard.\n\nDebug Eclipse\n-------------\n\nIf you want to run TF code through a debugger in Eclipse, it is recommended you\nfirst create a unit test for the code in question as this will be the simplest\nand fastest way to exercise the functionalilty.\n\nTo debug a TF unit test, simply right-click on it and select **Debug As \\\u003e JUnit\ntest**.\n\nTo debug a TF functional test, follow the instructions in the previous section\nfor running a functional test but use the *Run \\\u003e Debug configurations* menu.\n\nTo debug the TF program itself, when running any configuration, follow the\ninstructions in the previous section for running a functional test but provide\nthe command line arguments for the configuration you wish to run in step 4. So\nto debug the 'instrument' configuration, go to the *Run \\\u003e Debug configuration*\nmenu and set the *Arguments* tab in the Eclipse debug configuration to\n`-- package \u003cpackage to run\u003e instrument`.\n\n### Remote debug with Eclipse\n\nFollow these steps to remotely debug a tradefed session started from\n`tradefed.sh` command line:\n\n1. Start tradefed.sh with the debug flag: `TF_DEBUG=1 tradefed.sh`\n2. Wait until you see this prompt from the JVM: `Listening for transport\n dt_socket at address: 10088` This means the JVM is waiting for debugger to attach at port `10088`.\n3. Attach with Eclipse's remote debugging from main menu: Select *Run \\\u003e Debug\n Configurations...*.\n4. In the pop-up dialog, select *Remote Java Application* from the left menu.\n5. Click the *New launch configuration* icon on the action bar.\n6. Name the configuration as you desire and select **tradefederation** as the project.\n7. Populate the port using the address provided earlier.\n8. Switch to the *Source* tab and add the projects **tradefederation** and **google-tradefed** to the *Source Lookup Path*.\n9. Click **Debug** to start the debugging session.\n\nThe debugger attaches to the listening JVM process, and the terminal running\n`tradefed.sh` shows the `tf\u003e` prompt.\n\nTo step through your code in debug mode, set a break point in Eclipse and invoke\nyour Tradefed command (i.e. `run \u003ctest\u003e`) in the terminal. To debug anything\nduring TF startup, you can set the break point first and then attach the Eclipse\ndebugger.\n\nTIP: To use an alternative port, add `TF_DEBUG_PORT=nnn`to the command in step 1\nabove. You can even use this in production environment if you have mysterious\nhang bugs to investigate: change `suspend=y` to `suspend=n` in `tradefed.sh` and\nstart with debug flag. The JVM won't wait for debugger to attach, but you can do\nso at any time as long as the process is still running.\n\n### Remote debug using JDB\n\nTo use the Java Debugger JDB, follow steps resembling those for Eclipse:\n\n1. Start `tradefed.sh` with the debug flag: `TF_DEBUG=1 tradefed.sh`\n2. Wait until you see the prompt from JVM: `Listening for transport dt_socket\n at address: 10088`.\n3. Connect `jdb`. For example, from croot run:\n\n jdb -attach 10088 \\\n -sourcepath tools/tradefederation/core/src:vendor/google_tradefederation/core/src\n\n4. Wait for the connection and debug away! Run `man jdb` for more help.\n\nExamine code coverage\n---------------------\n\n1. Install the [Eclemma plugin](https://www.eclemma.org/).\n2. Go to *Help \\\u003e Install New Software* and point the wizard to: http://update.eclemma.org/\n3. Once installed, select the *Coverage As \\\u003e JUnit* test option to conduct a code coverage run."]]