Hands-on: Ada on Android!

and

This hands-on describes how to use a native Ada application on an Android device (ARM processor) without having to 'root' your device. This page has been updated for using the Android Studio and Android wear devices.

[11/02/2017] Minor update: The main process is now created using the ProcessBuilder.

<Plug>

</Plug>

Create a native Ada program for the ARM processor. Create an Android app in Eclipse or Android Studio. Deploy the app on your device.

1. Create a native Ada program for the ARM processor.



with Ada.Text_IO; use Ada.Text_IO;

procedure hello_ada is

begin

loop

declare

Line : String := Get_Line;

begin

Put_Line ("Hello from Ada!, You entered: " & Line);

exit when Line'length = 4 and then Line = "exit";

end;

end loop;

end hello_ada;







arm-android-eabi-gnatmake hello_ada.adb -largs -fPIC -pie



2. Create the Android app.

Alternatively you can install Eclipse, the Android SDK and AVD. A good reference to do this can be found here (

).

In Eclipse, right-click on the 'Assets' entry in the package explorer and click on 'Import'. Use the File System (under 'General') to browse and select the 'hello_ada' executable.

In Eclipse you will see a different Activity class but adapting this Java source should be straightforward.



package com.hello_ada;



import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;



import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.EditText;



public class MainActivity extends AppCompatActivity {

/* Called when the activity is first created. */



static String data_dir = "/data/data/com.hello_ada/";



private void logProcess (Process pr, EditText t) throws IOException

{

/* This routine reads the output and error streams from a process */

/* and logs it to an EditText object. */



int read;

char[] buffer1 = new char[4096];

StringBuffer output = new StringBuffer();



try

{

BufferedReader reader = new BufferedReader(new InputStreamReader(pr.getInputStream()));

while ((read = reader.read(buffer1)) > 0) {

output.append(buffer1, 0, read);

t.append(output.toString() + "

");

}

reader.close();



BufferedReader errReader = new BufferedReader(new InputStreamReader(pr.getErrorStream()));

while ((read = errReader.read(buffer1)) > 0) {

output.append(buffer1, 0, read);

t.append(output.toString() + "

");

}

errReader.close();

}

catch (Exception e) {

t.append("Exception " + e.getMessage());

e.printStackTrace();

}

}



@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);



setContentView(R.layout.activity_main);



EditText log_text = (EditText) findViewById (R.id.editText);



log_text.setText("Test native app



");



Process process = null;



String filename = data_dir + "files/hello_ada";

File hello_ada_file = new File (filename);



try

{

int assets_length = getAssets().open("hello_ada").available();



if ((hello_ada_file.exists()) && (hello_ada_file.length() == assets_length))

log_text.append("file already installed.

");

else

{

log_text.append("Installing...

");



/* Create a directory for the Ada executable */

process = Runtime.getRuntime().exec("mkdir " + data_dir + "files/");

process.waitFor();

logProcess (process, log_text);



DataInputStream is = new DataInputStream (getAssets().open("hello_ada"));

FileOutputStream os = new FileOutputStream (hello_ada_file);



int size = is.available();

log_text.append("Size : " + size + "

");



byte[] buffer = new byte[size];

is.read(buffer);

os.write(buffer);

is.close();

os.close();



log_text.append("File installed.

");



/* Make the file executable */

process = Runtime.getRuntime().exec("chmod 755 " + filename);

process.waitFor();

logProcess (process, log_text);

}



/* Check if the executable exists */

process = Runtime.getRuntime().exec("ls -l " + filename);

process.waitFor();

logProcess (process, log_text);



/* Start the Ada process */

ProcessBuilder prb = new ProcessBuilder ("." + filename);

process = prb.start();



/* Open streams to this process. It should be running now */

/* and waiting for input. */

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));



/* Now you can send text (or commands!) to the Ada program */

writer.write("Hello from Java!" + '

');

writer .flush();



/* Read the result */

log_text.append("Received: " + reader .readLine() + '

');



/* Make sure the Ada process terminates: */

writer . write("exit" + '

');

writer . flush();



log_text.append("Received: " + reader .readLine() + '

');



} catch (IOException e) {

/* TODO Auto-generated catch block */

e.printStackTrace();

} catch (InterruptedException e) {

/* TODO Auto-generated catch block */

e.printStackTrace();

}

}

}



3. Deploy the app on your device.

(or in the 'bin' directory on Eclipse)