I created an Android library named "core" that use the Logger library (https://github.com/orhanobut/logger).
Here its build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.orhanobut:logger:1.15'
compile 'com.android.support:appcompat-v7:25.0.1'
testCompile 'junit:junit:4.12'
}
Then I build a .aar of core library.
I add this library as a dependency into my application, by putting the .aarcore files in the libs folder.
That's the build.gradle of my application :
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "com.package.test"
minSdkVersion 16
targetSdkVersion 25
multiDexEnabled true
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
jcenter()
flatDir {
dirs 'libs'
}
}
dependencies {
compile 'com.mypackage:core:1.0#aar'
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:multidex:1.0.1'
}
As you can see I have multidex activated.
It compile just fine but at runtime, at the first call to Logger I get an exception:
stack=java.lang.NoClassDefFoundError: Failed resolution of: Lcom/orhanobut/logger/Logger;
Even if I set transitive = true
compile (com.mypackage:core:1.0#aar) {
transitive=true
}
it doesn't work.
Thank you !
I had to do a couple of things in concert to get it to work.
1) In your library project
Add the following to your project-level gradle file:
buildscript {
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
}
}
Be sure to use the right version of the plugin for your gradle version, check here.
Add the following to your module-level gradle file:
apply plugin: 'com.github.dcendents.android-maven'
group='com.github.YourPackage'// Doesn't have to be github, just an example
Also in this file, make sure your dependencies (the ones you want to be transitive) use api instead of implementation if you are using Gradle 3.4+. If you are using a version of Gradle <3.4, compile is the way to go. Example:
dependencies {
api 'com.google.android.gms:play-services-location:15.0.1'
}
2) In your app project (which uses the library)
Add the following to your module-level gradle file:
dependencies {
implementation('com.github.YourPackage:yourRepo:version#aar') {
transitive=true
}
}
Add the following to you project-level gradle file (but this will depend on where your library is served from, for me it's jitpack):
allprojects {
repositories {
...
maven {
url 'https://jitpack.io'
credentials { username 'yourAuthKey' }// Only for private repositories
}
}
}
Note: You shouldn't add the auth key as a string in build.gradle, put it in a property in your gradle.properties file.
Note 2: JitPack allows you to use for example development-SNAPSHOT as a version number for a gradle dependency. Android Studio caches these dependencies and will not re-download it when you push to your development branch. To overcome this, use commit hashes as version numbers during development or clear (delete) the cache files, located on Windows at ~/.gradle/caches/modules-2/metadata-x.xx/descriptors/com.github.YourPackage/yourRepo. (Needles to say, I learned this the hard way).
Related
I am creating a blank project in which I add mapbox navigation package compile 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.6.3' in the gradle file. It shows error in appcompat file like this:-
Error:Failed to resolve: com.android.support:appcompat-v7:26.1.0
eventhough I use different version of Appcompat file. Before adding navigation package, everything was fine. Can someone please help me to find out this weird error.
Here is my app's gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.example.aadhilahmed.test7"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.6.3'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
Specify:
compile ('com.mapbox.mapboxsdk:mapbox-android-navigation:0.6.3') {
transitive = false;
}
Try this.Starting from version 26 of support libraries make sure that the repositories section includes a maven section
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
For Gradle build tools plugin version 3.0.0
allprojects {
repositories {
jcenter()
google()
}
}
According to the Android Espresso documentation to date:
Add Espresso dependencies
To add Espresso dependencies to your project, complete the following steps:
Open your app’s build.gradle file. This is usually not the top-level build.gradle file but app/build.gradle.
Add the following lines inside dependencies:
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.0'
androidTestCompile 'com.android.support.test:runner:1.0.0'
I created a new project and the generated app/gradle file was like this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.app.test"
minSdkVersion 24
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
testCompile 'junit:junit:4.12'
}
When I change it to the following:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.app.test"
minSdkVersion 24
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:26.+'
testCompile 'junit:junit:4.12'
// App's dependencies, including test
compile 'com.android.support:support-annotations:22.2.0'
// Testing-only dependencies
androidTestCompile 'com.android.support.test:runner:1.0.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.0'
}
I get the following errors:
Error:(29, 24) Failed to resolve: com.android.support.test:runner:1.0.0
Install Repository and sync project
Error:(30, 24) Failed to resolve: com.android.support.test.espresso:espresso-core:3.0.0
Install Repository and sync project
I have tried clicking on the "Install repository and sync project" link but nothing happens. I have also tried looking through the SDK manager, but I can't really see anything.
As the solution from comment is solving the problem I am adding it as an answer for others:
Be sure to add Google's maven link to main build.gradle file:
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
Simply,
Adding google() into allprojects > repositories will do the trick here...
allprojects {
repositories {
google()
jcenter()
}
}
you can downgrade the version, somehow sometimes the targeted version is unavailable from the repo.
use IDE-UI to select the latest version:
Right click on your project folder, select Open Module Settings
search for the package you want in the dependencies, for example
This way, you'll never get an un available version
(of course remember to set proper repo in build.gradle, too)
use these versions
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
I'm developing a library for android that will be used in many applications.
The library depends on some other libraries.
For example, it uses Dagger 1.2, so if an app that will include my library will useDagger 2.0, the project won't build because of conflicts.
What can I do?
I tried Jarjar, with this workspace gradle
buildscript {
repositories {
jcenter()
mavenLocal()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'net.vrallev.gradle:jarjar-gradle:1.1.0'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
this is the app gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.xxx.yyy.sdklauncherapp"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile project(':library')
compile 'com.google.dagger:dagger:2.2'
}
and this is my library gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
apply plugin: 'net.vrallev.jarjar'
jarjar {
jarJarFile 'jarjar-1.4.jar'
rules = [
'rule com.squareup.dagger.** ext.com.squareup.dagger.#1'
]
srcExcludes = ['META-INF/**']
outputName 'build_repackaged.jar'
outputDir 'libs'
ignoreJarJarResult false
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
jarjar 'com.squareup.dagger:dagger:1.2.2'
}
I'm working with #Beni in the same problem and we get some working solution to this problem. The bad news are related with gradle version plugin. With this solution you are tied to gradle 1.3 version because espresso gradle script we are using is looking for a task that is present in 1.3 gradle versión and not in the following versions.
So this is not the best approach ever, but it works and solves our problem. Anyway if someone can help to not be tied to any gradle version it would be great.
I give you a link with our solution: https://github.com/GigigoGreenLabs/JarJarDagger2Example
You have similar tools to jarjar that you can use, have a look at:
https://github.com/musketyr/gradle-fatjar-plugin
Gradle shadow plugin or maven shade plugin if you are using maven.
Hope this helps!
I have new clear android app. I want to add volley to my app to make HTTP request. I am developing app with Android studio using Gradle. Here is screenshot for my app:
Ass you see i am going to add volley to my app adding this line of code com.mcxiaoke.volley:library-aar:1.0.0 to build.gradle file
I tried to add Retrofit too. But it gives same type of error: Failed to resolve: bla bla bla.
So i think in my situation gradle can not sync any other library different than com.android......
And i have tried add this lines of code to my build.gradle too. but nothing is changing
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
repositories {
mavenCentral()
}
}
Please advise
Edit: After comments added build.gradle files for project and for module.
Project:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
}
}
allprojects {
repositories {
jcenter()
}
}
Module:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "domain.testgradle"
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.mcxiaoke.volley:library:1.0.19'
}
Volley is now published by the Android Open Source Project on jCenter:
dependencies {
compile 'com.android.volley:volley:1.0.0'
}
You can use
com.mcxiaoke.volley:library-aar:1.0.1
Or its latest version (Not aar)
compile 'com.mcxiaoke.volley:library:1.0.19'
And set
buildToolsVersion "23.0.1"
and use
'com.google.code.gson:gson:2.3'
Add this
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:+'
compile 'com.mcxiaoke.volley:library:1.0.19'
}
My (Android Studio) project has two modules: an Android-Library (app) Module and a Google Backend Module. The app module accesses the backend. in my gradle dependencies (app) I have:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(path: ':Backend', configuration: 'android-endpoints')
compile ('com.google.android.gms:play-services-wearable:6.1.71') {
exclude module: 'support-v4'
}
I want to use the app library in other projects, so I tried a) exporting it to a aar-library and b) exporting it into a jar-library. It works during compile time. I can use all classes of the app-module. But when running this app, it gives me an error, that it does not find the class that is the Interface between app and backend modules (as part of the backend).
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/cluedup/voicecontrols/backend/apiVoiceForm/model/VoiceFormBean;
Does anybody have an idea of how I can get this to work?
EDIT: Here are the complete gradle-files:
Library-App:
apply plugin: 'android-library'
android {
compileSdkVersion 21
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "com.cluedup.voicecontrols"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
flatDir {
dirs 'libs', '../backend/libs'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(path: ':Backend', configuration: 'android-endpoints')
compile ('com.google.android.gms:play-services-wearable:6.1.71') {
exclude module: 'support-v4'
}
}
Importing app
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "com.cluedup.voicecontrols.sample"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile ('com.google.android.gms:play-services-wearable:6.1.71') {exclude module: 'support-v4'}
compile(name:'app-debug', ext:'aar'){transitive=true}
}
Here is what works for me - it seems like a hack for me, though! So if someone has a better solution, I'll still would be glad to hear about
Step 1: Create a jar of the backend-module. Actually, the jar of the backend is produced as byproduct when trying to publish the whole app in the sonatype central repository. I followed this example (http://gmariotti.blogspot.de/2013/09/publish-aar-file-to-maven-central-with.html) to prepare for the upload.
As a result, I found a jar-file of the backend in the ../backend/build/libs Directory.
Step 2: Remove the dependency of the app to the backend-module and replace it with a dependency to the created jar-file
// DO NOT USE compile project(path: ':Backend', configuration: 'android-endpoints')
compile(name:'Backend-0.0.2', ext:'jar'){transitive=true}
Step 3: create the aar-file of the app with gradlew assemble
Step 4: add the jar of the backend and the aar of the app as dependencies in the importing Project
compile(name:'app', ext:'aar'){transitive=true}
compile(name:'Backend-0.0.2', ext:'jar'){transitive=true}