Search code examples
androidmavengradleandroid-studionumberpicker

Converting Android Numberpicker Maven Project to Gradle Project For Android Studio


So I've started using Android Studio and I wanted to use a number picker library for my project. Specifically this one:

https://github.com/SimonVT/android-numberpicker

This is a maven project and my project is gradle based. In order to use this project in my app I would need to convert it into a gradle library project. Easy! Or so I thought...

I used the following articles for guidance:

http://www.androidsx.com/how-to-link-an-android-library-project-with-gradle-in-android-studio/ http://ryanharter.com/blog/2013/07/17/migrating-android-projects-to-gradle/

My directory structure is as follows:

android-numberpicker/
   - android-numberpicker/
      - src/
         - main/
            - res/
            - java/
      - build.gradle
   - settings.gradle
   - build.gradle

My settings.gradle file

include ':android-numberpicker'

My build.gradle file:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.3'
    }
}

apply plugin: 'android-reporting'

/**
 * Task to generate a gradle wrapper.
 */
task wrapper(type: Wrapper) {
    gradleVersion = '1.9'
}

and my android-numberpicker/build.gradle file looks like:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.3'
    }
}

apply plugin: 'android-library'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 15
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 15
    }
}

Now I haven't even tried to integrate it with my project or android studio. I just want to see if I could build the project on its own. In other words i wanted to see if i had ported the project to gradle correctly. However when I run ./gradlew build (I am using the gradle wrapper) I get a series of errors such as:

NumberPicker.java:48: error: cannot find symbol
import android.view.accessibility.AccessibilityNodeProvider;
                                 ^

No idea why gradle is doing this?

I did notice in the original projects pom file the following dependency:

<dependencies>
    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

Which I originally disregarded because I would have thought the 'android-library' plugin would automatically resolve the android sdk. However, when I got the above error I decided to add it to my android-numberpicker/build.gradle as follows:

dependencies {
    compile "com.google.android:android:+"
}

I used compile, since the android-library doesn't seem to recognize provided as a valid scope. Upon running ./gradlew build I got a different error:

ERROR: Debug has an indirect dependency on Android API level 15, but minSdkVersion for variant 'debug' is API level 7

Again, not really sure why I am getting this error. Have reached a brick wall with regards to this issue. Any help would be greatly appreciated.

Cheers


Solution

  • Make these all correction mentioned below and sync your project with gradle.

    1) Remove this dependency from your build.gradle file

    compile "com.google.android:android:+"
    

    2) Remove this from android-numberpicker/build.gradle after applying android plugin

     repositories {
        mavenCentral()
     }
    

    No use of it, you already defined in root gradle file.

    3) Change you minSdkVersion and compileSdkVersion in android-numberpicker/build.gradle to 16 because class has been add in API Level 16

    Check this fro reference http://developer.android.com/reference/android/view/accessibility/AccessibilityNodeProvider.html

    Sync your project with gradle after all these changes and let me know if you face any issue.

    EDIT :

    I found this in NumberPicker Library code :

    class SupportAccessibilityNodeProvider {
    
            AccessibilityNodeProviderImpl mProvider;
    
            private SupportAccessibilityNodeProvider() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    mProvider = new AccessibilityNodeProviderImpl();
                }
            }
    
            public boolean performAction(int virtualViewId, int action, Bundle arguments) {
                if (mProvider != null) {
                    return mProvider.performAction(virtualViewId, action, arguments);
                }
    
                return false;
            }
    
            public void sendAccessibilityEventForVirtualView(int virtualViewId, int eventType) {
                if (mProvider != null) mProvider.sendAccessibilityEventForVirtualView(virtualViewId, eventType);
            }
        }
    

    Which shows library handles it for lower versions so you can make your compileSDKVersion as 16 and minSdkVersion as 7/8.

    Also have to add support v4 library in your project using this in dependency iside build.gradle file

    compile 'com.android.support:support-v4:19.0.0'