Showing posts with label Android Studio.how to. Show all posts
Showing posts with label Android Studio.how to. Show all posts

Thursday, May 20, 2021

Fix Android Studio error: Missing essential plugin: org.jetbrains.android

Just update Android Studio 4.2.1, but fail to run with error of: 

Missing essential plugin: 
org.jetbrains.android 
Please reinstall Android Studio from scratch. 


Here how I fix it in my case, Android Studio 4.2.1 on Windows 10:


Search and delete "C:\Users\user\AppData\Roaming\Google\AndroidStudio4.X\disabled_plugins.txt"

There are two in my setup:
"C:\Users\user\AppData\Roaming\Google\AndroidStudio4.1\disabled_plugins.txt"
"C:\Users\user\AppData\Roaming\Google\AndroidStudio4.2\disabled_plugins.txt"

I delete both disabled_plugins.txt and re-start Android Studio.

Sunday, October 11, 2020

What's the difference between system image with Google API and Google APIs? when create Android Virtual Device

 


When you create a Android Virtual Device, you have to select a system image from a list of available images. Some marked Google Play, and some marked Google APIs, so what's the difference?

A system image labeled with Google APIs includes access to Google Play services. A system image labeled with the Google Play logo in the Play Store column includes the Google Play Store app and access to Google Play services.

reference: https://developer.android.com/studio/run/managing-avds#system-image


Including a Google Play tab in the Extended controls dialog that provides a convenient button for updating Google Play services on the device.





Monday, April 13, 2020

How to display layout xml in code view, for Android Studio 3.6.2

With Android Studio updated (currently 3.6.2), when you view layout xml file, the original design/code option (on lower-left corner) removed. So how to view its xml code?
Answer: The option is now moved to upper-right corner, you can view it in Code/Splite/Design view.




Wednesday, March 13, 2019

Add Google Maven repository to Android Studio Project

With latest Android Studio, I change the targetSdkVersion and compileSdkVersion of my old exercise to 28. And I also have to use updated com.android.support:appcompat-v7 and com.android.support.constraint:constraint-layout. After fail in rebuild, it's a number of WARNING and ERROR.

-----------------------------
ERROR: Failed to resolve: com.android.support:appcompat-v7:28.0.0
Add Google Maven repository and sync project
Show in Project Structure dialog
Affected Modules: app

ERROR: Failed to resolve: com.android.support.constraint:constraint-layout:1.1.3
Add Google Maven repository and sync project
Show in Project Structure dialog
Affected Modules: app

WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
Affected Modules: app

WARNING: Configuration 'testCompile' is obsolete and has been replaced with 'testImplementation'.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
Affected Modules: app

WARNING: Configuration 'androidTestCompile' is obsolete and has been replaced with 'androidTestImplementation'.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
Affected Modules: app
-----------------------------

As suggested, I change  'compile', 'testCompile' and 'androidTestCompile' to 'implementation', 'testImplementation' and 'androidTestImplementation', in app/build.gradle.


To fix the error of Failed to resolve: com.android.support:appcompat-v7:28.0.0 and com.android.support.constraint:constraint-layout:1.1.3, I add the follow lines of Google Maven repository to build.gradle.

        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }



And rebuild the project. At least it works for me now.




So, how to know the latest version of com.android.support:appcompat-v7 and com.android.support.constraint:constraint-layout?

For Support Library, you can check here: Android Developers > Platform > Libraries > Recent Support Library Revisions

28.0.0 is the recentest stable release of Support Library released on September 21, 2018 and will be the last feature release under the android.support packaging.

For ConstraintLayout, Google announced at https://androidstudio.googleblog.com/2018/08/constraintlayout-113.html.

You can also check from Maven repository:
>> com.android.support >> appcompat-v7
>> com.android.support.constraint >> constraint-layout


Sunday, March 10, 2019

What is "android.R.layout.simple_list_item_1"?

Sometimes you will find some predefined layout in other examples, such as android.R.layout.simple_list_item_1 like this:

        String[] suggestion = getResources().getStringArray(R.array.suggestion);
        ArrayAdapter<String> adapter =
                new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, suggestion);

It's a reference to built-in XML layout that is part of the Android OS, HERE. Basically, all reference start with android.R is reference to Android built-in resource.

You can locate the source file in your development setup from Android Studio (BUT DON'T EDIT IT):


> Select it and right click > Go to > Declaration
Android Studio will open it for you, and the location will be shown on top bar.


Monday, February 25, 2019

How to set Java version in Android Studio

The video show how to set Java version on Studio 3.3.1.



By default, the Java language version used to compile your project is based on your project's compileSdkVersion (because different versions of Android support different versions of Java). If necessary, you can override this default Java version by adding the following CompileOptions {} block to your build.gradle file:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }
}

(reference: Android Developers > Android Studio > User guide > Set the JDK version)

You can also set using Android Studio menu:
- File > Project Structure.
- In Structure Project dialog, Select App on the left, and select Properties tab.
- You can see option boxes of Source Compatibility and Target Compatibility).



sourceCompatibility vs targetCompatibility

According to Gradle documentation:

  • JavaVersion sourceCompatibility - Java version compatibility to use when compiling Java source. Default value: version of the current JVM in use JavaVersion. Can also set using a String or a Number, e.g. '1.5' or 1.5.
  • JavaVersion targetCompatibility - Java version to generate classes for. Default value: sourceCompatibility. Can also set using a String or Number, e.g. '1.5' or 1.5.

(reference: https://docs.gradle.org/current/userguide/java_plugin.html)

Sunday, February 10, 2019

Change Gradle plugin version in Android Studio 3.3.1

To change Gradle plugin to specified version, you can:
- the File > Project Structure > Project menu in Android Studio,


- or edit the top-level build.gradle file.

Monday, November 12, 2018

Where is the Android SDK location installed

In case you forget the location of Android SDK was installed during the installation of Android Studio. You can find it at:

> Android Studio Menu > File > Project Structure
> It's show in Android SDK Location box





Friday, October 19, 2018

How to capture screen in Android Studio

Follow the steps to capture screen of Android Emulator or connected Android device, using Android Studio. It work for me on Android Studio 3.2.1.

  1. Run your app on a connected device or emulator. If using a connected device, be sure you have enabled USB debugging.
  2. In Android Studio, select View > Tool Windows > Logcat to open Logcat.
  3. Select the device and a process from the drop-down at the top of the window.
  4. Click Screen Capture   on the left side of the window.

The screenshot appears in a Screenshot Editor window.


You can also rotate your captured screen or choose a device to wrap your screenshot with real device artwork; Drop Shadow, Screen Glare, or both.

source: https://developer.android.com/studio/debug/am-screenshot

Monday, October 15, 2018

Monitor memory related events with onTrimMemory(), and simulate memory low in Android Emulator using adb

Android can reclaim memory from your app or kill your app entirely if necessary to free up memory for critical tasks. To help balance the system memory and avoid the system's need to kill your app process, you can implement the ComponentCallbacks2 interface and override onTrimMemory() callback method to listen for memory related events when your app is in either the foreground or the background, and then release objects in response to app lifecycle or system events that indicate the system needs to reclaim memory. 

When the system begins killing processes in the LRU cache, it primarily works bottom-up. The system also considers which processes consume more memory and thus provide the system more memory gain if killed. The less memory you consume while in the LRU list overall, the better your chances are to remain in the list and be able to quickly resume.

Here is a example code to implement the ComponentCallbacks2 interface and override onTrimMemory() callback method.

package com.blogspot.android_er.androidontrimmemory;

import android.content.ComponentCallbacks2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

/*
http://android-er.blogspot.com/
Example to use onTrimMemory()
 */
public class MainActivity extends AppCompatActivity
        implements ComponentCallbacks2 {

    private static final String TAG = "MyActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.v(TAG, "onCreate() called");
    }

    public void onTrimMemory(int level){
        Log.v(TAG, "onTrimMemory(" + level + ")");
        switch(level){

            case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:
                Log.v(TAG, "TRIM_MEMORY_UI_HIDDEN");
                break;
            case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:
                Log.v(TAG, "TRIM_MEMORY_RUNNING_MODERATE");
                break;
            case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:
                Log.v(TAG, "TRIM_MEMORY_RUNNING_LOW");
                break;
            case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:
                Log.v(TAG, "TRIM_MEMORY_RUNNING_CRITICA");
                break;
            case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:
                Log.v(TAG, "TRIM_MEMORY_BACKGROUND");
                break;
            case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
                Log.v(TAG, "TRIM_MEMORY_MODERATE");
                break;
            case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
                Log.v(TAG, "TRIM_MEMORY_COMPLETE");
                break;

            default:
                Log.v(TAG, "default");
        }
    }

}


To simulate the memory low condition, you can use the adb command:

adb shell am send-trim-memory

Example for my case, it's:

adb shell am send-trim-memory com.blogspot.android_er.androidontrimmemory MODERATE

Notice that you cannot set background level if your app is in foreground. Otherwise the following exception will be thrown, as shown in the video.

java.lang.IllegalArgumentException: Unable to set a background trim level on a foreground process



reference:
Overview of memory management
Manage your app's memory





Friday, October 12, 2018

Android Studio tips: improve build by setting org.gradle.jvmargs in Gradle script

Dex In Process (introduced from Android 2.1) is a feature that can improve build times, as well as Instant Run performance. To take advantage of Dex In Process, you’ll need to modify your gradle.properties file and increase the amount of memory allocated to the Gradle Daemon VM to a minimum of 2 Gb, using the org.gradle.jvmargs property.

org.gradle.jvmargs=-Xmx2048m



reference:


Learn more here about how to enable Dex In Process: Faster Android Studio Builds with Dex In Process




Wednesday, November 8, 2017

What's New in Android Studio 3.0

What's New in Android Studio 3.0:

Android Studio 3.0 brings a ton of new features and improvements, including Kotlin support, Android O APIs, Java 8 language features support, external APK debugging, Instant Apps modules and refactoring, an integrated Android Profiler and more.

Migrating to Android Gradle Plugin 3.0.0:

Android Studio 3.0: Android Profiler:

Android Studio 3.0: Java 8 Language Features Support:


Read more in the release notes here: https://goo.gl/7yo9SM

Monday, May 22, 2017

Download and install Android Studio 3.0 Canary 1 on Ubuntu 17.04

This post show how to Download and install Android Studio 3.0 Canary 1 on 64-bit Ubuntu 17.04, over Windows 10/Oracle VirtualBox,


Firstly, Install Ubuntu 17.04 on Windows 10/Oracle VirtualBox.

Before install Android Studio on 64-bit Ubuntu 17.04, install 32-bit libraries:
$ sudo dpkg --add-architecture i386
$ sudo apt-get update
$ sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 libbz2-1.0:i386

(details refer to Fix "Unable to run mksdcard SDK tool" when install Android Studio on Linux)

Download Android Studio 3.0 Canary, visit https://developer.android.com/studio/preview/index.html
Unpack the ZIP file, to any directory you want.
Open a terminal, navigate into <android-studio>/bin/ and execute studio.sh

This video show how:


Setup for debugging on real device:

On Ubuntu/Linux, to debug on real device, you have to set up your system to detect your device.
(referencec: https://developer.android.com/studio/run/device.html#setting-up)

Edit/Create /etc/udev/rules.d/51-android.rules
$ sudo nano /etc/udev/rules.d/51-android.rules

Add the code:
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", MODE="0666", GROUP="plugdev"

Where 00b4 is the vendor ID, replace it to match your device. For my Samsung device, replace with 04e8.

You can check your USB vendor ID here: https://developer.android.com/studio/run/device.html#VendorIds.

or try to find it using lsusb command.



Kotlin programming language, Now official on Android. It's a dummy Hello World of Kotlin language, created in Android Studio 3.0 Canary 1.



Get USB devices' vendor ID and product ID on Linux, using lsusb command

To get the vendor ID/product ID of attached USB devices in Linux, you can use the command lsusb.

This video show how to, tested on Ubuntu 17.05 over Windows 10/Oracle VirtualBox 5.1




reference: Ubuntu Manual - lsusb



Friday, May 19, 2017

Fix "Unable to run mksdcard SDK tool" when install Android Studio on Linux

If you install Android Studio on 64-bit Linux and reported with error:
Unable to run mksdcard SDK tool



Most probably some 32-bit libraries are missed.

If you are running a 64-bit version of Ubuntu, you need to install some 32-bit libraries with the following command:
sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 lib32bz2-1.0

If you are running 64-bit Fedora, the command is:
sudo yum install zlib.i686 ncurses-libs.i686 bzip2-libs.i686

reference: Troubleshoot Android Studio - Linux libraries


When I try to install the 32-bit libraries on Ubuntu 16.04.2 LTS (64-bit) on VirtualBox with the command:

$ sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 lib32bz2-1.0

The errors reported:
E: Unable to locate package lib32bz2-1.0
E: Couldn't find any package by glob 'lib32bz2-1.0'
E: Couldn't find any package by regex 'lib32bz2-1.0'

To fixed it, add 32 bit architecture with the command:
$ sudo dpkg --add-architecture i386
$ sudo apt-get update

Re-install with the command 
$ sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 libbz2-1.0:i386


Done:


Saturday, February 11, 2017

Building interfaces with ConstraintLayout in Android Studio

The new Layout Editor in Android Studio 2.2 include a new blueprint mode, revamped properties inspector and support for ConstraintLayout, a new way to define layouts for your apps.


In this Android Tool Time episode Wojtek Kaliciński shows you the basics of working with ConstraintLayouts in the visual editor. If you want to try it out yourself, you can find our codelab here: https://codelabs.developers.google.com/codelabs/constraint-layout

When you’re familiar with the layout editor interface, read the rest of our Medium article where you’ll find some more advanced tips and tricks for ConstraintLayout: https://goo.gl/a5orYw


Monday, February 6, 2017

IOException: not create document. Error

When I prepare the example "Display PDF in assets folder (inside APK)", I face with the error of "java.io.IOException: not create document. Error". It should be generated by the code:

pdfRenderer = new PdfRenderer(fileDescriptor);


Somebody commented it's caused by new version of Gradle (ref: https://github.com/googlesamples/android-PdfRendererBasic/issues/1), so I edit dependencies of buildscript in build.gradle (Project: ...), use gradle:2.1.2 to solve this problem.


Create assets folder in Android Studio, and copy file into.

How to create assets folder in Android Studio, and copy a PDF file into the assets folder.

Saturday, February 4, 2017

Change minSdkVersion, targetSdkVersion, compileSdkVersion in Android Studio



To change minSdkVersion in Android Studio:

- Right click your app -> Open Module Settings

- Select app and Flavors tab, select your new Min Sdk Version, then OK.

- minSdkVersion changed.

You can also change targetSdkVersion, compileSdkVersion (under Properties tab) also.

Tuesday, October 25, 2016

Beginning Android Programming with Android Studio 4th Edition

A hands-on introduction to the latest release of the Android OS and the easiest Android tools for developers

Beginning Android Programming with Android Studio

As the dominant mobile platform today, the Android OS is a powerful and flexible platform for mobile device. The new Android 7 release (New York Cheesecake) boasts significant new features and enhancements for both smartphone and tablet applications. This step-by-step resource takes a hands-on approach to teaching you how to create Android applications for the latest OS and the newest devices, including both smartphones and tablets.
  • Shows you how to install, get started with, and use Android Studio 2 - the simplest Android developer tool ever for beginners
  • Addresses how to display notifications, create rich user interfaces, and use activities and intents
  • Reviews mastering views and menus and managing data
  • Discusses working with SMS 
  • Looks at packaging and publishing applications to the Android market
Beginning Android Programming with Android Studio starts with the basics and goes on to provide you with everything you need to know to begin to successfully develop your own Android applications.