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.
- You can read more about JNI here.
- 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.
- 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 >>















