Saturday, May 11, 2024

Developing Android App with chart supported by MPAndroidChart

 MPAndroidChart (https://github.com/PhilJay/MPAndroidChart) is a powerful Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations. As Open Source Project, the source code is free available from GitHub. There is general introduction, javadocs, and example code. But I cannot find a user guide document for how to use this lib from an Android project. This project was actively development several years ago. So it is using Java and Gradle Groovy. Unfortunately, latest Android Studio only support Gradle Kotlin, and no longer provides option to select java as language. I was having a lot of problem to run the example code. Also tried following other online example/tutorial using MPAndroidChart, but no luck either. That makes me decide to make this post to write down notes I have.

First, as mentioned here and several other posts, you would need to add 'jitpack.io' to your Project level Gradle file like this:

repositories {
maven { url 'https://jitpack.io' }
}

However, with recent Android Studio created project, if you add above to your root build.gradle file, you will get error like "Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by build file 'build.gradle'". The correct way is adding it to the settings.gradle file like this:

dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}

Second, if the project is using Gradle Kotlin, then would need to update settings.gradle.kts like this:

dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { setUrl("https://jitpack.io") }
}
}

Note, the syntax is different for the two types gradle file.

0 Comments:

Post a Comment