Tuesday, December 6, 2011

Android using Visual Studio 2010: Step 03: : Sample Project (Simple Text Display)

Now that we have our computer environment setup for android using visual studio 2010, let’s build a simple project. I will explain taking “simpleTextDisplay” project as reference. It is an extremely simple project to display a text sent from a CPP file.

Steps involved in project execution:
  • Create Win32 project
  • Include the Android basic pack to the Win32 project
  • Create Java source
  • Create JNI source
  • Setup Android project specific properties
  • Compile and run project in visual studio 2010

Simple Text Project
  • Create Win32 project: Download and unzip the Win32 project “simpleTextDisplay”. 

  • Include the Android basic pack to the Win32 project: Download the “AndroidProject” pack. This pack includes the essentials for you to build your android project using visual studio 2010. Unzip this pack inside your Win32 project (“simpleTextDisplay” in this case) as shown below.
        The pack has the following:
    • jni folder: This will house the jni files that we would need to communicate between cpp and java.
    • res folder:  This has couple of xml files and the application icons required for the building process. You can read more about it here.
    • src folder: This folder contains the java source files. The files would be in the proper package folder.
    • AndroidManifest.xml file: It provides essential information about the application to the Android system, information the system must have before it can run any of the application’s code.
    • default.properties file: This file contains the target platform for the application.
    • cmd.bat file: It is just to open command prompt, cause I am to lazy to do that extra few steps!
    For CPP to communicate with Android it has to follow certain steps as shown below.

    So for us to run our project we need to create a link between the project source files (in this case cpp files of the “simpleTextDisplay”) and android through JNI and java files.

    • Create JAVA source:
      • Go to <trunk>\ AndroidProject\src.
      • You would notice that it has subfolders named com, myProjects and projectName. These folders indicate the project path that android would use to install the application in the target device. You can rename them as you wish (without space in name).
      • The java source files are supposed to be in the <trunk>\AndroidProject\src\com\myProjects\projectName folder.
      • Let’s create a java source file named “AndroidTextDisplay.java” here. This file will create an activity and display the text sent from the native source.

      The method “stringFromJni()” would create a link between Java and JNI to receive the text to display.
      Note: By default the .so file generated has the name of the visual studio project name (simpleTextDisplay in this case).
      • You can read more about Android Java here.

    • Create JNI source:
      • Go to <trunk>\ AndroidProject\jni.
      • Now that we have established a link between Java and JNI, we need to thread the final connection between JNI and CPP in order to complete the communication.
      • Create a CPP file named “NativeJavaLink.cpp” here.
      • Let’s create a JNI method which in turn calls the native source method (cGame::getInstance()->getText() in this case) and sends it to the Java source.

      Note: The JNI file we created here is of extension CPP instead of the commonly used C. This is to ease the compilation process using visual studio 2010. However, the JNI methods are supposed to be C style so we are using, 
      extern “C” {
      // JNI method declaration
      }
      ,to indentify the CPP compiler that the that the following code must be compiled as C style.
      Also there is a specific syntax for JNI method naming, it is shown below,
      Tips: As you can see the different parts of the method name have ‘_’ (underscore) as separator. This is the reason why we do not use ‘_’ (underscore) as a part of the name itself, as it might cause compilation issues. For example: Say, your java project path has ‘_’ (underscore) it’s name, com.my_Projects.projectName, then your JNI method name above would be Java_com_my_Projects_projectName_AndroidTextDisplay_stringFromJni. This might cause the compiler to think that the project path is actually com.my.Projects, which is wrong.

    • Setup Android project specific properties:
      At this point we have all the source files we need. Now, we need to setup android project specific properties.
        • Open file <trunk>\AndroidProject\AndroidManifest.xml.
        • Update the package, android:name and android:minSdkVersion values as shown below.
        Save the changes and close the file.

        • Now open file <trunk>\AndroidProject\default.properties and update the target value to the appropriate version. Normally we keep it corresponding to the value we set for android:minSdkVersion in AndroidManifest.xml. Hence, android-8 in this case. Save the changes and close the file.
        • Double click on the cmd.bat and type, android update project –p .\ (link to : Managing Projects from the command line), and press enter.  This should create three new files; local.properties, build.xml and proguard.cfg.
        • These files use AndroidManifest.xml and default.properties to generate the new files. Together, they finalize the property files required by the ant to build an APK for android.
        • We only need the local.properties and build.xml for our project. So you can go ahead and delete the proguard.cfg.

      • Compile and run project in visual studio 2010:
        • Open your Win32 solution file (simpleTextDisplay.sln in this case).
        • Right click on the solution and click on “Configuration Manager



        • In the Configuration Manager window, click on the dropdown for “Active solution platform” and select New.

        • In the New Solution Platform window use Android as the new platform, Empty for Copy setting and check the box which says “Create new project platform”. Press OK and Close respectively.

        • Now right click on the project (simpleTextDisplay in this case) and select properties.

        • When we build projects in visual studio, the compiler and linker generates many intermediate and output files during the process of project building. Normally the default location is the same directory where the visual studio project file is present. However, for android I like to be organized in a separate folder (AndroidProject in this case). I like to do it because it keeps the all android related stuff in one folder. 
        So, we have to change few places in the Property page:
          • Under General tab, change Output Directory and Intermediate Directory directing it to the intended folder.

          • Under Debugging tab, change the Working Directory directing it to the intended folder.

          • And lastly, under Ant Build tab, change the Ant Build Root Path to the directory where build.xml is present. This is very important to change this directing to the folder containing your build.xml. This is because when you compile the build, visual studio would invoke ant telling it where to look for the build.xml and start building APK.

          • Upon changing the above values, press Apply and OK.
      • The last step is to add all the jni source files into the visual studio project. Go to <trunk>\AndroidProject\jni, drag and drop all the required files (NativeJavaLink.cpp in this case) in the folder into the visual studio project.

      • Now, 
        • Connect your android device to the computer using USB making sure it has the USB debugging option enabled.
        • Click on Debug from the task bar and select Build Solution. It should compile and run the project, displaying the text from the CPP file.
        • You can download the project with all the above steps here.

      Note: I had an issue in compiling related to JVM “could not create the Java virtual machine” and went about fixing by following the this link(comment 6).



      << Previous Step            Home            Next Step >>

      Friday, October 28, 2011

      Android using Visual Studio 2010 : Step 02 :: Setup Environment


      Let's setup our computer environment to start working. 

      Install Java
      Note: Make sure that you download the Windows x86 version as Google only supports this version and not Windows x64 one.

      Tips: I installed  Java SE 6 Update 29 and I prefer installing JDK to "C:\jdk1.6.0_27" (a path with no space in the name) rather then "C:\Program Files\Java\jdk1.6.0_27". This is because sometime the space in the name "Program Files" causes issues. However, the JRE accompanying JDK should be installed to "C:\Program Files\Java". So in short;
        •  Install JDK in "C:\jdk1.6.0_27

        • Install JRE in "C:\Program Files\Java\jre6"

       

      Setup Android SDK
      • Download and install the latest SDK (the installer version is recommended).
      • Make sure that you run "SDK Manager" to download the latest packages.
      Note: The installer version is the preferred because it is easy and allows you to downloads the latest packages. I normally download all the packages; it takes time to download around 2GB but once you download it, you can just copy and paste it anywhere so its worth it.

      Tips: I like to keep the developer's tools organized, so i install to single root folder as shown below(in my case E:\_DEV_TOOLS). I installed all the android related installations in this folder. It is not necessary to have the folder in the primary drive.


        Setup Android NDK
        • Download the latest NDK.
        • Unzip it to any location(in my case i did it to my _DEV_TOOLS folder), make sure the path has no space in the names.


        Setup ANT
        • Download the latest ANT.
        • Unzip it to any location(in my case i did it to my _DEV_TOOLS folder), make sure the path has no space in the names.


        Install Visual Studio 2010
        Note: You can either buy VS2010 or install the expression edition, both should work. This tutorial will based on Visual Studio 2010 Express Edition.


        Install vs-android
        vs-android is intended to provide a collection of scripts and utilities to support integrated development of Android NDK C/C++ software under Microsoft Visual Studio.

        • Download the latest vs-android release.
        • Unzip it anywhere you like as the installation process copies the necessary files from it. Once you are done with the installation, you can go ahead and delete the unzipped folder. It is no longer required.
        • Go into the "MsBuild" directory of the folder you just unzipped.
        • For Windows 2000 or XP, just double click on 'install.cmd'.
        • For Vista or Windows 7 you will need to right click 'install.cmd', and choose to 'Run as Administrator". Don't worry, you can view the install batch file in notepad if you wish. All it does is copy files to the correct place in your Visual Studio install.
        • Feel free to delete the files you unzipped earlier. They are no longer required.


        Setup Environment Variables
        Now that we have installed everything that we intended to, let us set the environment variables to finalize the setup process.

        • Open Environment Variable Window:
          • From the desktop, right-click My Computer and click Properties.
          • In the System Properties window, click on the Advanced tab.
          • In the Advanced section, click the Environment Variables button.
          • Environment Variables window should appear now.

        • Create an environment variable named ANDROID_HOME and it should point to the root directory of Android SDK (in my case it is E:\_DEV_TOOLS\android-sdk-windows).

        • Create second environment variable named ANDROID_NDK_ROOT and it should point to the root directory of Android NDK (in my case it is E:\_DEV_TOOLS\android-ndk-r6b).

        • Create third environment variable named ANT_HOME and it should point to the root directory of ANT (in my case it is E:\_DEV_TOOLS\apache-ant-1.8.2)

        • Similarly, create a forth environment variable named JAVA_HOME and it should point to the root directory of JDK (in my case it is C:\jdk1.6.0_27)

        • Finally, look for an environment variable named Path and double click on it. You would notice that there are other path mentioned under "Variable value". Add the blow mentioned part at the end of it;
          • ;%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\tools;%ANDROID_NDK_ROOT%\build;%JAVA_HOME%\bin;%ANT_HOME%\bin
        The last step concluded the things you need to do to setup you computer environment. Now you can use visual studio to compile and run you NDK projects.





        << Previous Step            Home            Next Step >>

        Android using Visual Studio 2010: Step 01: : Introduction


        A basic tutorial for integrating Android building process into Visual Studio so that you can compile and run android projects with NDK (one with native codes, C/CPP). It was a huge relief to me as i am quite comfortable with visual studio and did not want to get into the command prompt mode to port my CPP projects into android. Now, you can compile and run all your NDK projects using visual studio 2010.

        Must haves:
        Note: Cygwin and Eclipse are not required to compile NDK projects using Visual Studio 2010.

        Information was gathered from the following links and a bit of googling:



        Home            Next Step >>