EzDevInfo.com

gradle interview questions

Top gradle frequently asked interview questions

Android buildscript repositories. jcenter VS mavencentral

The last time I used Android Studio, it generated .gradle files with mavencentral() buildscript repositories whereas now there's jcenter().

Could anyone explain the issues connected with this. Are there any other repos? When should we switch them? What impact do they have on projects, modules, libs? Any other essentials for Android developers?

Who's responsible for maintaining those repos?


Source: (StackOverflow)

Gradle build without tests

I want to execute gradle build without executing the unit tests. I tried:

$ gradle -Dskip.tests build

That doesn't seem to do anything. Is there some other command I could use?


Source: (StackOverflow)

Advertisements

Android Studio - no debuggable applications

I tried to debug a release version of my Android application but Android Studio failed to attach a debugger. (It could not find Android procces of my running application).

Under devices console, there was only a message:

No debuggable applications


Source: (StackOverflow)

How to set -source 1.7 in Android Studio and Gradle

I'm getting following error when trying to compile my project in Android Studio:

Gradle: error: diamond operator is not supported in -source 1.6

I have 1.7 set as target in all project preferences I've found. Also the path displayed in project SDK's under 1.7 SDK is correct path to java 1.7 installation.

Even when I run java -version in terminal, it tells me I'm running on java 1.7.

I have tried to set JAVA_HOME env variable to this:

/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home

The error does not go away. How do I eliminate the error?


Source: (StackOverflow)

Android Studio: Add jar as library?

I'm trying to use the new Android Studio but i can't seem to get it working correctly.

I'm using the Gson-library to serialize/deserialize JSON-objects. But the library somehow isn't included in the build.

I created a new project with just a MainActivity. Copied gson-2.2.3.jar in the /libs folder and added it as a library (right click->Add as library). This includes the jar in android studio so it can be referenced from the source files.

When I try to run the project it cannot compile so i added

compile files('libs/gson-2.2.3.jar')

to the dependencies in de .gradle file. After that it compiles correctly but when running the application i get a ClassDefNotFoundException. Does anyone know what i'm doing wrong?


Source: (StackOverflow)

What is Gradle in Android Studio?

Gradle is bit confusing to me and also for new Android developer. Can anyone explain what gradle in Android Studio is and what its purpose is? Why is Gradle included in Android Studio?


Source: (StackOverflow)

Android Gradle plugin 0.7.0: "duplicate files during packaging of APK"

Using Android Gradle plugin 0.7.0 with the following build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.0'
    }
}

apply plugin: 'android'

repositories {
    maven { url "https://android-rome-feed-reader.googlecode.com/svn/maven2/releases" }
    maven { url "http://dl.bintray.com/populov/maven" }
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion '18.1.1'

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
    }
    buildTypes {
        release {
            runProguard true
            proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
        }
    }
    productFlavors {
        defaultFlavor {
            proguardFile 'proguard-rules.txt'
        }
    }
    sourceSets {
        instrumentTest.setRoot('src/instrumentTest')
    }
}

configurations {
    apt
}

ext.androidAnnotationsVersion = '2.7.1';

dependencies {
    compile 'com.android.support:support-v4:18.0.0'

    compile 'com.viewpagerindicator:library:2.4.1@aar'

    compile 'com.google.code.android-rome-feed-reader:android-rome-feed-reader:1.0.0-r2'
    compile 'org.jdom:jdom:1.1.1-android-fork'

    apt "com.googlecode.androidannotations:androidannotations:${androidAnnotationsVersion}"
    compile "com.googlecode.androidannotations:androidannotations-api:${androidAnnotationsVersion}"

    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.j256.ormlite:ormlite-android:4.47'
    compile 'com.j256.ormlite:ormlite-core:4.47'
    compile 'org.springframework.android:spring-android-core:1.0.1.RELEASE'
    compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
    compile 'com.prolificinteractive:actionbarsherlock:4.3.1@aar'

    compile 'com.google.code.geocoder-java:geocoder-java:0.15'

    compile files('libs/CWAC-Pager.jar')
}

android.applicationVariants.all { variant ->
    aptOutput = file("${project.buildDir}/source/apt_generated/${variant.dirName}")
    println "****************************"
    println "variant: ${variant.name}"
    println "manifest:  ${variant.processResources.manifestFile}"
    println "aptOutput:  ${aptOutput}"
    println "****************************"

    variant.javaCompile.doFirst {
        println "*** compile doFirst ${variant.name}"
        aptOutput.mkdirs()
        variant.javaCompile.options.compilerArgs += [
                '-processorpath', configurations.apt.getAsPath(),
                '-AandroidManifestFile=' + variant.processResources.manifestFile,
                '-s', aptOutput
        ]
    }
}

gives the error Duplicate files copied in APK META-INF/LICENSE.txt:

WARNING: Dependency commons-logging:commons-logging:1.0.4 is ignored for defaultFlavorDebug as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency commons-logging:commons-logging:1.0.4 is ignored for defaultFlavorRelease as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Please read http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html for information on the replacement for dynamic properties.
Deprecated dynamic property: "aptOutput" on "com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@5705013c", value: "/Users/david/Developer...".
****************************
variant: defaultFlavorDebug
manifest:  /Users/david/Developer/.../build/manifests/defaultFlavor/debug/AndroidManifest.xml
aptOutput:  /Users/david/Developer/.../build/source/apt_generated/defaultFlavor/debug
****************************
Deprecated dynamic property "aptOutput" created in multiple locations.
****************************
variant: defaultFlavorRelease
manifest:  /Users/david/Developer/.../build/manifests/defaultFlavor/release/AndroidManifest.xml
aptOutput:  /Users/david/Developer/.../build/source/apt_generated/defaultFlavor/release
****************************
:TravelGuard:compileDefaultFlavorDebugNdk UP-TO-DATE
:TravelGuard:preBuild UP-TO-DATE
:TravelGuard:preDefaultFlavorDebugBuild UP-TO-DATE
:TravelGuard:preDefaultFlavorReleaseBuild UP-TO-DATE
:TravelGuard:prepareComProlificinteractiveActionbarsherlock431Library UP-TO-DATE
:TravelGuard:prepareComViewpagerindicatorLibrary241Library UP-TO-DATE
:TravelGuard:prepareDefaultFlavorDebugDependencies
:TravelGuard:compileDefaultFlavorDebugAidl UP-TO-DATE
:TravelGuard:compileDefaultFlavorDebugRenderscript UP-TO-DATE
:TravelGuard:generateDefaultFlavorDebugBuildConfig UP-TO-DATE
:TravelGuard:mergeDefaultFlavorDebugAssets UP-TO-DATE
:TravelGuard:mergeDefaultFlavorDebugResources UP-TO-DATE
:TravelGuard:processDefaultFlavorDebugManifest UP-TO-DATE
:TravelGuard:processDefaultFlavorDebugResources UP-TO-DATE
:TravelGuard:generateDefaultFlavorDebugSources UP-TO-DATE
:TravelGuard:compileDefaultFlavorDebugJava
*** compile doFirst defaultFlavorDebug
/Users/david/Developer/.../src/main/java/com/travelguard/service/TravelGuardService.java:53: cannot find symbol
symbol  : class TravelGuardPrefs_
location: class com.travelguard.service.TravelGuardService
    TravelGuardPrefs_ travelGuardPrefs;
    ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/CategoriesActivity.java:14: cannot find symbol
symbol  : class AssistanceFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.AssistanceFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/CategoriesActivity.java:16: cannot find symbol
symbol  : class CategoryFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.CategoryFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/CategoriesActivity.java:17: cannot find symbol
symbol  : class ContactFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.ContactFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/CountriesActivity.java:25: cannot find symbol
symbol  : class TravelGuardPrefs_
location: package com.travelguard.service
import com.travelguard.service.TravelGuardPrefs_;
                              ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/CountriesActivity.java:44: cannot find symbol
symbol  : class TravelGuardPrefs_
location: class com.travelguard.ui.CountriesActivity
    TravelGuardPrefs_ travelGuardPrefs;
    ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/InstructionsActivity.java:18: cannot find symbol
symbol  : class AssistanceFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.AssistanceFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/InstructionsActivity.java:19: cannot find symbol
symbol  : class ContactFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.ContactFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/InstructionsActivity.java:21: cannot find symbol
symbol  : class InstructionsFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.InstructionsFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/MainActivity.java:36: cannot find symbol
symbol  : class TravelGuardPrefs_
location: package com.travelguard.service
import com.travelguard.service.TravelGuardPrefs_;
                              ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/MainActivity.java:40: cannot find symbol
symbol  : class AssistanceFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.AssistanceFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/MainActivity.java:41: cannot find symbol
symbol  : class ContactFragment_
location: package com.travelguard.ui.fragments
import com.travelguard.ui.fragments.ContactFragment_;
                                   ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/MainActivity.java:55: cannot find symbol
symbol  : class TravelGuardPrefs_
location: class com.travelguard.ui.MainActivity
    TravelGuardPrefs_ travelGuardPrefs;
    ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/TermsActivity.java:17: cannot find symbol
symbol  : class TravelGuardPrefs_
location: package com.travelguard.service
import com.travelguard.service.TravelGuardPrefs_;
                              ^
/Users/david/Developer/.../src/main/java/com/travelguard/ui/TermsActivity.java:23: cannot find symbol
symbol  : class TravelGuardPrefs_
location: class com.travelguard.ui.TermsActivity
    TravelGuardPrefs_ travelGuardPrefs;
    ^
Note: Starting AndroidAnnotations annotation processing
Note: AndroidManifest.xml file found: /Users/david/Developer/.../build/manifests/defaultFlavor/debug/AndroidManifest.xml
Note: Number of files generated by AndroidAnnotations: 22
Note: Generating source file: com.travelguard.service.SmartTravellerService_
Note: Generating source file: com.travelguard.service.SmartTravellerWebservice_
Note: Generating source file: com.travelguard.service.TravelGuardPrefs_
Note: Generating source file: com.travelguard.service.TravelGuardService_
Note: Generating source file: com.travelguard.service.TravelGuardWebservice_
Note: Generating source file: com.travelguard.ui.AdviceActivity_
Note: Generating source file: com.travelguard.ui.CategoriesActivity_
Note: Generating source file: com.travelguard.ui.ContactDetailActivity_
Note: Generating source file: com.travelguard.ui.ContactListActivity_
Note: Generating source file: com.travelguard.ui.ContactTravelGuardActivity_
Note: Generating source file: com.travelguard.ui.CountriesActivity_
Note: Generating source file: com.travelguard.ui.InformationActivity_
Note: Generating source file: com.travelguard.ui.InstructionsActivity_
Note: Generating source file: com.travelguard.ui.MainActivity_
Note: Generating source file: com.travelguard.ui.TermsActivity_
Note: Generating source file: com.travelguard.ui.TravelGuardActivity_
Note: Generating source file: com.travelguard.ui.fragments.AdviceDetailFragment_
Note: Generating source file: com.travelguard.ui.fragments.AdviceFragment_
Note: Generating source file: com.travelguard.ui.fragments.AssistanceFragment_
Note: Generating source file: com.travelguard.ui.fragments.CategoryFragment_
Note: Generating source file: com.travelguard.ui.fragments.ContactFragment_
Note: Generating source file: com.travelguard.ui.fragments.InstructionsFragment_
Note: Time measurements: [Whole Processing = 913 ms], [Generate Sources = 358 ms], [Extract Manifest = 163 ms], [Process Annotations = 158 ms], [Extract Annotations = 120 ms], [Validate Annotations = 82 ms], [Find R Classes = 28 ms],
Note: Time measurements: [Whole Processing = 0 ms],
Note: Time measurements: [Whole Processing = 0 ms],
warning: The following options were not recognized by any processor: '[androidManifestFile]'
Note: /Users/david/Developer/.../src/main/java/com/travelguard/ui/ContactDetailActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /Users/david/Developer/.../src/main/java/com/travelguard/service/SmartTravellerService.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:TravelGuard:preDexDefaultFlavorDebug UP-TO-DATE
:TravelGuard:dexDefaultFlavorDebug
:TravelGuard:processDefaultFlavorDebugJavaRes UP-TO-DATE
:TravelGuard:validateDebugSigning
:TravelGuard:packageDefaultFlavorDebug
Error: duplicate files during packaging of APK /Users/david/Developer/.../build/apk/TravelGuard-defaultFlavor-debug-unaligned.apk
    Path in archive: META-INF/LICENSE.txt
    Origin 1: /Users/david/.gradle/caches/modules-2/files-2.1/commons-httpclient/commons-httpclient/3.1/964cd74171f427720480efdec40a7c7f6e58426a/commons-httpclient-3.1.jar
    Origin 2: /Users/david/.gradle/caches/modules-2/files-2.1/commons-codec/commons-codec/1.2/397f4731a9f9b6eb1907e224911c77ea3aa27a8b/commons-codec-1.2.jar
:TravelGuard:packageDefaultFlavorDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':TravelGuard:packageDefaultFlavorDebug'.
> Duplicate files copied in APK META-INF/LICENSE.txt
    File 1: /Users/david/.gradle/caches/modules-2/files-2.1/commons-httpclient/commons-httpclient/3.1/964cd74171f427720480efdec40a7c7f6e58426a/commons-httpclient-3.1.jar
    File 2: /Users/david/.gradle/caches/modules-2/files-2.1/commons-httpclient/commons-httpclient/3.1/964cd74171f427720480efdec40a7c7f6e58426a/commons-httpclient-3.1.jar


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 21.957 secs

Things were working fine this morning before I updated Android Studio. Any ideas?

EDIT: I was able to temporarily fix it by running commands like

zip -d spring-android-core-1.0.1.RELEASE.jar META-INF/notice.txt

until all the duplicate errors went away.


Source: (StackOverflow)

How can I force gradle to redownload dependencies?

How can I tell gradle to redownload dependencies from repositories?


Source: (StackOverflow)

Gradle DSL method not found: 'runProguard'

I get an error after updating from my last project. Not a problem in my code but I'm having trouble with build.gradle. How can I fix it?

build.gradle code here:

apply plugin: 'android'

android {
    compileSdkVersion 21
    buildToolsVersion '20.0.0'

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }

    defaultConfig {
        applicationId 'com.xxx.axxx'
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 6
        versionName '1.0'
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
    compile files('libs/commons-codec-1.8.jar')
    compile files('libs/asmack-android-8-4.0.4.jar')
    compile 'com.android.support:support-v4:21.0.0'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.jakewharton:butterknife:5.1.1'
}

Gradle Sync message output:

Error:(27, 0) Gradle DSL method not found: 'runProguard()'
**Possible causes:
The project 'Atomic4Mobile' may be using a version of Gradle that does not contain the method.
**Gradle settings**
The build file may be missing a Gradle plugin.
**Apply Gradle plugin**

Source: (StackOverflow)

Error retrieving parent for item: No resource found that matches the given name after upgrading to AppCompat v23

I've always programed android with Eclipse and decided to start migrating to Android Studio. Decided to use the same SDK I already had for eclipse, then:

  • Started a new project
  • Set minimum SDK 4.0 (API Level 14)
  • Choose Blank Activity option
  • Used Default names for Activity Name and Layout Name
  • Hit Finish

After a few seconds Gradle finishes the build, and it throws me 2 errors with the following messages in file Teste4\app\build\intermediates/exploded-aar\com.android.support\appcompat-v7\23.0.0\res\values-v23\values-v23.xml

Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse'.

Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Button.Colored'.

Under File -> Project Structure -> Modules: app (left column) -> Properties tab, I have the following versions set up:

  • "Compile Sdk Version": Android 5.1 (API Level 22)
  • "Build Tools Version": 23.0.2

What should I do in order to fix this?

Already tried what was suggested here, but didn't work appcompat-v7:21.0.0': No resource found that matches the given name: attr 'android:actionModeShareDrawable'

Thanks!


Source: (StackOverflow)

appcompat-v7:21.0.0': No resource found that matches the given name: attr 'android:actionModeShareDrawable'

When attempting to use the latest appcompat-v7 support library in my project, I get the following error:

/Users/greg/dev/mobile/android_project/app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/21.0.0/res/values-v11/values.xml
Error:(36, 21) No resource found that matches the given name: attr 'android:actionModeShareDrawable'.
Error:(36, 21) No resource found that matches the given name: attr 'android:actionModeShareDrawable'.
Error:(36, 21) No resource found that matches the given name: attr 'android:actionModeShareDrawable'.
Error:(36, 21) No resource found that matches the given name: attr 'android:actionModeShareDrawable'.

How do I fix this?


Source: (StackOverflow)

Building and running app via Gradle and Android Studio is slower than via Eclipse

I have a multi-project (~10 modules) of which building takes about 20-30 seconds each time. When I press Run in Android Studio, I have to wait every time to rebuild the app, which is extremely slow.

Is it possible to automate building process in Android Studio? Or do you have any advice on how to make this process faster?

In Eclipse, thanks to automatic building, running the same project on an emulator takes about 3-5 seconds.

This is my build.gradle file (app module):

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':libraries:SharedLibs')
    compile project(':libraries:actionbarsherlock')
    compile project(':libraries:FacebookSDK')
    compile project(':libraries:GooglePlayServices')
    compile project(':libraries:HorizontalGridView')
    compile project(':libraries:ImageViewTouch')
    compile project(':libraries:SlidingMenu')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 16
    }
}

Source: (StackOverflow)

How to create a release signed apk file using Gradle?

I would like to have my Gradle build to create a release signed apk file using Gradle.

I'm not sure if the code is correct or if I'm missing a parameter when doing gradle build?

This is some of the code in my gradle file:

android {
    ...
    signingConfigs {
          release {
              storeFile file("release.keystore")
              storePassword "******"
              keyAlias "******"
              keyPassword "******"
         }
     }
}

The gradle build finishes SUCCESSFUL, and in my build/apk folder I only see the ...-release-unsigned.apk and ...-debug-unaligned.apk files.

Any suggestions on how to solve this?


Source: (StackOverflow)

Could not find com.google.android.gms:play-services:3.1.59 3.2.25 4.0.30 4.1.32 4.2.40 4.2.42 4.3.23 4.4.52 5.0.77 5.0.89 5.2.08 6.1.11 6.1.71 6.5.87

referencing the play-services via gradle stopped working for me - boiled it down - even the sample I used as a reference in the first place stopped working: https://plus.google.com/+AndroidDevelopers/posts/4Yhpn6p9icf

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':auth'.
> Failed to notify project evaluation listener.
   > Could not resolve all dependencies for configuration ':auth:compile'.
      > Could not find com.google.android.gms:play-services:3.1.36.
        Required by:
            gpsdemos:auth:unspecified

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.577 secs

I fear that just the version increased but that raises 2 questions: #1) what is the new one? #2) why is the old version gone?


Source: (StackOverflow)

How to add local .jar file dependency to build.gradle file?

So I have tried to add my local .jar file dependency to my build.gradle file:

apply plugin: 'java'

sourceSets {
    main {
        java {
            srcDir 'src/model'
        }
    }
}

dependencies {
    runtime files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar')
    runtime fileTree(dir: 'libs', include: '*.jar')
} 

And you can see that I added the .jar files into the referencedLibraries folder here: https://github.com/WalnutiQ/WalnutiQ/tree/master/referencedLibraries

But the problem is that when I run the command: gradle build on the command line I get the following error:

error: package com.google.gson does not exist
import com.google.gson.Gson;

Here is my entire repo: https://github.com/WalnutiQ/WalnutiQ


Source: (StackOverflow)