Issues after updating to Android Studio 2.0v

In this post, I just like to sum up all the issues faced after updating Android version.

After updating to Android Studio 2.0v, you may see following issues.

You’ll be asked to install latest gradle version to enable Instant Run. you think it helps and installs the latest Gradle version.

Now, your problems starts You may be able build project. But couldn’t able to run the project. When you run the project, you’ll see Out of memory exception as below. 😐

Error:java.lang.OutOfMemoryError: Java heap space.
Please assign more memory to Gradle in the project's gradle.properties file.
For example, the following line, in the gradle.properties file, sets the maximum Java heap size to 1,024 MB:
<em>org.gradle.jvmargs=-Xmx1024m</em>
<a href="http://www.gradle.org/docs/current/userguide/build_environment.html">Read Gradle's configuration guide</a><br><a href="http://docs.oracle.com/javase/7/docs/technotes/guides/vm/gc-ergonomics.html">Read about Java's heap size</a>

I tried different ways to increase the memory, like changing the memory values in gradle.properties file

org.gradle.jvmargs=-Xmx2048m -XX\:MaxPermSize\=512m

and changing values in studio64.exe.vmoptions file in C:\Program Files\Android\Android Studio\bin folder

-Xms512m
-Xmx2048m

but not worked. 🙁

The only thing that worked for me is adding javaMaxHeapSize value of dexOptions in app’s build.gradle file.

android{
  ....
  ....
  dexOptions {
     javaMaxHeapSize "2g"
  }
}

this fixes the OutOfMemoryError issue. The reason for this issue is the latest version gradle needs more memory to build the app.

And after fixing You may see Multiple dex files issue. For me I get this error

Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/zxing/BarcodeFormat;  😮

I tried following to fix this issue

defaultConfig {
..
// Enabling multidex support.
multiDexEnabled true
}

Which resulted same issue in different way 😡

Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/google/zxing/BarcodeFormat.class

in inconsistent location 'C:\Users\employee2\AppData\Local\Android\sdk\add-ons\addon-google_apis-google-19-1'
(Expected 'C:\Users\employee2\AppData\Local\Android\sdk\add-ons\addon-google_apis-google-19')

Error:Error converting bytecode to dex:

So, I removed that multiDexEnabled true line and search for the BarcodeFormat class usage in project. I finally find out that same dependencies library added in module’s and app’s  build.gradle file. I just commented dependencies library in app’s build.gradle file fixed the issue.  😎

So, finally After fixing all issues and you’ll be able to run the app. 

And then, now you made some code changes and run the app. App won’t run on device with latest code changes. In the Android Studio Run tab you’ll see “No changes”.  What! 😮

The reason for this Instant Run not recognizes certain code changes, and it also needs latest android API.

instant_run

So, the only option to see your code changes effect in app and run on device, is disable Instant Run. Please see following image to disable

after updating

That’s it now you able to see code changes by running the app on device.  😎

 

Hope it saves somebodies time. 💡

 

You may also interested in

 

Gradle DSL method not found – Android Studio

After importing Eclipse project to Android Studio. When you run the project, you may see following error message as, Gradle DSL not found: ‘android()’

Gradle DSL

Solution to fix this Gradle DSL method issue

open Top-level build.gradle file of the project

as_gbuild_file

And remove the following code lines.

android {
compileSdkVersion 21
buildToolsVersion '21.1.2'
}

And then click Sync menu button, fixes the issue.

sync

And another possible issue is,

Gradle DSL method not found: ‘runProguard()’

At times when you import a downloaded sample project in to Android Studio, see following error message as, Gradle DSL not found: ‘runProguard()’

app_gradlefile-2

Solution to fix this Gradle DSL method issue

open app’s build.gradle file of the project

app_gradlefile

In this file you can see following code snippet

buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

In the above code runProguard false causing the issue. Change that line to

minifyEnabled false

Now click Sync menu button, issue will be fixed. I see the reason for this is runProguard has been renamed to minifyEnabled in version 0.14.0 or more in Gradle.

Hope it helps somebody. 😎

You may be also interested in

Generate signed APK file with Specific name Android Studio

In Android Studio, when clicked menu Build-> Generate Signed APK file, the dialogs displayed for generating APK file, doesn’t allow you to edit the APK file name. It just applies default app name to build file, unlike ADT Eclipse.

Solution:

We have to write Groovy code in Gradle build file to, Generate singed APK file with Specific name.

Here i’m just giving an example how we can write it.
For eg: If your app name is helloWorld, If you generate APK build file, it will creates helloWorld-release.apk file. But if you want to generate Apk file with appending version name, and change filename like “HelloWorld-1.0v.apk”.

In project open build.gradle file, you should have defaultConfig as below

defaultConfig {
    applicationId "com.sample.helloworld"
    minSdkVersion 14
    targetSdkVersion 14
    versionCode 1
    versionName '1.0'
}

in this build.gradle file, define a method just above the dependencies, as below

def appendVersionName(variant, defaultConfig) {
    variant.outputs.each { output ->
        if (output.zipAlign) {
            def file = output.outputFile
            def fileName = file.name.replace("helloWorld-release.apk", "HelloWorld-" + defaultConfig.versionName  + "v.apk")
            output.outputFile = new File(file.parent, fileName)
        }

        def file = output.packageApplication.outputFile
        def fileName = file.name.replace("helloWorld-release.apk", "HelloWorld-" + defaultConfig.versionName + "v.apk")
        output.packageApplication.outputFile = new File(file.parent, fileName)
    }
}

dependencies {
    compile files('libs/android-support-v13.jar')
    compile 'com.android.support:appcompat-v7:21.0.2'
}

then search for buildTypes, and call the appendVersionName method as shown below,

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'

            applicationVariants.all { variant ->
                appendVersionName(variant, defaultConfig)
            }

        }
    }

That’s it. Now Goto Build-> Generate a Signed APK… just follow the dialog, click Finish, and you will see APK file created with your specified name.

Hope it helps somebody…

Cheers.. 🙂

ref link & credits: http://stackoverflow.com/a/22126638/341443

You may also interested in