User Tools

Site Tools


tuto_ecj_android

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
tuto_ecj_android [2014/03/03 14:52]
Denis Pallez
tuto_ecj_android [2014/03/03 16:05]
Denis Pallez
Line 1: Line 1:
-<​html>​ +====== How to install ECJ library on Android environment ? ====== 
-  <​body>​ +===== 1. Installation of Eclispe and Android SDK ===== 
-    <h1 id="​begin">​Tutorial<​br> ​How to install ECJ library on Android environment ? </​h1>​ + 
- <​ol>​ +First of all, to make the development of Android app easier, you have to install the Eclipse framework. In this environment,​ you will have to install the Android SDK to get all the tools. This two first steps are explained here :\\ [[http://​www.tutomobile.fr/​installer-le-sdk-android-sur-eclipse-tutoriel-android-n%C2%B01/​09/​06/​2010/​|- In French]]\\ [[http://​developer.android.com/​sdk/​installing/​installing-adt.html|- In English]]\\ Then, you have to download the ECJ library ​[[http://​cs.gmu.edu/​%7Eeclab/​projects/​ecj/​|here]]\\ You can find the official ECJ manual ​[[http://​cs.gmu.edu/​%7Eeclab/​projects/​ecj/​docs/​manual/​manual.pdf|here]]\\  
- <​li><​a href="#​install">​Installation of Eclispe and Android SDK</​a></​li>​ + 
- <​li><​a href="#​createANewProject">​Create a new Android Project</​a></​li>​ +[[#begin|Back to begin]] 
- <​li><​a href="#​addEcj">​Add ECJ library in your project</​a></​li>​ + 
- <​li><​a href="#​collectInfo">​Collect configuration data</​a></​li>​ +===== 2. Create a new Android Project ​===== 
- <​li><​a href="#​printInfo">​Print configuration data</​a></​li>​ + 
- <​li><​a href="#​writeInfo">​Write in a File with Android</​a></​li>​ +When the two first steps are done, we will create a new Android project with eclipse. <​code>​ - File -new -Android application project</​code>\\ {{ECJ_Android_fichiers:1.png}}\\ <​code>​- Give a name at your project and let the default configurations. 
- <​li><​a href="#​useNewConfigFile">​Use a file in an another one</​a></​li>​ +- In your workspace, your new project should be as shown below :</code>\\ \\ {{ECJ_Android_fichiers:2.png}}\\  
- </​ol>​ + 
- <​h2><​div id="​install">​1. Installation of Eclispe and Android SDK</​div></​h2>​ +[[#begin|Back to begin]] 
- <p> + 
- First of all, to make the development of Android app easier, you have  +===== 3. Add ECJ library in your project ​===== 
-to install the Eclipse framework. In this environment,​ you will have to  +<​code>​
-install the Android SDK to get all the tools. +
- This two first steps are explained here : <​br>​ +
- <a href="http://​www.tutomobile.fr/​installer-le-sdk-android-sur-eclipse-tutoriel-android-n%C2%B01/​09/​06/​2010/​">- In French</​a><​br>​ +
- <a href="http://​developer.android.com/​sdk/​installing/​installing-adt.html">- In English</​a><​br>​ +
- Then, you have to download the ECJ library ​ +
- <a href="http://​cs.gmu.edu/​%7Eeclab/​projects/​ecj/​">here</​a><​br>​ +
- You can find the official ECJ manual ​ +
- <a href="http://​cs.gmu.edu/​%7Eeclab/​projects/​ecj/​docs/​manual/​manual.pdf">here</​a><​br>​ +
- </p> +
- <div class="​return"><​a href="#begin">Back to begin</​a></​div>​ +
- <​h2><​div id="​createANewProject">​2. Create a new Android Project</​div></​h2>​ +
- <p> +
- When the two first steps are done, we will create a new Android project with eclipse. +
- <pre class="​code"> +
-- File -&​gt; ​new -&​gt; ​Android application project +
- </pre><​br>​ +
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:1.png"><br> +
- <pre class="​code"> +
-- Give a name at your project and let the default configurations.<br> +
-- In your workspace, your new project should be as shown below : <​br>​ +
- </pre><​br>​ +
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:2.png"><​br>​ +
- </p> +
- <div class="​return"><​a href="#begin">Back to begin</​a></​div>​ +
- <​h2><​div id="​addEcj">​3. Add ECJ library in your project</​div></​h2>​ +
- <p> +
- <pre class="​code">+
 - Add in the libs folder the ecj.jar file. - Add in the libs folder the ecj.jar file.
- </pre><​br+</code
- Even if ecj.jar is in the libs folder, it shouldn'​t appear in your workspace. You have to add it in your build path.  +Even if ecj.jar is in the libs folder, it shouldn'​t appear in your workspace. You have to add it in your build path. 
- <pre class="​code"> +<​code>​- Right click on the project name -build path -configure build path... 
-- Right click on the project name -&​gt; ​build path -&​gt; ​configure build path...<br> +- Libraries tab -add external JARs... and select "​ecj.jar"​ in the "​libs"​ folder 
-- Libraries tab -&​gt; ​add external JARs... and select "​ecj.jar"​ in the "​libs"​ folder<br> +- Go on the Order and Export tab -select "​ecj.jar"​ </code>\\ \\ {{ECJ_Android_fichiers:3.png}}\\ {{ECJ_Android_fichiers:4.png}}\\ 
- - Go on the Order and Export tab -&​gt; ​select "​ecj.jar"​<​br>​ +Now, the ECJ library can be used in your Android app, but this library uses some external files including a configuration file. From your Android app point of view, this file is an external file. It's not obvious to include in your app. We will see how to collect the information of this configuration file. First of all you have to create it :  
- </pre><​br>​ +<​code>​ 
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:3.png"><​br>​ +- Right click on src -new -package 
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:4.png"><​br>​ +- Name it "​assets"​ </code>\\ \\ {{ECJ_Android_fichiers:5.png}} 
- Now, the ECJ library can be used in your Android app, but this library +<​code>​ 
- uses some external files including a configuration file. From your  +- Right click on this new package -new -File 
-Android app point of view, this file is an external file. It's not  +- Name the new file "​config_ecj.params"​ for exemple</​code>\\ \\ You can now edit your configuration file as usual.\\ {{ECJ_Android_fichiers:6.png}}\\  
-obvious to include in your app. We will see how to collect the  + 
-information of this configuration file.  +[[#begin|Back to begin]] 
- First of all you have to create it :  + 
- <pre class="​code"+===== 4. Collect configuration data ===== 
-- Right click on src -&​gt; ​new -&​gt; ​package<br> + 
-- Name it "​assets"​<​br>​ +Now, to get the all data in "​config_ecj.params",​ we have to create an InputStream.\\ Add these lines in your code :  
- </pre><​br>​ +<​code ​java
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:5.png"><​br>​ +InputStream configStream = null; 
- <pre class="​code"+try { 
-- Right click on this new package -&​gt; ​new -&​gt; ​File<br> +  Context mContext = this; 
-- Name the new file "​config_ecj.params"​ for exemple<​br>​ +  configStream = mContext.getAssets().open("​config_ecj.params"​);​ 
- </pre><​br>​ +} 
- You can now edit your configuration file as usual.<​br>​ +catch (IOException e) { 
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:6.png"><​br>​ +  e.printStackTrace();​ 
- </p> +
- <div class="​return"><​a href="#begin">Back to begin</​a></​div>​ +</code
- <​h2><​div id="​collectInfo">​4. Collect configuration data</​div></​h2>​ + 
- <p> +If your configuration file is named "​config_ecj.params",​ located in the assets package and "​this"​ correspond to the current context of your application,​ this code shouldn'​t cause any problem. 
- Now, to get the all data in "​config_ecj.params",​ we have to create an InputStream. ​<​br>​ +It only remains to transfer the InputStream "​configStream"​ at the ParameterDatabase who's in charge of initializing ECJ. Here is how to initialize the ParameterDatabase : 
- Add these lines in your code :  +<​code ​java
- <pre class="​code"+ParameterDatabase paramDB = null; 
- InputStream configStream = null;<br> +try { 
- try {<br> +  paramDB = new ParameterDatabase(configStream);​ 
- <span class="​unAlin">​Context mContext = this;<​br></​span > +
- <span class="​unAlin">​configStream = mContext.getAssets().open("​config_ecj.params"​);​<​br></​span > +catch (IOException e) { 
- } catch (IOException e) {<br> +  e.printStackTrace();​ 
- <span class="​unAlin">​e.printStackTrace();​<​br></​span > +
- }<br> +</code
- </pre+ 
- </​p><​p>​ +Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44) 
- If your configuration file is named "​config_ecj.params",​ located in  +<​code ​java
-the assets package and "​this"​ correspond to the current context of your  +EvolutionState evaluatedState = Evolve.initialize(paramDB,​ 0); 
-application,​ this code shouldn'​t cause any problem.<br> +evaluatedState.startFresh();​ 
- It only remains to transfer the InputStream "​configStream"​ at the ParameterDatabase who's in charge of initializing ECJ.  +int result = EvolutionState.R_NOTDONE;​ 
- Here is how to initialize the ParameterDatabase :  +while( result == EvolutionState.R_NOTDONE ) 
- <pre class="​code"+  result = evaluatedState.evolve();​ 
- ParameterDatabase paramDB = null;<br> + </code>  
- try {<br> + 
- <span class="​unAlin">​paramDB = new ParameterDatabase(configStream);​<​br></​span>​ +[[#begin|Back to begin]] 
- }<br> + 
- catch (IOException e) {<br> +===== 5. Print configuration data ===== 
- <span class="​unAlin">​e.printStackTrace();​<​br></​span>​ + 
- }<br> +If you want to be convinced that your configurations are in the InputStream,​ you can add this code who's in charge of printing the data in the InputStream on logCat console :  
- </pre><​br+<​code ​java
- Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44) +String out = "";​ 
- <pre class="​code"+try{ 
- EvolutionState evaluatedState = Evolve.initialize(paramDB,​ 0);<br> +  InputStreamReader configStreamReader = new InputStreamReader(configStream);​ 
- evaluatedState.startFresh();​<br> +  BufferedReader br = new BufferedReader(configStreamReader);​ 
- int result = EvolutionState.R_NOTDONE;​<br> +  String ligne = "";​ 
- while( result == EvolutionState.R_NOTDONE )<br> +  while ( (ligne=br.readLine()) != null) { 
- <span class="​unAlin">​result = evaluatedState.evolve();​<​br></​span>​ +    out += ligne + "​\n";​ 
- </pre><​br+  
- </p> +  br.close();​ 
- <div class="​return"><​a href="#begin">Back to begin</​a></​div>​ + 
- <​h2><​div id="​printInfo">​5. Print configuration data</​div></​h2>​ +catch (Exception e) { 
- <p> +  e.printStackTrace();​ 
- If you want to be convinced that your configurations are in the  +
-InputStream,​ you can add this code who's in charge of printing the data  +System.out.println("​Out =" + out); 
-in the InputStream on logCat console :  +</code
- <pre class="​code"+\\ {{ECJ_Android_fichiers:7.png}}\\  
- String out = "";​<br> + 
- try{<br> +[[#begin|Back to begin]] 
- <span class="​unAlin">​InputStreamReader configStreamReader = new InputStreamReader(configStream);​<​br></​span>​ + 
- <span class="​unAlin">​BufferedReader br = new BufferedReader(configStreamReader);​<​br></​span>​ +===== 6. Write in a File with Android ​===== 
- <span class="​unAlin">​String ligne = "";​<​br></​span>​ + 
- <span class="​unAlin">​while ( (ligne=br.readLine()) != null) {<​br></​span>​ +However, ECJ may need a real File to run, and not an InputStream. For example, to initialize a population. 
- <​span class="​deuxAlin">​out += ligne + "​\n";​<​br></​span>​ + 
- <span class="​unAlin">​}<​br></​span>​ +In this part we'll see how to write a file. 
- br.close(); ​<br> +As in the previous example you'll need a package named "​assets"​.\\ Create a file in your workspace named "​config_ecj_in.in"​ (for an initialization file, by example). 
- } catch (Exception e) {<br> +After, you'll need the context of you application : <​code ​java>Context mContext = this; </code
- <span class="​unAlin">​e.printStackTrace();​<​br></​span>​ +Then, you have to load your file on your smartphone : <​code ​java>File file = mContext.getFileStreamPath("​config_ecj_in.in"​);​ </code
- }<br> +Finaly, you can write into your file thanks to a FileWrite and a BufferedWriter : <​code ​java
- System.out.println("​Out =&​gt; ​" + out);<br> +FileWriter fileWriter = null; 
- </pre><​br+try { 
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:7.png"><​br>​ +  fileWriter = new FileWriter(file);​ 
- </p> +} 
- <div class="​return"><​a href="#begin">Back to begin</​a></​div>​ +catch (IOException e1) { 
- <​h2><​div id="​writeInfo">​6. Write in a File with Android</​div></​h2>​ +  e1.printStackTrace();​ 
- <p> +
- However, ECJ may need a real File to run, and not an InputStream. For example, to initialize a population. ​<​br><​br>​ +BufferedWriter output = new BufferedWriter(fileWriter);​ 
- In this part we'll see how to write a file.<br> +try { 
- As in the previous example you'll need a package named "​assets"​. ​<​br>​ +  output.write("​Hello World ! "); 
- Create a file in your workspace named "​config_ecj_in.in"​ (for an initialization file, by example).<br> +
- After, you'll need the context of you application : <br> +output.flush();​ 
- <pre class="​code"> +catch (Exception e) { 
- Context mContext = this; +  Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​ 
- </pre+
- Then, you have to load your file on your smartphone :  +finally { 
- <pre class="​code"> +  try { 
- File file = mContext.getFileStreamPath("​config_ecj_in.in"​);​ +    output.close();​ 
- </pre+    fileWriter.close();​ 
- Finaly, you can write into your file thanks to a FileWrite and a BufferedWriter :  +  } 
- <pre class="​code"+  ​catch (IOException e) { 
- FileWriter fileWriter = null;<br> +    Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​ 
- try {<br> +  
- <span class="​unAlin">​fileWriter = new FileWriter(file);​<​br></​span>​ +
- } catch (IOException e1) {<br> +</code
- <span class="​unAlin">​e1.printStackTrace();​<​br></​span>​ + 
- }<br> +As you can understand, this code write the text "Hello World" into the file "​config_ecj_in.in"​.\\  
- BufferedWriter output = new BufferedWriter(fileWriter);​<br> + 
-         ​try { <br> +[[#begin|Back to begin]] 
- <span class="​unAlin">​output.write("​Hello World ! ");<​br></​span>​ + 
- }<br> +===== 7. Use a file in an another one ===== 
-         ​output.flush(); ​ <​br>​ + 
-         ​catch (Exception e) {       <br> +Now, you can use this file as in the previous ​[[#​collectInfo|example]].\\  
-         <span class="​unAlin">​Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show(); ​<​br></​span>​ + 
- <br> +But, as we said before, ECJ may need a file. Or more precisely, the path of this file.\\ For a population initialization,​ you give the path of the initialization file to the configuration file.\\ This path is the following one:\\ /​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in.\\ \\ If you want to initialize your population "​config_ecj_in.in"​ from the configuration file "​mainConfigFile.params",​ add this line in "​mainConfigFile.params":​ 
-         ​finally { <br> +<​code>​ 
-         <span class="​unAlin">​try { <​br></​span>​ +pop.file = /​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in 
-             <span class="​deuxAlin">​output.close(); ​<​br></​span>​ +</code
-                 <span class="​deuxAlin">​fileWriter.close(); ​<​br></​span>​ +Now, if you load "​mainConfigFile.params"​ as show in this [[#​collectInfo|example]] you will also initialize you popullation with "​config_ecj_in.in"​.\\ Simple, isn't it ?\\ Here is the end of our tutorial. Good luck !\\  
-            <​span class="​unAlin">​} catch (IOException e) { <​br></​span>​ + 
-             <span class="​deuxAlin">​Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​<​br></​span> ​ +[[#begin|Back to begin]] 
-             <span class="​unAlin">​<​br></​span>​ + 
-       }<br> +Any questions or suggestions ? [[guegan@polytech.unice.fr;​fauvelja@polytech.unice.fr;​adiaz@polytech.unice.fr;​arnaud@polyech.unice.fr?​subject=Tutorial%20ECJ%20in%20Android|Contact us !]] 
- </pre+ 
- As you can understand, this code write the text "Hello World" into the file "​config_ecj_in.in"​.<br> +<note>written by Ancelin Arnaud, Aliénor Diaz, Olivier Fauvel-Jaeger,​ Frédéric Guégan in 2013/06</note>
- </p> +
- <div class="​return"><​a href="#begin">Back to begin</​a></​div>​ +
- <​h2><​div id="​useNewConfigFile">​7. Use a file in an another one</​div></​h2>​ +
- Now, you can use this file as in the previous ​<a href="#​collectInfo">example</a>.<br> +
- <p> +
- But, as we said before, ECJ may need a file. Or more precisely, the path of this file.<​br>​ +
-For a population initialization,​ you give the path of the initialization file to the configuration file. <​br>​ +
-This path is the following one: <​br>​ +
-<pre class="​deuxAlin">​/​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in. ​ </​pre><​br><​br>​ +
- If you want to initialize your population "​config_ecj_in.in"​ from the  +
-configuration file "​mainConfigFile.params",​ add this line in  +
-"​mainConfigFile.params":​<br> +
- <pre class="​code"+
- pop.file = /​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in<br> +
- </pre+
- Now, if you load "​mainConfigFile.params"​ as show in this <a href="#​collectInfo">example</​a> ​you will also initialize you popullation with "​config_ecj_in.in"​.<​br>​ +
- Simple, isn't it ?<​br>​ +
- Here is the end of our tutorial. Good luck ! <br> +
- </p>  +
- <div class="​return"><​a href="#begin">Back to begin</​a></​div>​ +
- <div id="​footer">​ +
- <p> +
- Any questions or suggestions ?  +
- <a href="​mailto:​guegan@polytech.unice.fr;​fauvelja@polytech.unice.fr;​adiaz@polytech.unice.fr;​arnaud@polyech.unice.fr?​subject=Tutorial%20ECJ%20in%20Android">Contact us !</a> +
- </p> +
- </div> +
-  ​+
  
-</​body></​html>​ 
tuto_ecj_android.txt · Last modified: 2014/03/03 17:27 by Denis Pallez