Developing android operating system

Get started with native Android development on Windows

This guide will get you started using Windows to create native Android applications. If you would prefer a cross-platform solution, see Overview of Android development on Windows for a brief summary of some options.

The most straight-forward way to create a native Android app is using Android Studio with either Java or Kotlin, though it is also possible to use C or C++ for Android development if you have a specific purpose. The Android Studio SDK tools compile your code, data, and resource files into an archive Android package, .apk file. One APK file contains all the contents of an Android app and is the file that Android-powered devices use to install the app.

Install Android Studio

Android Studio is the official integrated development environment for Google’s Android operating system. Download the latest version of Android Studio for Windows.

  • If you downloaded an .exe file (recommended), double-click to launch it.
  • If you downloaded a .zip file, unpack the ZIP, copy the android-studio folder into your Program Files folder, and then open the android-studio > bin folder and launch studio64.exe (for 64-bit machines) or studio.exe (for 32-bit machines).

Follow the setup wizard in Android Studio and install any SDK packages that it recommends. As new tools and other APIs become available, Android Studio will notify you with a pop-up, or check for updates by selecting Help > Check for Update.

Create a new project

Select File > New > New Project.

In the Choose your project window, you will be able to choose between these templates:

Basic Activity: Creates a simple app with an app bar, a floating action button and two layout files: one for the activity and one to separate out text content.

Empty Activity: Creates an empty activity and a single layout file with sample text content.

Bottom Navigation Activity: Creates a standard bottom navigation bar for an activity. For more information on this, see the Bottom Navigation Component section of the Material Design guidelines by Google.

Templates are commonly used to add activities to new and existing app modules. For example, to create a login screen for your app’s users, add an activity with the Login Activity template. To learn more about selecting an activity and how to add code from a template, see Android Developer guide by Google.

The Android operating system is based on the idea of components and uses the terms activity and intent to define interactions. An activity represents a single, focused task that a user can do. An activity provides a window for building the user interface using classes based on the View class. There is a lifecycle for activities in the Android operating system, defined by six callbacks: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() . The activity components interact with one another using intent objects. Intent either defines the activity to start or describes the type of action to perform (and the system selects the appropriate activity for you, which can even be from a different application). Learn more about Activities, the Activity Lifecycle, and Intents in the Android Developer guide by Google.

Java or Kotlin

Java became a language in 1991, developed by what was then Sun Microsystems, but which is now owned by Oracle. It has become one of the most popular and powerful programming languages with one of the largest support communities in the world. Java is class-based and object-oriented, designed to have as few implementation dependencies as possible. The syntax is similar to C and C++, but it has fewer low-level facilities than either of them.

Kotlin was first announced as a new open-source language by JetBrains in 2011 and has been included as an alternative to Java in Android Studio since 2017. In May 2019, Google announced Kotlin as it’s preferred language for Android app developers, so despite being a newer language, it also has a strong support community and has been identified as one of the fastest growing programming languages. Kotlin is cross-platform, statically typed, and designed to interoperate fully with Java.

Java is more widely used for a broader range of applications and offers some features that Kotlin does not, such as checked exceptions, primitive types that are not classes, static members, non-private fields, wildcard-types, and ternary-operators. Kotlin is specifically designed for and recommended by Android. It also offers some features that Java does not, such as null references controlled by the type system, no raw types, invariant arrays, proper function types (as opposed to Java’s SAM-conversions), use-site variance without wildcards, smart casts, and more. Find a more in-depth look at the comparison to Java in the Kotlin documentation.

Minimum API Level

You will need to decide the minimum API level for your application. This determines which version of Android your application will support. Lower API levels are older and therefore generally support more devices, but higher API levels are newer and therefor provide more features.

Select the Help me choose link to open a comparison chart showing the device support distribution and key features associated with the platform version release.

Instant app support and Androidx artifacts

You may notice a checkbox to Support instant apps and another to Use androidx artifacts in your project creation options. The instant apps support is not checked and the androidx is checked as the recommended default.

Google Play Instant apps provide a way for people to try an app or game without installing it first. These instant apps can be surfaced across the Play Store, Google Search, social networks, and anywhere you share a link. By checking the Support instant apps box, you are asking Android Studio to include the Google Play Instant Development SDK with your project. Learn more about Google Play Instant apps in the Android developer guide.

AndroidX artifacts represents the new version of the Android support library and provides backwards-compatibility across Android releases. AndroidX provides a consistent namespace starting with the string androidx for all available packages.

AndroidX is now the default library. To uncheck this box and use the previous support library requires removing the lastest Android Q SDK. See Uncheck use Androidx artifacts on StackOverflow for instructions, but first note that the former Support Library packages have been mapped into corresponding androidx.* packages. For a full mapping of all the old classes and build artifacts to the new ones, see Migrating to AndroidX.

Project files

The Android Studio Project window, contains the following files (be sure that the Android view is selected from the drop-down menu):

app > java > com.example.myfirstapp > MainActivity

The main activity and entry point for your app. When you build and run your app, the system launches an instance of this Activity and loads its layout.

app > res > layout > activity_main.xml

The XML file defining the layout for the activity’s user interface (UI). It contains a TextView element with the text «Hello World»

Читайте также:  Драйвера для андроид фоне

app > manifests > AndroidManifest.xml

The manifest file describing the fundamental characteristics of the app and each of its components.

Gradle Scripts > build.gradle

There are two files with this name: «Project: My First App», for the entire project, and «Module: app», for each app module. A new project will initially only have one module. Use the module’s build.file to control how the Gradle plugin builds your app. Learn more about how to configure your build in the Android developer guide.

Use C or C++ for Android game development

The Android operating system is designed to support applications written in Java or Kotlin, benefiting from tooling embedded in the system’s architecture. Many system features, like Android UI and Intent handling, are only exposed through Java interfaces. There are a few instances where you may want to use C or C++ code via the Android Native Development Kit (NDK) despite some of the associated challenges. Game development is an example, since games typically use custom rendering logic written in OpenGL or Vulkan and benefit from a wealth of C libraries focused on game development. Using C or C++ might also help you squeeze extra performance out of a device to achieve low latency or run computationally intensive applications, such as physics simulations. The NDK is not appropriate for most novice Android programmers however. Unless you have a specific purpose for using the NDK, we recommend sticking with Java, Kotlin, or one of the cross-platform frameworks.

To create a new project with C/C++ support:

In the Choose your project section of the Android Studio wizard, select the Native C++* project type. Select Next, complete the remaining fields, then select Next again.

In the Customize C++ Support section of the wizard, you can customize your project with the C++ Standard field. Use the drop-down list to select which standardization of C++ you want to use. Selecting Toolchain Default uses the default CMake setting. Select Finish.

Once Android Studio creates your new project, you can find a cpp folder in the Project pane that contains the native source files, headers, build scripts for CMake or ndk-build, and prebuilt libraries that are a part of your project. You can also find a sample C++ source file, native-lib.cpp , in the src/main/cpp/ folder which provides a simple stringFromJNI() function returning the string «Hello from C++». Additionally, you should see a CMake build script, CMakeLists.txt , in your module’s root directory required for building your native library.

To learn more, about adding C and C++ code to your project, see the Android developer guide. To find Android NDK samples with C++ integration, see the Android NDK samples repo on GitHub. To compile and run a C++ game on Android, use the Google Play Game services API.

Design guidelines

Device users expect applications to look and behave a certain way. whether swiping or tapping or using voice-controls, users will hold specific expectations for what your application should look like and how to use it. These expectations should remain consistent in order to reduce confusion and frustration. Android offers a guide to these platform and device expectations that combines the Google Material Design foundation for visual and navigational patterns, along with quality guidelines for compatibility, performance, and security.

Fluent Design System for Android

Microsoft also offers design guidance with the goal of providing a seamless experience across the entire portfolio of Microsoft’s mobile apps.

Fluent Design System for Android design and build custom apps that are natively Android while still uniquely Fluent.

Источник

Developing for Android — An Introduction

What is Android?

Android is the world’s most popular operating system for mobile devices and tablets. It is an open source operating system, created by Google, and available to all kinds of developers with various expertise levels, ranging from rookie to professional.

(The term ‘open source’ sounds pretty familiar, doesn’t it? Well, open-source means software with source available for modification and bound to an open source license agreement. More about open source terminology can be found here).

From a developer’s perspective, Android is a Linux-based operating system for smartphones and tablets. It includes a touch screen user interface, widgets, camera, network data monitoring and all the other features that enable a cell phone to be called a smartphone. Android is a platform that supports various applications, available through the Android Play Store. The Android platform also allows end users to develop, install and use their own applications on top of the Android framework. The Android framework is licensed under the Apache License, with Android application developers holding the right to distribute their applications under their customized license.

Like most software, Android is released in versions. Google has also assigned names to its versions since April 2009. Below are all the versions of Android released to date:

Version No. Name For:
1.0 Android Beta Phone
1.1 Android Phone
1.5 Cupcake Phone
1.6 Donut Phone
2.0/2.1 Eclair Phone
2.2.x Froyo Phone
2.3.x Gingerbread Phone
3.x Honeycomb Tablet
4.0.x Ice Cream Sandwich Phone and Tablet
4.1/4.2 Jelly Bean Phone and Tablet

As we can see in the table above, various versions of Android are supported on phones and tablets. There are umpteen Android devices available in the market from manufacturers like Samsung, HTC, Motorola and others. Google itself also has phones made in conjunction with OEMs, branded as the Nexus series.

Understanding Android

To begin development on Android even at the application level, I think it is paramount to understand the basic internal architecture. Knowing how things are arranged inside helps us understand the application framework better, so we can can design the application in a better way.

Android is an OS based on Linux. Hence, deep inside, Android is pretty similar to Linux. To begin our dive into the Android internals, let us look at an architectural diagram.

The above diagram illustrates the Android architecture. As you can see, it is a software stack above the hardware that is provided by the OEMs. Let’s start with the topmost layer, i.e., the applications.

Applications

The diagram shows four basic apps (App 1, App 2, App 3 and App 4), just to give the idea that there can be multiple apps sitting on top of Android. These apps are like any user interface you use on Android; for example, when you use a music player, the GUI on which there are buttons to play, pause, seek, etc is an application. Similarly, is an app for making calls, a camera app, and so on. All these apps are not necessarily from Google. Anyone can develop an app and make it available to everyone through Google Play Store. These apps are developed in Java, and are installed directly, without the need to integrate with Android OS.

Application Framework

Scratching further below the applications, we reach the application framework, which application developers can leverage in developing Android applications. The framework offers a huge set of APIs used by developers for various standard purposes, so that they don’t have to code every basic task.The framework consists of certain entities; major ones are:

This manages the activities that govern the application life cycle and has several states. An application may have multiple activities, which have their own life cycles. However, there is one main activity that starts when the application is launched. Generally, each activity in an application is given a window that has its own layout and user interface. An activity is stopped when another starts, and gets back to the window that initiated it through an activity callback.

Читайте также:  Android studio масштабирование шрифтов

This manager enables the applications to create customized alerts

Views are used to create layouts, including components such as grids, lists, buttons, etc.

Applications do require external resources, such as graphics, external strings, etc. All these resources are managed by the resource manager, which makes them available in a standardized way.

Applications also share data. From time to time, one application may need some data from another application. For example, an international calling application will need to access the user’s address book. This access to another application’s data is enabled by the content providers.

Libraries

This layer holds the Android native libraries. These libraries are written in C/C++ and offer capabilities similar to the above layer, while sitting on top of the kernel. A few of the major native libraries include

  • Surface Manager: Manages the display and compositing window-ing manager. — Media framework: Supports various audio and video formats and codecs including their playback and recording.
  • System C Libraries: Standard C library like libc targeted for ARM or embedded devices.
  • OpenGL ES Libraries : These are the graphics libraries for rendering 2D and 3D graphics.
  • SQLite : A database engine for Android.

Android Runtime

The Android runtime consists of the Dalvik Virtual Machine. It is basically a virtual machine for embedded devices, which like any other virtual machine is a bytecode interpreter. When we say it is for embedded devices, it means it is low on memory, comparatively slower and runs on battery power. Besides the Dalvik Virtual Machine, it also consists of the core libraries, which are Java libraries and are available for all devices.

Kernel

The Android OS is derived from Linux Kernel 2.6 and is actually created from Linux source, compiled for mobile devices. The memory management, process management etc. are mostly similar. The kernel acts as a Hardware Abstraction Layer between hardware and the Android software stack.

Android SDK

As already mentioned, Android is open source and hence the source code is available for all developers. In totality it is called the Android SDK. You can download, build and work on Android in a number of different ways—it all depends on what you want to do. If your goal is to develop an Android application, you don’t necessarily need to download all the source. Google recommends the Eclipse IDE, for which there is an available Android Developer Tools (ADT) plugin, through which you can install the specific SDK, create projects, launch emulators, debug, etc. You can see more details of Eclipse and ADT through Android’s official website for developers — http://developer.android.com/sdk/index.html

Android Development for Windows Users

Android as of now does not support building on Windows, so if you want to modify the Android OS itself, you’ll have to use Linux (see building the Android OS). However, on Windows, we do have tools and plugins for application and native Android development. And here we will talk about setting up basic Android development tools on Windows.

Downloading the Android SDK and developer tools

Google provides a convenient bundle to download and setup Android for Windows developers, which you can download here, under the name ADT bundle for Windows. The exact name of the file you download will depend on your OS architecture (32 vs 64 bit), but for my case (64 Bit Win 7), I see the following zip file downloaded: adt-bundle-windows-x86_64.zip. Extracting the zip file, I have contents as in the following snapshot:

First of all, we have Eclipse, which is the IDE for writing source. As an IDE it provides the environment for developing Android Applications. Android Applications are developed primarily in Java. Next we have the ‘sdk’, which does not include any of the source. However, it holds the already built platform tools, tools, images and some platform specific libraries. When we say, building Android is not supported on Windows,we mean that we can’t compile system images and tools. However, other sources needed for application development are be available through the SDK Manager, which is the third entity present in the extracted zip file.

So, let’s download the sources! Double click the SDKManager.exe. You’ll see something like this:

This is the SDK Manager, via which we can install or delete whatever version of SDK we want. As you can see, it mentions we have the Android SDK Tools and Android SDK Platform Tools installed.

The latest Android available, as of the writing of this article, is 4.2, but with the SDK we can download and install any of the previous versions too. Now let us play around with the latest Android—i.e.,4.2, which is also known as Jelly Bean.

Select the check box for «Android 4.2(API 17)», which will select everything required for and under Android 4.2.

In all, as we can see, SDK Manager found 6 packages that need installation. Click the «Install 6 packages» button. We see another dialog box for package descriptions and license.

Select «Accept All» and click Install, which will initiate the download and then installation. When done, you will see «Installed» in front of all the packages selected.

It’s now time to launch Eclipse, but first, we need to make sure that we have the Java Developer Kit (JDK) installed. If you don’t have it, you can download it from Oracle here. I have JDK 7 installed in my case. Next, we need to launch Eclipse from the executable present in the Eclipse directory . If we obtained Eclipse via the instructions in this article, it should already have the ADT plugin installed. (Otherwise, you can download the ADT separately by following the instructions here.) Eclipse generally asks for a workspace path where it will create and maintain projects.

Here is what the newly launched Eclipse looks like:


Eclipse also includes the SDK Manager from where you can manage the SDK packages. Check out the Window menu and select «Android SDK Manager».

To see what the emulator looks like, go to the Window menu and select «Android Virtual Device Manager». From there, we can create our virtual device or use one of the standard devices available. To create a new virtual device, click on ‘New’ as we see in the following snapshot:


Another window will pop up to take inputs for device type, target processor, memory, etc.


You can provide customized device specifications for a virtual device. Once the device is created by clicking ‘OK’, it will be available in the list on «Android Virtual Device Manager» window. To launch the emulator for your own defined virtual device, select it and click «Start».

You can also select pre-loaded options that correspond to the specifications of existing Android devices. To see them in the Android Virtual Device Manager, go to the Device Definitions tab.

We select the first in our list, «Nexus S by Google», and add a virtual device by clicking «Create AVD». The following dialog box requires the «Target» and «CPU» to be specified along with the size of the SD Card.

We will assign Target as «Android 4.2 — API level 17» CPU as «ARM» and SD Card Size equals «1024 MiB» and click «OK». We can now see the newly defined virtual device in the AVD list

Читайте также:  Три богатыря ход конем полная версия андроид

To launch the emulator, select it and click «Start». Here’s what the emulator looks like:

We can now use our newly-created emulator for running our Windows Android apps. All we have to do is compile our code, then load the app onto the emulator.

Building the Android OS

The above instructions, for Windows users, will work great if all you want to do is create Android apps. However, you can do even more with Android, including modifying the Android OS to create Android ROMs or MODs—that’s the beauty of open source! To do this, you’ll need to download the complete available Android source from its repository and cross-compiling for the device. You’ll also need to be using Linux, since building Android is not supposed on Windows machines. And before we move further, there are certain assumptions to get out of the way:

1. All the information is generic and should work for all Linux flavours, however these instructions have only been tried and tested on Ubuntu 11.04.

2. Because we had to pick one of the versions of Android, we chose Android 4.0.1. Hence, some commands might be specific to Android 4.0.1, but things might be slightly different for other versions.

Memory Requirements

It is always a wise idea to check the memory requirements before starting any project. The size of the Android SDK is around 8.5GB and you will need around 30GB free disk space to build it.

Prerequisite Installations

Prior to downloading the SDK and starting cross-compiling, there are certain prerequisites of the Android SDK we have to have. It is better to set up these before jumping into the Android SDK, as we all know prevention is much better than debugging! First of all, for the Android version we have chosen (i.e Android 4.0.1) cross compilation is well tested on 64 -bit machines, but the documentation says it is experimental on 32-bit machines.

1. JDK Android SDK building requires the JDK, so you must install the JDK. For Android versions 2.3.x and later, one needs to install Java 6. It is recommend to install Sun JDK only, rather than Open JDK.. First, download the JDK 6. Then run the following commands. (Please Note: To avoid specifying a specific version, the installer binary name and directories are modified to be generic names.) For versions prior to 2.3.x, Java 5 is required.

2. You also need to install the following packages: These packages are needed for downloading and compiling the SDK.

Once these get installed, you are ready to download the Android SDK. There may be some optional things to do, for example enabling caching, etc. But we will not go into much detail here as we don’t really need to do those things.

Downloading the Android SDK

The entire Android SDK is stored in a git repository that also maintains various other versions as well. We need to know the repository path and then download to a separate directory where we wish to store the complete source.

Let us first create a root source directory.

Further, we will initialise and configure a tool ‘repo’, which is used to work with git conveniently. To download it, we need a ‘bin’ directory in our home, which should be added to the path.

Now we have the repo installed and must initialise it with the path of git where the Android SDK lies. There is a master branch of git, which holds the latest Android version. The master branch in git is the main source repository, which means any new release or new update would be part of this master branch. However, if we want a specific Android version, we need to initialise with a specific branch.

Here in this article, we will go for a specific branch, since the master will keep on changing.

It will ask you for a name and email id, and you must provide them.

To get all the Android SDK files, you just now need to do:

It may take some time to download the complete source. In the end, we get

This indicates the sources are downloaded. Let’s have a look at the folder structure from the topmost level; here is what I have:

Building Android

Now that we have the source, the next step is to build it. The first thing we need to determine before building is what platform are we going to run this Android on? It is essential to determine this. The options could be the type of your device, or an emulator.

When flashing a phone, the worst case it that you may brick the phone in the event of a critical bug. As a result, it is a good idea to develop on Android through an emulator—so we’ll do that.

First of all, we need to set the environment variables, which are specified in a shell script. Next, we need to specify the target for which we need to cross-compile, using the command ‘lunch’. But first of all, what is cross-compiling and why we need to do it? Well, cross-compiling is a compiling process that is done for a different platform, to create an executable or library to be used on a that different platform. Therefore, we would have two platforms,

  • host : On which we have the sources and are being cross-compiled. In our case, this is our Linux machine.
  • target: The platform for which the sources are being compiled for, and the compiled image or library will be used on the target platform. In this case, this would be our Android emulator.

This is required for embedded systems, as not all platforms support compiling and debugging. Examples of such platforms are ARM, MIPS, etc.

For the target emulator and development build, we do Here ‘full’ is for emulator and ‘eng’ is because we want a development build.

To see other available options, do:

Next, we just have to build the code using ‘make’. GNU make has the ability to run parallel tasks. How many tasks to run in parallel is determined by the ‘-j’ option. The usage is make -jN, where N is the number For example, to run 4 parallel tasks, our command would be

It is common to set this number between 1 to 2 times the number of hardware threads the computer supports, which is generally the number of processors. To know how many processors we have in our system, we need to peek into the cpuinfo. To get the CPU info, we have command However, to get to know the number of processors in a single command, we can use

The building of Android also takes quite a lot of time when done for the first time. Note, all the images, libraries and applications built are placed in the directory out/target/product/generic/

Running on the Emulator

To launch the emulator with our built Android, just run the following command

This will automatically pick the built images and binaries for the recently built Android. You can have the look and feel of the emulator environment, which is pretty similar to a device.

Conclusion

This article covers the basics to understand Android, and a pragmatic approach to understanding the SDK and get it working after building it. In future articles, we’ll talk more about the details of development in Android for native libraries and apps including how to write a native Android application.

Источник

Оцените статью