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:52]
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>​ +
- <p> +
- 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 : <​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.  +===== 4. Collect configuration data ===== 
- First of all you have to create it :  + 
- <pre class="​code"+Now, to get the all data in "​config_ecj.params",​ we have to create an InputStream.\\ Add these lines in your code :  
-- Right click on src -&​gt; ​new -&​gt; ​package<br> +<​code ​java
-- Name it "​assets"​<​br>​ +InputStream configStream = null; 
- </pre><​br>​ +try { 
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:5.png"><​br>​ +  Context mContext = this; 
- <pre class="​code"+  configStream = mContext.getAssets().open("​config_ecj.params"​);​ 
-- Right click on this new package -&​gt; ​new -&​gt; ​File<br> +} 
-- Name the new file "​config_ecj.params"​ for exemple<​br>​ +catch (IOException e) { 
- </pre><​br>​ +  e.printStackTrace();​ 
- You can now edit your configuration file as usual.<​br>​ +
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:6.png"><​br>​ +</code
- </p> + 
- <div class="​return"><​a href="#​begin">​Back to begin</​a></​div>​ +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. 
- <​h2><​div id="​collectInfo">​4. Collect configuration data</​div></​h2>​ +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 : 
- <p> +<​code ​java
- Now, to get the all data in "​config_ecj.params",​ we have to create an InputStream. ​<​br>​ +ParameterDatabase paramDB = null; 
- Add these lines in your code :  +try { 
- <pre class="​code"+  paramDB = new ParameterDatabase(configStream);​ 
- InputStream configStream = null;<br> +
- try {<br> +catch (IOException e) { 
- <span class="​unAlin">​Context mContext = this;<​br></​span > +  e.printStackTrace();​ 
- <span class="​unAlin">​configStream = mContext.getAssets().open("​config_ecj.params"​);​<​br></​span > +
- } catch (IOException e) {<br> +</code
- <span class="​unAlin">​e.printStackTrace();​<​br></​span > + 
- }<br> +Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44) 
- </pre+<​code ​java
- </​p><​p>​ +EvolutionState evaluatedState = Evolve.initialize(paramDB,​ 0); 
- If your configuration file is named "​config_ecj.params",​ located in  +evaluatedState.startFresh();​ 
-the assets package and "​this"​ correspond to the current context of your  +int result = EvolutionState.R_NOTDONE;​ 
-application,​ this code shouldn'​t cause any problem.<br> +while( result == EvolutionState.R_NOTDONE ) 
- It only remains to transfer the InputStream "​configStream"​ at the ParameterDatabase who's in charge of initializing ECJ.  +  result = evaluatedState.evolve();​ 
- Here is how to initialize the ParameterDatabase :  + </code>  
- <pre class="​code"+ 
- ParameterDatabase paramDB = null;<br> +===== 5. Print configuration data ===== 
- try {<br> + 
- <span class="​unAlin">​paramDB = new ParameterDatabase(configStream);​<​br></​span>​ +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 :  
- }<br> +<​code ​java
- catch (IOException e) {<br> +String out = "";​ 
- <span class="​unAlin">​e.printStackTrace();​<​br></​span>​ +try{ 
- }<br> +  InputStreamReader configStreamReader = new InputStreamReader(configStream);​ 
- </pre><​br+  BufferedReader br = new BufferedReader(configStreamReader);​ 
- Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44) +  String ligne = "";​ 
- <pre class="​code"+  while ( (ligne=br.readLine()) != null) { 
- EvolutionState evaluatedState = Evolve.initialize(paramDB,​ 0);<br> +    out += ligne + "​\n";​ 
- evaluatedState.startFresh();​<br> +  
- int result = EvolutionState.R_NOTDONE;​<br> +  br.close();​ 
- while( result == EvolutionState.R_NOTDONE )<br> + 
- <span class="​unAlin">​result = evaluatedState.evolve();​<​br></​span>​ +catch (Exception e) { 
- </pre><​br+  e.printStackTrace();​ 
- </p> +
- <div class="​return"><​a href="#​begin">​Back to begin</​a></​div>​ +System.out.println("​Out =" + out); 
- <​h2><​div id="​printInfo">​5. Print configuration data</​div></​h2>​ +</code
- <p> +\\ {{ECJ_Android_fichiers:7.png}}\\  
- 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  +===== 6. Write in a File with Android ​===== 
-in the InputStream on logCat console :  + 
- <pre class="​code"+However, ECJ may need a real File to run, and not an InputStream. For example, to initialize a population. 
- String out = "";​<br> + 
- try{<br> +In this part we'll see how to write a file. 
- <span class="​unAlin">​InputStreamReader configStreamReader = new InputStreamReader(configStream);​<​br></​span>​ +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). 
- <span class="​unAlin">​BufferedReader br = new BufferedReader(configStreamReader);​<​br></​span>​ +After, you'll need the context of you application : <​code ​java>Context mContext = this; </code
- <span class="​unAlin">​String ligne = "";​<​br></​span>​ +Then, you have to load your file on your smartphone : <​code ​java>File file = mContext.getFileStreamPath("​config_ecj_in.in"​);​ </code
- <span class="​unAlin">​while ( (ligne=br.readLine()) != null) {<​br></​span>​ +Finaly, you can write into your file thanks to a FileWrite and a BufferedWriter : <​code ​java
- <​span class="​deuxAlin">​out += ligne + "​\n";​<​br></​span>​ +FileWriter fileWriter = null; 
- <span class="​unAlin">​}<​br></​span>​ +try { 
- br.close(); ​<br> +  fileWriter = new FileWriter(file);​ 
- } catch (Exception e) {<br> +} 
- <span class="​unAlin">​e.printStackTrace();​<​br></​span>​ +catch (IOException e1) { 
- }<br> +  e1.printStackTrace();​ 
- System.out.println("​Out =&​gt; ​" + out);<br> +
- </pre><​br+BufferedWriter output = new BufferedWriter(fileWriter);​ 
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:7.png"><​br>​ +try { 
- </p> +  output.write("​Hello World ! "); 
- <div class="​return"><​a href="#​begin">​Back to begin</​a></​div>​ +
- <​h2><​div id="​writeInfo">​6. Write in a File with Android</​div></​h2>​ +output.flush();​ 
- <p> +catch (Exception e) { 
- However, ECJ may need a real File to run, and not an InputStream. For example, to initialize a population. ​<​br><​br>​ +  Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​ 
- 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>​ +finally { 
- Create a file in your workspace named "​config_ecj_in.in"​ (for an initialization file, by example).<br> +  try { 
- After, you'll need the context of you application : <br> +    output.close();​ 
- <pre class="​code"> +    fileWriter.close();​ 
- Context mContext = this; +  } 
- </pre+  ​catch (IOException e) { 
- Then, you have to load your file on your smartphone :  +    Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​ 
- <pre class="​code"> +  
- File file = mContext.getFileStreamPath("​config_ecj_in.in"​);​ +
- </pre+</code
- Finaly, you can write into your file thanks to a FileWrite and a BufferedWriter :  + 
- <pre class="​code"+As you can understand, this code write the text "Hello World" into the file "​config_ecj_in.in"​.\\  
- FileWriter fileWriter = null;<br> + 
- try {<br> +===== 7. Use a file in an another one ===== 
- <span class="​unAlin">​fileWriter = new FileWriter(file);​<​br></​span>​ + 
- } catch (IOException e1) {<br> +Now, you can use this file as in the previous ​[[#​collectInfo|example]].\\  
- <span class="​unAlin">​e1.printStackTrace();​<​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":​ 
- BufferedWriter output = new BufferedWriter(fileWriter);​<br> +<​code>​ 
-         ​try { <br> +pop.file = /​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in 
- <span class="​unAlin">​output.write("​Hello World ! ");<​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 !\\  
-         ​output.flush(); ​ <​br>​ + 
-         ​catch (Exception e) {       <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 !]] 
-         <span class="​unAlin">​Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show(); ​<​br></​span>​ + 
- <br> +<note>written by Ancelin Arnaud, Aliénor Diaz, Olivier Fauvel-Jaeger,​ Frédéric Guégan in 2013/06</note>
-         ​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> +
- </pre+
- 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>​ +
-<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.1393854754.txt.gz · Last modified: 2014/03/03 14:52 by Denis Pallez