Menu

170817Ago - CHAPTER 1

170817Ago - CHAPTER 1

Hello, welcome to my Android Programming Blog.

We are going to develop an example application with several functionalities to learn about this incredible platform... enjoy!!!

This blog is dedicated to:
- My cousin Esther Tejada R.I.P. and her premature daughter.
- My best friend Rocio Sancho.
- My mother Sensi.

I hope it'll useful for you if you are new in Android developing. Note that you must have previous programming experience because is for programmers.

Why Android apps for programmers? Well there are a lot of for normal programming. We, programmers need apps too, but we think in a different form to typical users.

Programmers wait very, very customizable apps, fast and eficient code, sources, several views for the same content, sophisticated interfaces, logs, time meditations, etc. Normal users hate all of this.

A virus problem destroyed my previous platform and I lost my first Android apps.

Now, I taked some security steps:

  • A new portatil Intel 7HQ, 16 Gb RAM, 1 T HD.
  • Windows 10 OEM in the computer.
  • No software installed apart of Oracle VirtualBox: No software - No virus!
  • No permanent internet conexión: Only for updates and downloads concreted.
  • One master virtual box machine: 8Gb RAM, 4 Processors.
  • New Windows10 (with his license) for the master machine.
  • Several cloned machines with his own MAC address, cloned from master machine.
  • Android Studio installed in a clone. Easy to check, clean and do backups.

This new home development platform I hope that resist at least for write again my Android apps and this blog.

First of all we go with the first application. Android Studio installed and updated.

How I have Android Studio running in a Virtual Box machine over a computer, I can't to activate ADB to debug the applications in emulated devices.

I can explain this: to run a windows10 64bits on a windows10 64 bits you need Hyper-Vt (hyper virtualization enabled)...

But to run ADB emulation you need disable Hyper-Vt because aren't compatible.

That is I can use the emulator and I must test the application generating the apk, copying it into a phone (I'm using a Samsung Galaxy J1) and installing the apk from the file explorer.

Too, I can use the "ADB Run in external device" from Android Studio, but not in the emulator.

Of course that the phone have the Developer Options enabled and unlock the security for installed applications from apks instead from Google Play.

Well, we begin with "New Project":

Application name: MySuperApp
Company domain: androidblog.dhtejada.homebrew
Package name: homebrew.dhtejada.androidblog.mysupperapp
Include C++ support (ENABLED)
Project location: ...AndroidStudioProjects\MySuperApp

I love C++ programming and I'll use it all that I'll can, is beautiful and very fast!!!

In the newst wizard page we must to check Phone and Tablet. We go to use at first the Minimun SDK that came, in my case: API15: Android 4.0.3 (IceCreamSandwich)

We click on Next and we go to select an "Empty Activity" option with data:

Activity Name: MainActivity
Checked: Generate Layout File
Layout name: activity_main
Checked: Backwards Compatibility (AppCompat)

Click Next and select C++ options:

C++ standard: Toolchain Default. (If more forware we like programming in C++ 11 we'll change this)

Checked: Exceptions Support (-fexceptions)
Checked Runtime Type Information Support (-frtti)

And we go to the IDE for begin to program: Gradle begin to build our default code generated by the wizard. This first build perhaps fails if you hav'nt installed the NDK support, but you only needs click in the message to Download NDK. Perhaps too you'll need the CMake utility download.

Remember that NDK is the API to program in C++ for Android. Remeber too that Android platform is a base layer programmed in C/Linux, and an Java layer over that run the apps normaly programmed in Java.

But with NDK you can to program some modules in C++ and this is more fast than java and you can do calls to the C/Linux base layer and to access directly to the hardware (micro, memory, etc)

When you have all necessary downloads, in the menu Build the option "Rebuild project" will run ok: Gradle to configure the project and build the application.

If you run it in an emulator or install it in a phone (you must use the option Build
APK first), you'll see the app: A blank screen with the label "Hello from C++" and a blue title bar with the name of the app "MySuperApp".

To run directly in the phone from the Android Studio IDE. You must to have conected the phone in a USB port and to have installed the USB Driver for Android Studio. This is different for each phone maker, in my case I use: SAMSUNG_USB_Driver_for_Mobile_Phones_v1.5.45.00.exe

I you have not this driver, your phone don't appear in the Connected devices list in the ADB launcher:

The apk file is generated in the folder: ...AndroidStudioProjects\MySuperApp\app\build\outputs\apk normally as "app-debug.apk", you can copy to the phone and install it.

We go to see the Project Folders: App tree (with sources) and Gradle Scripts (to build and compile)

Into the app directory tree we have:

  • The manifests folder with the AndroidManifest.xml (That explain the requirements for our app). In this moment we must paid attention to the line: <activity android:name=".MainActivity"> because is were we say to Android which is the main activity java class for our app.

  • The java tree has the typical java sources packages/classes.

  • The C++ folder has the c++ sources. in this case "native-lib.cpp" that return the string "Hello from C++" to be shown in the screen.
  • The res tree has the resource folders, that is very important in Android development.

Into the Gradle Scripts we have several scripts but now we can see:

build.gradle: This is the main build script, is important the dependencies block because if we use libraries we must to include here, if not the compiler cannot find the libs.

Well we go to our main activity class file into the app\java folder MainActivity.java

Is a class inherited from Activity. This is the main class for an application (similar to main class in a normal java program).

Think that an Android app is maked with Activitys, you can have several in the same application and launch ones from others. But you must be carefull with a thing: Each Activity has his own memory space and YOU CAN NOT TO USE AN OBJECT OF AN ACTIVITY FROM OTHER, or you will have a memory exception!!!

But you can pass data from an Activity to others activities using the correct API calls for that, we will see this more later.

Posted by David Hernandez Tejada 2017-08-24 Labels: ANDROID STUDIO ANDROID DEVELOPMENT PROGRAMMING

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.