Tuesday, October 2, 2018

Emulator: ERROR: x86 emulation currently requires hardware acceleration

I tried to run my Hello World application in Android Studio. I got the following error:
* Emulator: ERROR: x86 emulation currently requires hardware acceleration!
* Please ensure Intel HAXM is properly installed and usable.
* CPU acceleration status: HAX kernel module is not installed!
So solution is as below:
* Open SDK Manager (In Android Studio, go to Tools > Android > SDK Manager) and Download Intel x86 Emulator Accelerator (HAXM installer) if you haven't.
* Now go to your SDK directory C:\users\%USERNAME%\AppData\Local\Android\sdk\extras\intel\Hardware_Accelerated_Execution_Manager\ and run the file named intelhaxm-android.exe.
* In case you get an error like "Intel virtualization technology (vt,vt-x) is not enabled". Go to your BIOS settings and enable Hardware Virtualization.
* Restart Android Studio and then try to start the AVD again.

Saturday, September 15, 2018

Android studio 3.X does not render xml file due to java class missing

java.lang.ClassNotFoundException: android.view.View$OnUnhandledKeyEventListener
In my very first project is obviously Hello world and I just open android studio and click on the empty activity and then w8 for the build to finish but it doesn't render the app as I expected. I reinstalled android studios and JDK10. but it doesn't work. the project shows fine in virtual device and I can even edit hello world to any string, but in XML view, it just shows a white screen and its showing every time. Below is the stack trace:
java.lang.ClassNotFoundException: android.view.View$OnUnhandledKeyEventListener 
at org.jetbrains.android.uipreview.ModuleClassLoader.load(ModuleClassLoader.java:180) 
at com.android.tools.idea.rendering.RenderClassLoader.findClass(RenderClassLoader.java:61) 
at org.jetbrains.android.uipreview.ModuleClassLoader.findClass(ModuleClassLoader.java:118) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
at org.jetbrains.android.uipreview.ModuleClassLoader.loadClass(ModuleClassLoader.java:213) 
at android.support.v7.widget.ActionBarContainer.(ActionBarContainer.java:62) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:481) 
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:264) 
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:222) 
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:209) 
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:337) 
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:348) 
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:248) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) 
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:863) 
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:837) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at com.android.layoutlib.bridge.bars.BridgeActionBar.(BridgeActionBar.java:89) 
at com.android.layoutlib.bridge.bars.AppCompatActionBar.(AppCompatActionBar.java:68) 
at com.android.layoutlib.bridge.impl.Layout.createActionBar(Layout.java:277) 
at com.android.layoutlib.bridge.impl.Layout.(Layout.java:161) 
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:288) 
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:384) 
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:193) 
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:544) 
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$3(RenderTask.java:678) 
at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
at java.lang.Thread.run(Thread.java:745)
First of all we are developing an app, so we don't need to face these types of issues because it has no valid reason to get such errors. So I suggest you use always stable version of every dependency, and this is because of unstable version using.
The issue you faced was due to using alpha version of AppCompact library. Use stable version to get rid of this issue now and in future.
FIRST Currently 27.1.1 is latest version of AppComactV7. You can see Maven Repo link to find out latest.
final def SUPPORT_VERSION = "27.1.1"
implementation "com.android.support:appcompat-v7:$SUPPORT_VERSION"
implementation "com.android.support:design:$SUPPORT_VERSION" // use only if already using
I use definitions to have same version for all support versions. Also ignore design library if you are not already using it.
SECOND You also need to downgrade your compileSdkVersion & targetSdkVersion to remove some gradle warnings.
compileSdkVersion 27
targetSdkVersion 27
Below is full example of build.gradle (Module:app)
apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.hp.myapplication"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
final def SUPPORT_VERSION = "27.1.1"
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "com.android.support:appcompat-v7:$SUPPORT_VERSION"
    implementation "com.android.support:design:$SUPPORT_VERSION"

    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:gridlayout-v7:27.1.1'
}

Emulator: ERROR: x86 emulation currently requires hardware acceleration

The problem is very simple and am going to solve that problem in very simple way. Problem is to lunch android app from Android Studio. The problem is as below described:
  • Emulator: ERROR: x86 emulation currently requires hardware acceleration!
  • Please ensure Intel HAXM is properly installed and usable.
  • CPU acceleration status: HAX kernel module is not installed!
First step to open SDK Manager (In Android Studio, go to Tools > Android > SDK Manager) and Download Intel x86 Emulator Accelerator (HAXM installer) if you haven't as below screen shot:

Now go to your SDK directory C:\users\%USERNAME%\AppData\Local\Android\sdk\extras\intel\Hardware_Accelerated_Execution_Manager\ and run the file named intelhaxm-android.exe. This will almost fix your problem. In case you get an error like "Intel virtualization technology (vt,vt-x) is not enabled". Go to your BIOS settings and enable Hardware Virtualization.
Restart Android Studio and then try to start the AVD again.
It might take a minute or 2 to show the emulator window.

Saturday, September 8, 2018

Portable GIT > A way to access Git without installing it on Windows

This is the portable edition of Git, click below link to download it from below link provided, a command-line application that helps engineers and designers to keep all versions of a code as well as images or layout, by storing all data in a collection of snapshots taken from a small filesystem.
PortableGit-1.9.5-preview20150319
Portability advantages
Since there is no installation required, you can just download and extract the archive files to any local hard disk and directly execute Git just clicking on it. There is also the option to save it to a USB flash disk or other removable (temporary) storage unit, in order to launch it on any PC here and there easily and without previous installation. As like most installers we install on windows, Portable Git does not add new entries to the Windows registry or Start menu, thus leaving no traces behind after its removal. Because they are not installed in the system, they are just standalone.
Rich and simple-to-activate commands
As Git is a console application, all its options as well as functionalities are accessible from a Command Prompt window. Thus, it is possible to add file contents to the index, locate changes which introduced bugs via binary searches, list, create and delete branches, record changes to the repository, via commit logs, join together two or more development histories, and many others. A user name and email address can be assigned in order to restrict the access of other users.

Saturday, September 1, 2018

Extract the contents from an .ISO file without burning the .ISO to disc using third party application

We need it frequently to install ISO image file into our Windows operating system. One way is to burn ISO into USB or DVD. But there is another way, we can extract contents from an ISO file into hard drive using third party tools. Below list of some tools:
  1. IsoBuster
  2. Daemon Tools
  3. Pismo File Mount or Download from here
First I will demonstrate how we can extract an ISO file using Pismo File Mount. Download Pismo File Mount from above link. then click on an ISO file will show menu as below:
Click on very first option named "Mount" will show a folder with extract files and folders. From there you can find your installation exe file and run to install your desired software. Actually Mounting an ISO file create a virtual drive like below image. You can then Eject that virtual drive by right click on that drive and select "Eject" option from options.

Wednesday, August 22, 2018

Grails on Groovy > Way in Grails to Eager Fetch the Whole Record | Load Related Entity With Single Query

I am trying to find if there is any way in Grails to eager fetch complete records instead of a left Join.
For example, let you have domain named Tabel1 with following structure and another domain named Table3 below:
package com.pkm

class Table1 {
    Long id
    String name
    String roll

    static mapping = {
        table("table1")
    }

    static belongsTo = [
            table3: Table3
    ]

    static constraints = {
        table3 nullable: true
    }
}



package com.pkm

class Table3 {
    Long id
    String name
}
So when you get/list Table1, what will be the default behavior? It will load only Table1 data, not other associated data like Table3.
There is a procedure to load selected associated by joining them. See the below example, enabled eager face data when creating criteria:
package com.pkm

import grails.transaction.Transactional
import org.hibernate.FetchMode
import org.hibernate.sql.JoinType
import org.springframework.transaction.TransactionStatus

@Transactional
class HomeService {
    TransactionStatus transactionStatus

    void callMe() {
        List list = Table1.createCriteria().list {
            createCriteria("table3", "j3", JoinType.LEFT_OUTER_JOIN)
            setFetchMode("j3", FetchMode.JOIN)
            order("id", "desc")
            setMaxResults(20)
        }
        list.each { Table1 table1 ->
            println("Name=${table1.name}, Table3=${table1.table3?.name}")
        }
    }
}
Now we will print the query generated:
select this_.id as id1_0_1_, this_.name as name2_0_1_, this_.roll as roll3_0_1_, this_.table3_id as table4_0_1_, j3x1_.id as id1_2_0_, j3x1_.name as name2_2_0_ from table1 this_ left outer join table3 j3x1_ on this_.table3_id=j3x1_.id order by this_.id desc limit ?
You observed that in select query, both Table1 and Table3 exists. And this is how we can implement eager face data when creating criteria builder to get data.

Saturday, August 11, 2018

Setting Up Recaptcha 2.0 with JavaScript and PHP

reCAPTCHA need to verify bots (automatic system) can't take advantage of your site. reCAPTCHA 2.0 is somewhat different than the old reCAPTCHA. In version 1.0 it required to type text but now in 2.0 it’s simply a click of a button as below image.

The term of verifying front end users is a bit different. A variable named "g-recaptcha-response" is sent through a POST request to your server-side script, and along with your reCAPTCHA secret key that you get when you sign up for your website, you need to pass that secret key along to Google to tell that the user is a bot or not to determine by system. The response from Google is a JSON response on success for failure both case, so that we will be using file_get_contents() and json_decode() to fetch and parse the response and decide that front user is valid or not.


The below is just a HTML form you should create to see captcha in your desired page. The div element with the class of "g-recaptcha" is the element where we want to place our Google captcha (reCAPTCHA). You need to replace data-sitekey with the key you get from google when you sign up for reCAPTCHA (https://www.google.com/recaptcha/intro/index.html).
<form method="post" enctype="multipart/form-data">
    <div class="g-recaptcha" data-sitekey="YOUR_KEY"></div>
    <button type="submit">CHECK RECAPTCHA</button>
</form>
But I will show another technique how to show reCAPTCHA using JavaScript. Below is a simple script to show how you can integrate reCAPTCHA using JavaScript. But server side validation using PHP. Use need to create an div with ID (suppose, you can create your div with other ID) captcha_container
<script src='https://www.google.com/recaptcha/api.js'></script>

var captchaContainer = grecaptcha.render('captcha_container', {
    'sitekey' : 'Your sitekey',
    'callback' : function(response) {
        console.log(response);
    }
});
Now time to validate reCAPTCHA from server side.
<?php
$url = 'https://www.google.com/recaptcha/api/siteverify';

$data = array(
    'secret' => 'YOUR_SECRET',
    'response' => $_POST["g-recaptcha-response"]
);

$options = array(
    'http' => array(
        'method' => 'POST',
        'content' => http_build_query($data)
    )
);

$context = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success = json_decode($verify);

if ($captcha_success->success == false) {
    echo "<p>You are a bot! Go away!</p>";
}
else if ($captcha_success->success == true) {
    echo "<p>You are not not a bot!</p>";
}