Android

15 Notes
+ Difference between min SDK version/target SDK version vs. compile SDK version? (Jan. 28, 2023, 11:54 p.m.)

The min SDK version is the earliest release of the Android SDK that your application can run on. Usually, this is because of a problem with the earlier APIs, lacking functionality, or some other behavioral issue. The target SDK version is the version your application was targeted to run on. Ideally, this is because of some sort of optimal run conditions. If you were to make your app for version 19, this is where that would be specified. It may run on earlier or later releases, but this is what you were aiming for. This is mostly to indicate how current your application is for use in the marketplace, etc. The compile SDK version is the version of Android your IDE (or other means of compiling I suppose) uses to make your app when you publish an APK file. This is useful for testing your application as it is a common need to compile your app as you develop it. As this will be the version to compile to an APK, it will naturally be the version of your release. Likewise, it is advisable to have this match your target SDK version. ------------------------------------------------------------------------------------------------- The formula is minSdkVersion <= targetSdkVersion <= compileSdkVersion -------------------------------------------------------------------------------------------------

+ Fix Cleartext Traffic Error in Android 9 Pie (April 2, 2020, 12:27 a.m.)

1- Add a network security config file under res/xml: res/xml/network_security_config.xml 2- Add a domain config and set cleartextTrafficPermitted to "true": <?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">your_domain.com</domain> </domain-config> </network-security-config> 3- Add your network security config to your Android manifest file under application: <application android:name=".MyApplication" android:networkSecurityConfig="@xml/network_security_config" </application>

+ AndroidX (Nov. 8, 2019, 5:37 p.m.)

AndroidX and the Android Support Library cannot live side-by-side in the same Android project - doing so will lead to build failures. AndroidX (Jetpack) is the successor to the Android Support Library.

+ AndroidX / Jetifier (Nov. 8, 2019, 5:30 p.m.)

android.useAndroidX: When set to true, this flag indicates that you want to start using AndroidX from now on. If the flag is absent, Android Studio behaves as if the flag were set to false. android.enableJetifier: When set to true, this flag indicates that you want to have tool support (from the Android Gradle plugin) to automatically convert existing third-party libraries as if they were written for AndroidX. If the flag is absent, Android Studio behaves as if the flag were set to false. -------------------------------------------------------------------------- Jetifier tool migrates support-library-dependent libraries to rely on the equivalent AndroidX packages instead. The tool lets you migrate an individual library directly, instead of using the Android Gradle plugin bundled with Android Studio. For Example: If you have PhotoView.java in your dependency. That uses support library AppCompatImageView. ` import android.support.v7.widget.AppCompatImageView;` This class is moved now to androidx package, so how will PhotoView get androidx AppCompatImageView? And the app still runs in the device. Who made this run? Jetifier, which converts all support packages of dependency at build time. Jetifier will convert android.support.v7.widget.AppCompatImageView to androidx.appcompat.widget.AppCompatImageView while building the project. Conclusion Enabling Jetifier is important when you migrate from Support Libraries to AndroidX. --------------------------------------------------------------------------

+ Animations (June 14, 2019, 1:06 p.m.)

https://android.googlesource.com/platform/frameworks/base/+/HEAD/core/res/res/anim

+ Platform codenames, versions, API levels, and NDK releases (May 26, 2019, 9:31 p.m.)

Codename Version API level/NDK release Pie 9 API level 28 Oreo 8.1.0 API level 27 Oreo 8.0.0 API level 26 Nougat 7.1 API level 25 Nougat 7.0 API level 24 Marshmallow 6.0 API level 23 Lollipop 5.1 API level 22 Lollipop 5.0 API level 21 KitKat 4.4 - 4.4.4 API level 19 Jelly Bean 4.3.x API level 18 Jelly Bean 4.2.x API level 17 Jelly Bean 4.1.x API level 16 Ice Cream Sandwich 4.0.3 - 4.0.4 API level 15, NDK 8 Ice Cream Sandwich 4.0.1 - 4.0.2 API level 14, NDK 7 Honeycomb 3.2.x API level 13 Honeycomb 3.1 API level 12, NDK 6 Honeycomb 3.0 API level 11 Gingerbread 2.3.3 - 2.3.7 API level 10 Gingerbread 2.3 - 2.3.2 API level 9, NDK 5 Froyo 2.2.x API level 8, NDK 4 Eclair 2.1 API level 7, NDK 3 Eclair 2.0.1 API level 6 Eclair 2.0 API level 5 Donut 1.6 API level 4, NDK 2 Cupcake 1.5 API level 3, NDK 1 (no codename) 1.1 API level 2 (no codename) 1.0 API level 1

+ Action Bar, Toolbar, App Bar (May 26, 2019, 7:47 p.m.)

Toolbar is a generalization of the Action Bar pattern that gives you much more control and flexibility. Toolbar is a view in your hierarchy just like any other, making it easier to interleave with the rest of your views, animate it, and react to scroll events. You can also set it as your Activity’s action bar, meaning that your standard options menu actions will be display within it. In other words, the ActionBar now became a special kind of Toolbar. The app bar, formerly known as the action bar in Android, is a special kind of toolbar that is used for branding, navigation, search, and actions. -------------------------------------------------------------------- Toolbar provides greater control to customize its appearance unlike old ActionBar. It fully supported Toolbar features to lower android os devices via AppCompact support library. Use a Toolbar as an replacement to ActionBar. In this you can still continued to use the ActionBar features such as menus, selections, etc. Use a standalone Toolbar, whereever you want to place in your application. -------------------------------------------------------------------- Toolbar’s are more flexible than ActionBar. We can easily modify its color, size and position. We can also add labels, logos, navigation icons and other views in it. In Material Design Android has updated the AppCompat support libraries so that we can use Toolbar’s in our devices running API Level 7 and up. --------------------------------------------------------------------

+ XML - Introduction (April 25, 2019, 12:14 p.m.)

XML describes the views in your activities, and Java tells them how to behave.

+ Common naming conventions for icon assets (April 22, 2019, 2:32 a.m.)

Asset Type Prefix Example Icons ic_ ic_star.png Launcher icons ic_launcher ic_launcher_calendar.png Menu icons and Action Bar icons ic_menu ic_menu_archive.png Status bar icons ic_stat_notify ic_stat_notify_msg.png Tab icons ic_tab ic_tab_recent.png Dialog icons ic_dialog ic_dialog_info.png

+ Android Studio - Transparent Background Launcher Icon (April 22, 2019, 1:21 a.m.)

1- File > New > Image Asset. 2- Turn to Launcher Icons (Adaptive and Legacy) in Icon Type. 3- Choose Image in Asset Type and select your picture inside Path field (Foreground Layer tab). 4- Create or download below a PNG file with transparent background of 512x512 px size (this is a size of ic_launcher-web.png). PNG link: https://i.stack.imgur.com/Pwbuz.png 5- In Background Layer tab select Image in Asset Type and load the transparent background from step 4. 6- In Legacy tab select Yes for all Generate, None for Shape. 7- In Foreground Layer and Background Layer tabs you can change trim size. Though you will see a black background behind the image in Preview window, after pressing Next, Finish and compiling an application you will see a transparent background in Android 5, Android 8.

+ NDK (April 19, 2019, 5:08 p.m.)

The Native Development Kit (NDK) is a set of tools that allow you to use C and C++ code in your Android app. It provides platform libraries to manage native activities and access hardware components such as sensors and touch input. The NDK may not be appropriate for most novice Android programmers who need to use only Java code and framework APIs to develop their apps. However, the NDK can be useful for the following cases: - Squeeze extra performance out of a device to achieve low latency or run computationally intensive applications, such as games or physics simulations. - Reuse code between your iOS and Android apps. - Use libraries like FFMPEG, OpenCV, etc.

+ SDK / NDK (April 19, 2019, 5:04 p.m.)

Software Development Kit (SDK) Native Development Kit (NDK) Traditionally, all Software Development Kit (SDK) were in C, very few in C++. Then Google comes along and releases a Java based library for Android and calls it a SDK. However, then came the demand for C/C++ based library for development. Primarily from C/C++ developers aiming game development and some high performance apps. So, Google released a C/C++ based library called Native Development Kit (NDK).

+ ADB (Oct. 2, 2015, 4:34 p.m.)

apt install android-tools-adb android-tools-fastboot

+ Android Development Environment (July 6, 2016, 10:28 a.m.)

Visit the following links to get information about the dependencies you might need for the SDK version you intend to download: http://socialcompare.com/en/comparison/android-versions-comparison http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels https://cordova.apache.org/docs/en/latest/guide/platforms/android/ ---------------------------------------------------------------------- You might find the tools and all the dependencies in the following links: http://osgard.blogspot.com/2011/11/download-of-android-sdk-components.html https://dl.zjuqsc.com/android/android-sdk-linux/ http://archive.virtapi.org/packages/a/android-sdk-build-tools/ ---------------------------------------------------------------------- 1- Create a folder preferably name it "android-sdk-linux" in any location. 2- Downloading SDK Tools: From the following link, scroll to the bottom of the page, the table having the title "Command line tools only" and download the "Linux" package. https://developer.android.com/studio/index.html Extract the downloaded file "sdk-tools-linux.zip" to the folder you created in step 1. 3- Download an API level (for example, android-15_r03.zip or android-15.zip which is for Android 4.0.4). Create a folder named "platforms" in "android-sdk-linux" and extract the downloaded file to it. 4- Download the latest version of `build-tools` (build-tools_r25-linux.zip). Create a folder named `build-tools` in `android-sdk-linux` and extract it to it. You need to rename the extracted folder to `25`. 5- Download the latest version of `platform-tools` (platform-tools_r23.0.1-linux.zip). Extract it to the folder `android-sdk-linux`. It should have already a folder named `platform-tools`, so no need to create any further folders. 6- Open the file `~/.bashrc` and add the following line to it: export ANDROID_HOME=/home/mohsen/Programs/Android/Development/android-sdk-linux 7- apt install openjdk-9-jdk If you got errors like this: \dpkg: warning: trying to overwrite '/usr/lib/jvm/java-9-openjdk-amd64/include/linux/jawt_md.h', which is also in package openjdk-9-jdk-headless To solve the error: apt-get -o Dpkg::Options::="--force-overwrite" install openjdk-9-jdk ----------------------------------------------------------------------

+ AVD with HAXM or KVM (Emulators) (April 10, 2016, 7:55 a.m.)

Official Website: https://software.intel.com/en-us/android/articles/intel-hardware-accelerated-execution-manager -------------------------------------------------------- For a faster emulator, use the HAXM device driver. Linux Link: https://software.intel.com/en-us/blogs/2012/03/12/how-to-start-intel-hardware-assisted-virtualization-hypervisor-on-linux-to-speed-up-intel-android-x86-emulator As described in the above link, Linux users need to use KVM. Taken from the above website: (Since Google mainly supports Android build on Linux platform (with Ubuntu 64-bit OS as top Linux platform, and OS X as 2nd), and a lot of Android Developers are using AVD on Eclipse or Android Studio hosted by a Linux system, it is very critical that Android developers take advantage of Intel hardware-assisted KVM virtualization for Linux just like HAXM for Windows and OS X.) -------------------------------------------------------- KVM Installation: https://help.ubuntu.com/community/KVM/Installation 1- egrep -c '(vmx|svm)' /proc/cpuinfo If the output is 0 it means that your CPU doesn't support hardware virtualization. 2- apt install cpu-checker Now you can check if your cpu supports kvm: # kvm-ok 3- To see if your processor is 64-bit, you can run this command: egrep -c ' lm ' /proc/cpuinfo If 0 is printed, it means that your CPU is not 64-bit. If 1 or higher, it is. Note: lm stands for Long Mode which equates to a 64-bit CPU. 4- Now see if your running kernel is 64-bit: uname -m 5- apt install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils ia32-libs-multiarch If a screen with `Postfix Configuration` was displayed, ignore it by selecting `No Configuration`. 6- Next is to add your <username> account to the group kvm and libvirtd sudo adduser mohsen kvm sudo adduser mohsen libvirtd 7-Verify Installation: You can test if your install has been successful with the following command: sudo virsh -c qemu:///system list Your screen will paint the following below if successful: Id Name State ---------------------------------------------------- 8- Install Java: Oracle java has to be installed in order to run Android emulator x86 system Images. sudo apt-get install openjdk-8-jre 9- Download a System Image from the following link: http://mirrors.neusoft.edu.cn/android/repository/sys-img/android/ Create a folder named `system-images` in `android-sdk-linux` and extract the downloaded system image in it. (You might need to create another folder inside, named `default`.) Run the Android SDK Manager, you will probably see the system image under `Extras` which is broken. If it was so, for solving the problem, you need to download its API from this link and extract it in `platforms` folder: http://downloads.puresoftware.org/files/android/API/ 9- Start the AVD from Android SDK Directly from Terminal and create a Virtual Device: ~/Programs/Android/Development/android-sdk-linux/tools/android avd --------------------------------------------------------