Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Tuesday, January 30, 2018

Android - Fix gradle Could not initialize class AndroidSdkHandler

Fix gradle Could not initialize class AndroidSdkHandler

I have Java 8 and 9 installed on my machine, I did not have problems until one day I tried to run gradle command. I google for the answer of course, some suggested to remove Java 9. But I did not like to mess up the system by installing/uninstalling software. So I changed JAVA_HOME to point java 8, and the problem solved.

Environment

  • macOS

Fix

JAVA_HOME was set to point to the latest Java in ~/.bash_profile

export JAVA_HOME=$(/usr/libexec/java_home)
# java -version
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)

Gradle throws error
* What went wrong:
A problem occurred configuring project ':app'.
> Failed to notify project evaluation listener.
   > Could not initialize class com.android.sdklib.repository.AndroidSdkHandler

Change JAVA_HOME to point to Java 8

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home

To view the specific version of Java installed, look the directory /Library/Java/JavaVirtualMachines/

# source ~/.bash_profile
# java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

Tuesday, September 26, 2017

Android - set host IP on app

Android - dynamic host IP

I want to test my Android RESTful API requests against the mock web server running on the same PC. The PC has dynamically assigned IP address, so the mock web server might also have different IP address when I restart the server. I could manually change the server IP on Android source code when new IP is assigned, but it’s not practical because not only do I modify the source code, it will also block the tests on daily build machine. The code snippet is to demonstrate how to dynamically set the host IP address on Android.

Setup

  • Android Studio

Development

build.gradle

android {
    . . .
    buildTypes {
        debug {
            buildConfigField "String", "SERVER_ADDR", '\"' + getHostIp() + '\"'
        }
        release {
            . . .
        }
    }
}

def getHostIp() {
    String result = "127.0.0.1";
    Enumeration ifaces = NetworkInterface.getNetworkInterfaces();
    while (ifaces.hasMoreElements()) {
        NetworkInterface iface = ifaces.nextElement();
        if (!iface.isLoopback()) {
            for (InterfaceAddress ifaceAddr : iface.getInterfaceAddresses()) {
                if (ifaceAddr.getAddress().isSiteLocalAddress()) {
                    result = ifaceAddr.getAddress().getHostAddress();
                    break;
                }
            }
        }
    }
    return result;
}

Now the host IP can be obtained from SERVER_ADDR

package com.example;

import static com.example.test.BuildConfig.SERVER_ADDR;

@RunWith(AndroidJUnit4.class)
public class RESTApiTest {
    private final static String MOCK_SERVER = "http://" + SERVER_ADDR + “:5438/API/v1.0/“;

    @Test
    Public void testHelloWorld {
        . . .
    }
}

In case you are running the tests on an actual device, make sure the device is also running on the same subnet.