You are here

How to create an SDK application

This tutorial shows you how to create an application using the SDK binary package.

This tutorial has the following important pre-requisites:

  1. You have obtained the SDK binaries.
  2. You have set up Eclipse to build and run Android applications.
  3. You have set up a LogCat tab within the Eclipse IDE. Details on Eclipse setup can be found on the Eclipse website.

This tutorial takes you through the process of creating an application that uses the SDK binary package. In the approach used in this tutorial, the JAR files that make up the SDK binary package are simply dropped into the libs folder of your existing Android project.

Create an Android project

This task shows you how to create a new Android project in Eclipse. You can use this procedure whenever the other tutorials ask you to create a new Android project - you can change the application name to suit your requirements.
  1. From the Eclipse main menu select File > New > Android Application Project.
  2. Enter a name for the Application Name. In this example enter TestApp1.
  3. Accept the default Project Name.
  4. Enter a value for Package Name.

    In this example enter com.alfresco.tutorials.testapp1.

  5. Set Minimum Required SDK to API 14.
  6. Set Target SDK to API 19.
  7. Set Compile With to API 19.
  8. Leave Theme as default.
  9. Click Next.
  10. Leave checkboxes at defaults:

    • Create custom launcher icon checkbox is selected.
    • Create activity checkbox is selected.
    • Mark this project as library checkbox is cleared.
    • Create Project in Workspace checkbox is selected.
  11. Click Next.
  12. In the Configure Launcher Icon dialog click Next to accept the defaults.
  13. Select the CreateActivity checkbox and ensure Blank Activity is highlighted.
  14. Click Next.
  15. Leave the Activity name as MainActivity and the layout name as activity_main and then click Finish to create the new project.
  16. Test your work so far by running the application. You can do this by right-clicking on the root of your project in the Package Explorer pane then selecting Run As and then Android Application.

    The application will display Hello world!

At this point you have a basic Android project that will build and run.

Extract the library files

You need to extract your SDK Zip file containing the necessary JAR files. These JAR files contain the SDK, and your application needs to build against them in order to create a working Alfresco application.
Unzip the provided Zip file that contains the Alfresco SDK to a suitable directory such as ~/mobile_sdk/binaries/Android, or on Windows, c:\mobile_sdk\binaries\Android.

The following files will be extracted:

alfresco-mobile-android-client-api-1.4.0.jar
alfresco-opencmis-extension-0.7-ANDROID.jar
chemistry-opencmis-android-client-0.11.0.jar
slf4j-android-1.6.1-RC1.jar
          
You have now unzipped all the necessary JAR files, and they are ready for inclusion into your Eclipse project.

Add the external library files to the project

You now need to add the necessary JAR files that incorporate the functionality to work with Alfresco repositories.
  1. In Mac Finder or Windows Explorer navigate to the directory containing the SDK JAR files, in this example, ~/mobile_sdk/binaries/Android/libs.
  2. Drag and drop the JAR files into the libs folder of your project in Eclipse's Package Explorer, ensuring that the Copy Files radio button is selected in the File Operation dialog that is displayed during the drag and drop operation.
The library files have now been physically added into your project.

Add SDK-specific code to your application

You are now in a position to start adding Alfresco-specific code to your application. You will first create an application that simply connects to an Alfresco repository, and retrieve some repository information.
  1. In the Eclipse Package Explorer navigate to your project's source code and load the file MainActivity.java into the editor.
  2. Modify the source code so that it matches the following code:

    Attention: You will need to provide the URL, username, and password for the Alfresco repository you wish to connect to.
            
    package com.alfresco.tutorials.testapp1;
        
    import java.util.List;
    
    import org.alfresco.mobile.android.api.exceptions.AlfrescoSessionException;
    import org.alfresco.mobile.android.api.model.Folder;
    import org.alfresco.mobile.android.api.model.Node;
    import org.alfresco.mobile.android.api.model.RepositoryInfo;
    import org.alfresco.mobile.android.api.model.Site;
    import org.alfresco.mobile.android.api.services.DocumentFolderService;
    import org.alfresco.mobile.android.api.services.SiteService;
    import org.alfresco.mobile.android.api.session.RepositorySession;
    
    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Modify to suit your Alfresco server installation 
            String url = "http://localhost:8080/alfresco";
            String username = "admin";
            String password = "admin";
    
            new ConnectToRepo().execute(url, username, password);
    
        }
    
        class ConnectToRepo extends AsyncTask<String, Integer, String> {
    
            private static final String TAG = "ConnectToRepo";
    
            @Override
            protected String doInBackground(String... params) {
    
                Log.d(TAG, "doInBackground");
                Log.d(TAG, params[0] + ":" + params[1] + ":" + params[2]);
    
                String url = params[0];
                String username = params[1];
                String password = params[2];
    
                // HelloRepo
                try {
                    // connect to on-premise repo
                    RepositorySession session = RepositorySession.connect(url,
                            username, password);
    
                    if (session != null) {
    
                        // Get some repository information
                        Log.d(TAG, "baseUrl: " + session.getBaseUrl());
                        Log.d(TAG, "rootFolder: "
                                + session.getRootFolder().getName());
    
                        // Obtain a repository information object
                        RepositoryInfo repoInfo = session.getRepositoryInfo();
    
                        Log.d(TAG, "repoId: " + repoInfo.getIdentifier());
                        Log.d(TAG, "repoName: " + repoInfo.getName());
                        Log.d(TAG, "repoDescription: " + repoInfo.getDescription());
                        Log.d(TAG, "repoVersion: " + repoInfo.getVersion());
                        Log.d(TAG, "repoEdition: " + repoInfo.getEdition());
    
                        // Get site service
                        SiteService siteService = session.getServiceRegistry()
                                .getSiteService();
    
                        // Get sites for current user
                        List<Site> sites = siteService.getSites();
    
                        // Get first site
                        Site site = sites.get(0);
    
                        // Get site document library
                        Folder folder = siteService.getDocumentLibrary(site);
    
                        // Find DocumentFolderService
                        DocumentFolderService documentFolderService = session
                                .getServiceRegistry().getDocumentFolderService();
    
                        // Get children of document library
                        List<Node> nodes = documentFolderService
                                .getChildren(folder);
    
                        for (Node node : nodes) {
    
                            Log.d(TAG,
                                    "node: " + node.getTitle() + "="
                                            + node.getName() + " created by: "
                                            + node.getCreatedBy() + " isFolder: "
                                            + node.isFolder());
    
                        }
    
                    } else {
    
                        Log.d(TAG, "No Session available!");
    
                    }
    
                } catch (AlfrescoSessionException e) {
                    Log.e(TAG, "Failed to connect: " + e.toString());
                }
    
                Log.d(TAG, "doInBackground Complete");
                return "doInBackground Complete";
            }
    
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                Log.d(TAG, "onPostExecute");
                Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
            }
    
            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
                Log.d(TAG, "onProgressUpdate");
    
            }
    
        }
    
    }
    
          
    Attention: If you are testing this application against a local Alfresco installation do not use the loopback address 127.0.0.1 as this will not work when you run your application in the Android Emulator. Instead, find your local IP address using ifconfig or similar and use that.

    This code provides a minimal application that will connect to the specified Alfresco repository, and retrieve some information about that respository. This information is displayed in LogCat in the Eclipse IDE.

Add permissions to the Android manifest file

This task shows you how to add permissions to your Android manifest file.
You also need to modify your application's Android manifest file, AndroidManifest.xml, to ensure that it has the correct permissions. The file needs to include the following lines:

        
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />        
        
      

Add these two lines, if not present, in the location as illustrated by the following example manifest file:

        
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.alfresco.tutorials.myfirstalfrescoapp"
    android:versionCode="1"
    android:versionName="1" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:configChanges="orientation|keyboardHidden"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        >
        <activity
            android:name="MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>        
      

The manifest file will now have the necessary permissions to access the internet and write to storage.

Run your application

This task shows you how to run your application.
To run your application in the Android emulator, right-click your project in Package Explorer and then select Run As > Android Application.

The Android Emulator will launch. Unlock the Virtual Device and your application will run. The repository information will be displayed in the LogCat tab in the Eclipse IDE (example output shown):

            
...
03-03 08:01:29.921: D/ConnectToRepo(1115): baseUrl: http://10.244.50.101:8080/alfresco
03-03 08:01:29.921: D/ConnectToRepo(1115): rootFolder: Company Home
03-03 08:01:29.921: D/ConnectToRepo(1115): repoId: -default-
03-03 08:01:29.921: D/ConnectToRepo(1115): repoName: 
03-03 08:01:29.921: D/ConnectToRepo(1115): repoDescription: 
03-03 08:01:29.921: D/ConnectToRepo(1115): repoVersion: 5.1.0 (r96687-b328)
03-03 08:01:29.921: D/ConnectToRepo(1115): repoEdition: Enterprise
...     
03-03 08:01:30.911: D/ConnectToRepo(1115): node: Agency related files=Agency Files created by: mjackson isFolder: true
03-03 08:01:30.911: D/ConnectToRepo(1115): node: Project finance files=Budget Files created by: mjackson isFolder: true
03-03 08:01:30.911: D/ConnectToRepo(1115): node: Project meeting notes=Meeting Notes created by: mjackson isFolder: true
03-03 08:01:30.911: D/ConnectToRepo(1115): node: Project presentations=Presentations created by: mjackson isFolder: true
03-03 08:01:30.911: D/ConnectToRepo(1115): doInBackground Complete
03-03 08:01:30.921: D/ConnectToRepo(1115): onPostExecute
...
            
          

In this tutorial you have learned how to build and run an application that uses the Mobile SDK binary package.

Sending feedback to the Alfresco documentation team

You don't appear to have JavaScript enabled in your browser. With JavaScript enabled, you can provide feedback to us using our simple form. Here are some instructions on how to enable JavaScript in your web browser.