<Android-SDK>/tools
folder. As I wrote in the last part of the article, it should already be in the PATH, so you can run directly from the command line or the search string in Windows, but no one, of course, does not interfere and create a shortcut. Those who worked in Eclipse, immediately recognize all the panels in this tool. The most important are logcat and Devices.Debug.waitForDebugger()
in the code. As soon as the program reaches this method, it will stop, and execution will continue further only after connecting the debugger.CTRL+F5
), NetBeans will offer to generate an Ant-file, which will tell him how to do it in our project. That is what we need. After that, in the subfolder of the nbproject
project, the ide-file-targets.xml
, the contents of which need to be replaced with the following: <?xml version="1.0" encoding="UTF-8"?> <project basedir=".." name="KillerApp-IDE"> <import file="../build.xml"/> <target name="-load-props"> <property file="nbproject/debug.properties"/> </target> <target name="-check-props"> <fail unless="jpda.host"/> <fail unless="jpda.address"/> <fail unless="jpda.transport"/> </target> <target name="-init" depends="-load-props, -check-props"/> <target name="-launch-monitor"> <if> <condition> <not> <socket server="localhost" port="8700"/> </not> </condition> <then> <exec executable="${android.tools.dir}/monitor${bat}"/> <waitfor maxwait="20" maxwaitunit="second"> <socket server="localhost" port="8700"/> </waitfor> <sleep seconds="2"/> </then> </if> </target> <target name="-launch-debug" depends="-find-main-activity"> <exec executable="adb"> <arg line="shell am start -D"/> <arg line="${project.app.launcharg}"/> </exec> </target> <target name="debug-nb" depends="-init, -launch-monitor, -launch-debug"> <nbjpdaconnect address="${jpda.address}" host="${jpda.host}" name="${ant.project.name}" transport="${jpda.transport}" /> </target> </project>
debug.properties
file, which you need to debug.properties
into the same folder with the following contents: jpda.host=localhost jpda.address=8700 jpda.transport=dt_socket
debug-nb
, which NetBeans starts when debugging starts, and it depends on the tasks -init
, -launch-monitor
and -launch-debug
. There -init
nothing particularly interesting in -init
, the task simply loads and checks the variables from the debug.properties
file. But -launch-monitor
already more accessible: we need to start the monitor, if it is not already running, and this task just takes on the task. In ant, there is a good task that allows you to see if a program is listening on a specific port or not - a socket
. On this basis, it is just possible to determine whether the monitor is running or not. If not, then you need to start it and wait (task wait-for
). After the launch, it is still worth waiting for two seconds in order for the monitor to start accepting connections (the value may have to be adjusted slightly depending on the specific hardware configuration).adb shell am start -a android.intent.action.MAIN -n < >/<>
command adb shell am start -a android.intent.action.MAIN -n < >/<>
. This time we will analyze the command in a bit more detail. adb shell
is a command that allows you to work directly from the command line inside Android. am
is an activity manager that has a rather impressive set of features; You can read about them in the official documentation . We only need the start
command to start the necessary activity, which we specify after the -n
key, and the -a
key sets the intention, as has probably already become clear.custom_rules.xml
file, there is already a task that gives the parameters needed for launching: -find-main-activity
. This time we need to start the application in the same way as last time, but with the -D
key, so that after launching the application does not immediately continue to work, but first wait for the debugger.debug-nb
already ready to be launched: the monitor is running, the application is running and the debugger is waiting. It remains only to connect it using the job nbjpdaconnect
. As the name implies, this task is purely specific to NetBeans.libs
folder of the main project folder.android update project -p < > -l < >
android update project -p KillerApp -l ../AndroidCompatibilityPackage-v7-appcompat
project.properties
file, we will find a line in this configuration file.android.library.reference.1=../AndroidCompatibilityPackage-v7-appcompat
android.library.reference.2
, android.library.reference.3
and so on.src
, gen
and jar folders in the libs
folder of the library project.android create lib-project -n < > -t android-< API> -p < > -k < >
lib-project
is introduced instead of project
. In addition, you do not need to specify the name of the main activity, since the library will not have to be run directly. Further creation of the project proceeds in the same way as for a regular project.android create test-project -p < > -n < > -m < >
android create test-project -p tests -n KillerAppTest -m ..
test
item in the third step, when we assign ant tasks to different menu items. But from the Run Project
now worth removing the launch
and leaving only debug install
, since we still have nothing to run here.CTRL+F6
, CTRL+F5
and CTRL+SHIFT+F5
. After that, you need to nbproject
files into the nbproject
folder again, just like when adding debugging to a regular project using the second method, only the ide-file-targets.xml
will be slightly different. The beginning of the file is the same as in the case of debugging a regular project, so I do not copy the entire file. Those interested can watch it on BitBucket . But then we have other tasks: <target depends="-setup" name="run-selected-file-in-src"> <fail unless="run.class">Must set property 'run.class'</fail> <echo level="info">Running tests in ${run.class}...</echo> <run-tests-helper> <extra-instrument-args> <arg value="-e"/> <arg value="class"/> <arg value="${run.class}"/> </extra-instrument-args> </run-tests-helper> </target> <macrodef name="launch-debug-and-connect"> <element name="debugged-class" optional="yes"/> <sequential> <parallel> <run-tests-helper> <extra-instrument-args> <debugged-class/> <arg value="-e"/> <arg value="debug"/> <arg value="true"/> </extra-instrument-args> </run-tests-helper> <sequential> <sleep seconds="5"/> <nbjpdaconnect address="${jpda.address}" host="${jpda.host}" name="${ant.project.name}" transport="${jpda.transport}" /> </sequential> </parallel> </sequential> </macrodef> <target depends="-setup, -init, -launch-monitor" name="debug-selected-file-in-src"> <fail unless="debug.class">Must set property 'debug.class'</fail> <echo level="info">Debugging tests in ${debug.class}...</echo> <launch-debug-and-connect> <debugged-class> <arg value="-e"/> <arg value="class"/> <arg value="${debug.class}"/> </debugged-class> </launch-debug-and-connect> </target> <target depends="-setup, -init, -launch-monitor" name="debug-nb"> <launch-debug-and-connect/> </target>
run-selected-file-in-src
task is needed to run individual tests. It uses the run-tests-helper
macro, which is defined in the Android build system with additional parameters. In fact, all that this macro does is run the adb shell am instrument
command with parameters to test the program (yes, this is again the activity manager). We add the -e class < >
arguments to the command launch, so the device will not chase all tests indiscriminately, but will focus on a specific file.parallel
, which runs different tasks together. The result is designed as a macro, so that you can adjust with what parameters the test is called. Accordingly, our debugging tasks just call it, with additional parameters, if needed.R.java
generationant debug install launch
, to build and run the project. And, unlike a self-made script build , it will be a full build - exactly the same as Eclipse does with ADT: with the generation of interfaces from AIDL, BuildConfig, RenderScript, zipalign, ProGuard and all the rest. As for using it to program in NetBeans, this is, of course, very much for the amateur. But, in any case, I personally was very interested in conducting this experiment, and I hope that it was interesting for others to read about it.Source: https://habr.com/ru/post/210926/
All Articles