Pages

Tuesday, June 6, 2017

Setup Kotlin Plugin for Android Studio 2.3

AndroidDev annouce officially adding support for the Kotlin programing language. Check here Android Developer Blog.

The Kotlin plug-in is now bundled with Android Studio 3.0. Kotlin was developed by JetBrains, the same people who created IntelliJ. Also Kotlin pluging available for Android Studio 2.3.2 version. See below how to add plugin Kotlin in Android Studio lower vsersion.

Let's Start to setup

Install Kotlin Plugin for Android Studio


In Windows PC : Open File>Setting>Plugins>Browse Repositories (Preferences > Plugins > Browse Repositories). Search Kotlin and Install.

 
When the install is complete, you will need to restart Android Studio to apply the new plugin.

Apply Kotlin Plugin to the project


First of all create a new Android Project. File>New>New Project and follow through project creation steps.

Next step to apply the Kotlin Plugin to both build.gradle files at the Project level and app-module level. There is an automated tools to do this, but sometimes the manual process of applying the plugin in our build.gradle files.

First we need to add the plugin to the root project build.gradle.

 
buildscript {
    ext.kotlin_version = "1.1.2-4" // replace with the latest (stable) version: https://github.com/JetBrains/kotlin/releases/latest

    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

Second we need to add the plugin to the app-module build.gradle. To add apply plugin : kotlin-android in build.gradle file

  apply plugin: 'com.android.application'
  apply plugin: 'kotlin-android' // apply kotlin android plugin

Convert Java Code to Kotlin


We setup all we need, but our Empty Activity still in java. Here show how to convert java file to Kotline. Way to convert java file to Koitlin Select Code>Convert Java File to Kotlin or use shortcut - control + Alt + Shift + K  (Replace control to Command if you are using MAC)

MainActivity.java
package com.limbani.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

After converting code to Kotlin. MainActivity.kt look like below
package com.limbani.helloworld

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Thanks for reading this post. I will be writing tutorial for Kotlin language.

Resource
https://kotlinlang.org
Android Developer Blog
 

Wednesday, June 22, 2016

Firebase Analytics integrate in Android


Hello, Today writing post for how to integrate Firebase Analytics in Android Application. Firebase Analytics is a free to analytic for android application.

1. Download firebase SDK  (Google Play services SDK from the Android SDK Manager)
2. Create a project in the Firebase Consol and download json file
3. Android studio 1.5 or latter and Android 2.3 or newer.
4. Check firebase report.


1. Firebase SDK : First update your Google Play services from the Android SDK Manager.

Check in Extras

Google Play Services installed rev 30 or up
Google Repository installed rev 26  or up. both are required to updated.


2. Create Project in Firebase Consol

* Select Add Firebase to your android App



 *  Add Package name here and click ADD APP button. Here make sure to add that package name to use in application.

* Download google-services.json File and click continue




*Here you can see to how to init Firebase sdk in android application. Click Finish


3. Create Application in Android studio and integrate Firebase SDK

 In Android studio select New > New Project
* Add Application and package name (package name is same as added Firebase project)


 * Select here Android minimum sdk version


* Select Empty Activity


* Enter Activity Name


* Now Copy google-services.json file and past to project's module folder "app/" folder





*  Add rules to your root-level build.gradle file, to include the google-services plugin:


* Add dependency for Firebase Analytics to your app-level build.gardle file and add plugin bottom
after add dependency and plugin then click Sync Now Button





*Create App.java and create FirebaseAnalytics object in Application class


 
package com.limbani.firebaseanalyticsdemo;

import android.app.Application;

import com.google.firebase.analytics.FirebaseAnalytics;


public class App extends Application {

    private static FirebaseAnalytics mFirebaseAnalytics;
    @Override
    public void onCreate() {
        super.onCreate();
        this.mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
    }

    public static FirebaseAnalytics getFirebaseAnalytics() {
        return mFirebaseAnalytics;
    }
}

* MainActivity.java clas and add Firebase LogEven see in code. You can add more Event log see in FirebaseAnalytics.Event


package com.limbani.firebaseanalyticsdemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.firebase.analytics.FirebaseAnalytics;

public class MainActivity extends AppCompatActivity {

    private Button btn_click;

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

        btn_click = (Button) findViewById(R.id.btn_click);

        btn_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Here Add Firebase LogEvent
                Bundle bundle = new Bundle();
                bundle.putString(FirebaseAnalytics.Param.ITEM_ID, "btn_click");
                bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, "Next Activity");
                bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "Button");
                App.getFirebaseAnalytics().logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);

                startActivity(new Intent(MainActivity.this, SecondActivity.class));
            }
        });
    }
}


* activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.limbani.firebaseanalyticsdemo.MainActivity">

    <Button
        android:id="@+id/btn_click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next Activity" />
</RelativeLayout<

* Create SeconActivity.java class and activity_second.xml file


package com.limbani.firebaseanalyticsdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SecondActivity extends AppCompatActivity {

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

* activity_second.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.limbani.firebaseanalyticsdemo.SecondActivity">

</RelativeLayout>



* Edit AndroidManifest.xml File


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.limbani.firebaseanalyticsdemo">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"></activity>
    </application>

</manifest>


4. Check Report in Firebase :



Error Resolve

1. If you find error while sync time like faild to resolve firebase then you need to check 1st point to Google Play Service updated or not.


2. Execution faild for task ':app:processDebugGoogleServices'.
> File google-services.json is missing. ......
getting this error then you are missing to add google-services.json or miss placed this file. check 3rd point

Download Source code

Thank you :)