I am trying to use some of the awesome android arsenal libraries. For example: https://android-arsenal.com/details/1/703. If you go to main page and the package page it gives instructions on what to add to the build.gradle file. I recently updated Android Studio and the build.grade files are now in the same "Gradle Scripts" Tab. I want to know where to put the lines:
// stock actionBar
compile 'com.balysv.materialmenu:material-menu:1.x.x'
// Toolbar and ActionBarCompat-v21 (includes support-v7:21.0.x)
compile 'com.balysv.materialmenu:material-menu-toolbar:1.x.x'
// actionBarCompat-v20 (up to support-v7:20.0.0 - does not support Toolbar)
compile 'com.balysv.materialmenu:material-menu-abc:1.x.x'
// actionBarSherlock
compile 'com.balysv.materialmenu:material-menu-abs:1.x.x'
and/or
repositories {
maven {
url "https://jitpack.io"
}
}
and
dependencies {
compile 'com.github.balysv:material-menu:v1.5.1'
}
I am confused because of the two build.gradle files listed here:
The Project:Socio file looks like this:
The Module:app file looks like this:
You should put all dependencies in the module/build.gradle file.
For example
compile 'com.balysv.materialmenu:material-menu:1.x.x'
compile 'com.balysv.materialmenu:material-menu-toolbar:1.x.x'
compile 'com.github.balysv:material-menu:v1.5.1'
About the repositories you can put it in the top level o in module gradle file. It depends if any project uses the same repos.
For example: root/build.gradle
allprojects{
repositories {
jcenter()
maven {
url "https://jitpack.io"
}
}
}
or module/build.gradle
repositories {
jcenter()
maven {
url "https://jitpack.io"
}
}
Put them in the Module:app gradle file.
The other gradle file is for project-wide build configuration. From the documentation:
Android Studio projects contain a top-level project Gradle build file that allows you to add the configuration options common to all application modules in the project. Each application module also has its own build.gradle file for build settings specific to that module.
Related
I've create a flutter plugin with:
flutter create --template plugin flutter_plugin
I've put my aar file inside flutter_plugin/android/src/main/libs folder
I've modified flutter_plugin/android/build.gradle
and changed rootProject.allprojects section to
rootProject.allprojects {
repositories {
google()
jcenter()
flatDir {
dirs "src/main/libs"
}
}
}
And added dependencies section, after android {}:
dependencies {
implementation (name:"mylib",ext:"aar")
}
but when i try running with: flutter run
I get gradle exception, apparently it tried to look for my mylib.aar inside example directory: example/src/main/libs/mylib.aar and failed.
I can put my lib inside example dir, but i don't think it's the right way,
as i want my aar to be part of the plugin.
My solution to add local .aar depencies to a flutter plugin is as follows (based on the instructions here How to add .aar dependency in library module?):
create a libs folder in your android plugin source code (<plugin_name>/android/libs) (on top level of your plugin, not in the example project)
add your .aar files in this folder (e.g. myFirstDependency.aar)
add your .aar dependencies (without the .aar extension) to your plugins build.gradle file (<plugin_name>/android/build.gradle) e.g.
dependencies {
...
implementation (name: 'myFirstDependency', ext: 'aar')
implementation (name: 'myOtherDependency', ext: 'aar')
}
in order for gradle to be able to find these dependencies you need to instruct gradle to search in the libs folder inside of the plugin. Therefore add the following to your plugins build.gradle (<plugin_name>/android/build.gradle, same as for your dependencies) under rootProject.allprojects repositories section.
(NOTE: there is also a buildscripts a repositories section which you should not change for this).
rootProject.allprojects {
repositories {
google()
jcenter()
//Add this below <plugin_name> is whatever your plugin is called eg. url_launcher
flatDir {
dirs project(':<plugin_name>').file('libs')
// e.g. dirs project(':url_launcher').file('libs') - don't miss the ':'
}
}
}
With that, you don't have the .aar library dependencies directly in your plugin.
I created a libs directory in the android directory of my plugin. I placed the aar I generated in the libs directory:
I updated the build.gradle file for the android code to include the following:
rootProject.allprojects {
repositories {
google()
jcenter()
flatDir {
dirs 'libs'
}
}
}
And in the dependencies section:
implementation fileTree(dir: 'libs', include: ['*.aar'])
In my experience, adding direct plugin aar in another plugin project (flutter plugin's android project) is either very difficult or not allowed, so in that case, you have two options,
If your aar dependancy is available on gradle, use gradle that in your build file. You can search for it here
Or if you are building aar yourself from another project, then you can publish aar to your local maven and use that dependency in your flutter plugin by adding "mavenLocal()" to your plugins build file.
For example here is what I did to fix same issue:
My dependancy's build.gradle
group 'com.companyname.artifactname'
version '1.0-SNAPSHOT'
buildscript {
repositories {
mavenLocal()
google()
jcenter()
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}
allprojects {
repositories {
mavenLocal()
google()
jcenter()
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
}
apply plugin: 'com.android.library'
uploadArchives {
repositories {
mavenLocal()
}
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 2
versionName "2.0.1"
}
lintOptions {
abortOnError false
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:support-v4:23.2.1'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
}
apply plugin: 'maven-publish'
publishing {
publications {
library(MavenPublication) {
version "1.1"
artifact(bundleRelease)
}
}
}
Then just run following command to publish it to local maven repository
gradlew publishToMavenLocal
And then just add mavenLocal() in your flutter plugin's repositories section and add dependancy as
compile 'com.companyname.artifactname:libraryname:1.1#aar'
With the flatDir solution you will get a warning when you try to sync your project with the Gradle files:
using flatDir should be avoided because it doesn't support any meta-data formats.
You can put this in your build.gradle instead to get rid of the warning:
android {
sourceSets {
main.jniLibs.srcDirs += project(':<plugin_name>').file('libs')
}
}
dependencies {
implementation files('libs/<dependency_name>.aar')
}
Unfortunately, with both solutions you'll get this error when you try to build the project:
Direct local .aar file dependencies are not supported when building an AAR.
The resulting AAR would be broken because the classes and Android resources from any local .aar
file dependencies would not be packaged in the resulting AAR. Previous versions of the Android
Gradle Plugin produce broken AARs in this case too (despite not throwing this error).
The accepted answer seems to be correct. I put the library inside the app's project instead because both options of the accepted answer were not possible in my case.
For this I had to create a new directory in the android directory of my app (i.e. example/android/<dependency_name>) and put my .aar file and a new build.gradle in that directory.
android/build.gradle:
dependencies {
implementation project(':<dependency_name>')
}
example/android/<dependency_name>/build.gradle:
configurations.maybeCreate("default")
artifacts.add("default", file('<dependency_name>.aar'))
example/android/settings.gradle:
include ':<dependency_name>'
i managed to make it work by adding
maven { url("${project(':test_flutter_plugin').file('libs').path}/my_lib") }
into rootProject.allprojects
of plugin/android/build.gradle
I tried to add the Google location Service to my Android Project by referencing the official link.
As the tutorial said I added the following line to the dependencies section in the Project Gradle file.
implementation "com.google.android.gms:play-services-location:11.8.0"
But when i resynced the project i got the following error
Error:(19, 0) Could not find method implementation() for arguments [com.google.android.gms:play-services-location:11.8.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Open File
I am using Android Studio 3.0.1 and Gradle Version 4.1.
How to overcome this error?
please try this
Add in Build.gradel at app lavel
dependencies {
Implementation 'com.google.android.gms:play-services:9.6.0'
}
apply plugin: 'com.google.gms.google-services
// Top-level build file where you can add configuration options common
to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they
belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I think you have forgot to add maven repository in your app gradle file. Try by doing these steps. Open the build.gradle file for your application. Make sure that the repositories section includes a maven section with the "https://maven.google.com" endpoint. For example: allprojects {repositories {jcenter()maven {url "https://maven.google.com"}}}
Avoid using + in your dependencies as it android studio will try to update dependencies every 24 hours. Also replace compile by implementation for low APK size.
Edit 1:
As you told maven repository did not worked.
Check your project settings by going to Android Studio.
Click on file
click on project structure
There will option of Project in sidebar. Tap on it.
Check for the gradle and andrpid plugin version.
Put these values
Gradle Version - 4.1
Android plugin version - 3.0.1
Android Plugin Repository - jcenter,google()
Default Library Repository - jcenter, 'https://maven.google.com'
Reply me if it works
Here's build.gradle:
C:\Users\billb\StudioProjects\ShoppingList03>type build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
dependencies {
classpath "/com.android.tools.build.gradle:3.3/"
compile "com.android.support:support-core-utils:26.1.0"
}
}
allprojects {
repositories {
jcenter()
}
}
Error is on the compile: cannot find DefaultDependencyHandler.
I've reloaded Android SDK Platform-Tools and SDK Tools, both 26.0.1
Where is this DefaultDependencyHandler? Why is it required?
There's something in my android java code that requires this, evidently.
Any ideas out there? I've spent way too much time trying to track this down.
Add compile "com.android.support:support-core-utils:26.1.0" line in your app level build.gradle file instead of project level build.gradle file.
I'm using this library in my Android app. (https://github.com/yazeed44/MultiImagePicker)
Before now, I was adding it to my project this way:
compile 'net.yazeed44.imagepicker:imagepicker:1.3.0'
The problem with importing it that way is, as far as I know, that I can't override any code because I'll lose all the changes after building the project again. (I need to change some code)
For that reason, I have downloaded the source code and I've imported the project as a module with this name: 'imagepicker'
After that, I added this line to my app build.gradle:
compile project(':imagepicker')
and this to my settings.gradle (Android Studio did it)
include ':app', ':imagepicker'
After doing that, I try to run the project and Android studio shows this error:
Gradle 'Project' project refresh failed
Error:Plugin with id 'com.github.dcendents.android-maven' not found.
Since you are using the module locally you have to add in your top-level build.gradle or in the imagepicker/build.gradle same config added in the imagepicker build.gradle project.
buildscript {
repositories {
jcenter()
}
dependencies {
//ADD THESE DEPENDENCIES
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
}
An alternative can be modify the imagepicker/build.gradle removing the last 2 lines. But you have to test this way.
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
If you check these files you will find the
apply plugin: 'com.github.dcendents.android-maven'
In your case you don't need these files because they are useful only to uplaod in a maven repo the aar file.
I added below code in Project:gradle.build file and its resolved the problem :
allprojects {
repositories {
jcenter()
maven {
url "https://repo.commonsware.com.s3.amazonaws.com"
}
}
}
EDIT
If you still facing after adding above maven dependencies
Change url "https://repo.commonsware.com.s3.amazonaws.com" to url "https://s3.amazonaws.com/repo.commonsware.com".
I am adding this library to my project. I followed the instructions. I put th project under app/lib. the problem is when i add :
compile project(':lib:paralloid:paralloid')
compile project(':lib:paralloid:paralloidviews')
an error message in generated:
Error:(26, 0) Project with path ':lib:paralloid:paralloid' could not
be found in project ':app'.
Seems like you haven't specified mavenCentral() as a repository.
In build.gradle file, you need to specify which repository to use when resolving dependencies for building your project.
Add mavenCentral() repository in build.gradle file like this:
repositories {
mavenCentral()
}
And add library to your project
dependencies {
compile 'uk.co.chrisjenx.paralloid:paralloid:0.1.+'
}
Have you tried the other option they give?
dependencies {
compile 'uk.co.chrisjenx.paralloid:paralloid:0.1.+'
}