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