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 24, 2019

Google Play will require new/update App to target API level 28+ in 2019

Google Play Console will continue to require that apps target a recent API level:

  • August 2019: New apps are required to target API level 28 (Android 9) or higher.
  • November 2019: Updates to existing apps are required to target API level 28 or higher.

Existing apps that are not receiving updates are unaffected and can continue to be downloaded from the Play Store.

source: Android Developer Blog - Expanding target API level requirements in 2019


Thursday, February 21, 2019

Install JetBrains Toolbox App on Linux, and also Android Studio and IntelliJ IDEA (Java IDE)

JetBrains Toolbox App is a lightweight cross-platform companion application for JetBrains' coding tools, to manage installed tools, download new ones and open recent projects.

System requirement to install Toolbox App for Linux:
64-bit x86, glibc 2.17 (Ubuntu 14.04 or newer). JetBrains Toolbox App is packaged in AppImage and requires FUSE to run.

To check the version of your glibc, it's a simple method, using ldd command in Terminal:
$ ldd --version

It will show something like this:
ldd (Ubuntu GLIBC 2.27-3ubuntu1) 2.27

To install and set FUSE, follow the steps in AppImage Wiki.

For Ubuntu, enter the commands in Terminal:

$ sudo apt install fuse
$ sudo modprobe fuse
$ sudo groupadd fuse
$ user="$(whoami)"
$ sudo usermod -a -G fuse $user

Visit https://www.jetbrains.com/toolbox/app/ to download the, currently it's version 1.13.

This video show how to Install JetBrains Toolbox App on Linux Mint 19.1

Install Android 3.3.1 on Linux Mint 19.1 with JetBrains Toolbox, Hello World and convert Java to Kotlin.

IntelliJ IDEA (Java IDE) with JetBrains Toolbox.

Wednesday, February 20, 2019

Install OpenJDK on Linux Mint/Ubuntu

OpenJDK (https://openjdk.java.net/) provides open-source builds of the Java Development Kit, an implementation of the Java SE Platform under the GNU General Public License, version 2, with the Classpath Exception.

Commercial builds of JDK from Oracle under a non-open-source license, for a wider range of platforms, can be found at the Oracle Technology Network.

This video show how to Install OpenJDK 11.0.2 on Linux Mint 19.1. The OpenJDK will be stored in /opt/java directory, and set as default. It should be work on other Ubuntu variants also.


Oracle's OpenJDK JDK binaries for Windows, macOS, and Linux are available on release-specific pages of jdk.java.net as .tar.gz or .zip archives.

As an example, the archives for JDK 11 (currently 11.0.2) may be found on https://jdk.java.net/11/.

- Visit https://jdk.java.net/11/ to download Linux / x64 build.

- Change to the downloaded directory, it's ~/Downloads in my case.

- Extract the downloaded file, openjdk-11.0.2_linux-x64_bin.tar.gz.
$ tar -zxvf openjdk-11.0.2_linux-x64_bin.tar.gz

-  Create a folder in /opt/java where jdk will be stored.
$ sudo mkdir -p /opt/java

- Move the extracted folder to /opt/java
$ sudo mv jdk-11.0.2 /opt/java

- Set default java and javac using update-alternatives:
$ sudo update-alternatives --install /usr/bin/javac javac /opt/java/jdk-11.0.2/bin/javac 1
$ sudo update-alternatives --install /usr/bin/java java /opt/java/jdk-11.0.2/bin/java 1
$ sudo update-alternatives --config javac
$ sudo update-alternatives --config java

There are 2 choices for the alternative java, enter the selection of "1", corresponding to the number of /opt/java/jdk-11.0.2/bin/java.



may be interested:
Install JetBrains Toolbox App on Linux, and also Android Studio and IntelliJ IDEA (Java IDE)

Monday, February 11, 2019

My first exercise of using Data Binding + ObservableField

The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically. It's my first exercise of using Data Binding and ObservableField, in Java. Basically, it follow the steps in Android Developers document of Data Binding Library.


The TextViews myArchitecture, myRelease and myDensity are loaded in onCreate() once. The TextView myNumber is associated with ObservableField, and will be updated at run-time.

Prepare:

- To use the Data Binding Library in your app, you have to target devices running Android 4.0 (API level 14) or higher.

- It's recommended to use the latest Android Plugin for Gradle in your project. Currently it's 3.3.1.


Read this video how to:

- Make sure include Support Repository in the Android SDK manager.


-  Add the dataBinding element to your build.gradle file in the app module.

In Android Studio, select Project view, extend app view, select to edit build.gradle. Add the code:

    dataBinding {
        enabled = true
    }


- Enable the new data binding compiler.
Note: The new compiler in Android Plugin version 3.2 is enabled by default. (refer https://developer.android.com/topic/libraries/data-binding/start) So it's skipped here.

- layout:
layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable name="user" type="com.blogspot.android_er.myandroidinfo.MainActivity.User"/>
    </data>

    <android.support.constraint.ConstraintLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="My device info"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:textStyle="bold"/>
        <TextView
            android:id="@+id/mylink"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="android-er.blogspot.com"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/title" />
        <TextView
            android:id="@+id/myArchitecture"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text='@{user.bArchitecture, default="Architecture"}'
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/mylink"
            android:gravity="left"
            android:textSize="30dp"/>
        <TextView
            android:id="@+id/myRelease"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text='@{user.bRelease, default="Release"}'
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/myArchitecture"
            android:gravity="left"
            android:textSize="30dp"/>
        <TextView
            android:id="@+id/myDensity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text='@{user.bDensity, default="Density"}'
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/myRelease"
            android:gravity="left"
            android:textSize="30dp"/>

        <TextView
            android:id="@+id/myNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{user.bNum}"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/myDensity"
            android:gravity="left"
            android:textSize="50dp"
            android:textStyle="italic|bold"
            android:textColor="#0000FF"/>

    </android.support.constraint.ConstraintLayout>
</layout>

- Java code
MainActivity.java
package com.blogspot.android_er.myandroidinfo;

import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;

import com.blogspot.android_er.myandroidinfo.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    public class User {
        public final String bArchitecture;
        public final String bRelease;
        public final String bDensity;
        public final ObservableField<String> bNum = new ObservableField<>();
        public User(String arch, String rel, String den) {
            this.bArchitecture = arch;
            this.bRelease = rel;
            this.bDensity = den;
        }
    }

    private class MyTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {

            for (int i = 0; i < 20; i++) {
                user.bNum.set(String.valueOf(i));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    }

    User user;

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

        String arch = System.getProperty("os.arch");
        String release = Build.VERSION.RELEASE;

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int dpi = metrics.densityDpi;

        ActivityMainBinding binding =
                DataBindingUtil.setContentView(this, R.layout.activity_main);
        user = new User(arch, release, String.valueOf(dpi));
        binding.setUser(user);

        new MyTask().execute();

    }
}


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.

Tuesday, February 5, 2019

Get Architecture, Version and Screen DPI programmatically

This code get Architecture, Version and Screen DPI of Android device programmatically, using Java.


package com.blogspot.android_er.myandroidinfo;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tvMyArch = findViewById(R.id.myArchitecture);
        TextView tvMyRelease = findViewById(R.id.myRelease);
        TextView tvMyDensity = findViewById(R.id.myDensity);

        String arch = System.getProperty("os.arch");
        tvMyArch.setText(arch);

        String release = Build.VERSION.RELEASE;
        tvMyRelease.setText(release);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int dpi = metrics.densityDpi;
        tvMyDensity.setText(String.valueOf(dpi));

    }
}



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My device info"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/mylink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-er.blogspot.com"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title" />
    <TextView
        android:id="@+id/myArchitecture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Architecture"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mylink"
        android:gravity="left"
        android:textSize="30dp"/>
    <TextView
        android:id="@+id/myRelease"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Release"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/myArchitecture"
        android:gravity="left"
        android:textSize="30dp"/>
    <TextView
        android:id="@+id/myDensity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Density"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/myRelease"
        android:gravity="left"
        android:textSize="30dp"/>

</android.support.constraint.ConstraintLayout>