How to exlude a jar file from an Android dependency? - java

I've read and tried a lot of post solution for this problem but there is something I am doing wrong because it is not working for me.
I have just updated Android to AndroidX which came with the dependency androidx.preference:preference:1.1.0-alpha04. This dependency included the jar file named com.google.guava:listenablefuture:1.0#jar. The problem is that com.google.common.util.concurrent.ListenableFuture is found twice in the dependencies and this gives me an error when I build the project. I also found that the whole jar file (androidx.preference:preference:1.1.0-alpha04) can be removed as the file that is in there is already in another dependency library.
I have tried to add some notations to exclude the library but none of them worked out unfortunatelly. The jar file keeps being added once I build the project.
build.gradle
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation('org.ros.android_core:android_10:[0.3, 0.4)') {
exclude group: 'junit'
exclude group: 'xml-apis'
}
// e.g. official msgs
implementation 'org.ros.rosjava_messages:std_msgs:0.5.11'
implementation 'org.ros.rosjava_messages:sensor_msgs:1.12.7'
implementation 'org.ros.rosjava_messages:geometry_msgs:1.12.7'
implementation 'org.ros.rosjava_bootstrap:message_generation:0.3.3'
implementation 'com.android.support.constraint:constraint-layout:2.0.0-alpha5'
implementation ("androidx.preference:preference:1.1.0-alpha04", {
exclude group: 'com.google.guava'
})
implementation 'com.google.code.gson:gson:2.8.5'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.27.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
repositories {
mavenCentral()
}
The dependency that can be excluded:
What am I doing wrong or how should I actually exclude the jar file from the dependency?
EDIT: The reason I want to do this is because I get the following error:
Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules guava-12.0.jar (com.google.guava:guava:12.0) and listenablefuture-1.0.jar (com.google.guava:listenablefuture:1.0)

I think this should work:
implementation ("androidx.preference:preference:1.1.0-alpha04", {
exclude group: 'com.google.guava'
})
EDIT
This answer probably should help you:
https://stackoverflow.com/a/54717425/7947225
Add in your gradle.properties:
android.useAndroidX=true
android.enableJetifier=true
In your build.gradle, add
configurations {
all*.exclude group: 'com.google.guava', module: 'listenablefuture'
}
Then Clean and finally Build the project.

Related

Implementation 'com.google.android.gms:play-services-ads:18.1.0'

I want to add ads to my project but when i add the line below, it doesn't work:
implementation 'com.google.android.gms:play-services-ads:18.1.0'
these are the dependencies from the build.gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.gridlayout:gridlayout:1.0.0'
implementation 'com.google.android.gms:play-services-ads:18.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
It is likely, that you might not have added the Google Services Gradle Plugin to the project, as well as the google-services.json file (which can be downloaded from the Firebase console) - and so your application fails to authenticate.
Adding the com.google.android.gms.ads.APPLICATION_ID into the AndroidManifest.xml has the same effect, because the Play Services plugin adds the values from google-services.json at build-time.
... when only using AdMob, the second option should be preferred.
Do you have inside top-level build.gradle file reference to the google() repo or to maven { url "https://maven.google.com" }? Do you have some specific error inside log?
Try to create a new project and copy old project files and paste in a new one. If it should help!

Manifest merger failed after import a module

Hi I am developing a sdk, and when I add these SDK into a app proyect I get the following error:
Manifest merger failed : Attribute application#appComponentFactory
value=(android.support.v4.app.CoreComponentFactory) from
[com.android.support:support-compat:28.0.0]
AndroidManifest.xml:22:18-91
is also present at [androidx.core:core:1.0.1] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).
Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:22:5-125:19 to override.
The build.gradle are the followings
App build.gradle
dependencies {
implementation project(':sdkA')
implementation project(':sdkB')
implementation 'org.webrtc:google-webrtc:1.0.28032'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:preference-v7:28.0.0'
implementation 'com.android.support:preference-v14:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:gridlayout-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'io.michaelrocks:libphonenumber-android:8.4.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.android:flexbox:1.0.0'
implementation 'com.github.woxthebox:draglistview:1.6.3'
implementation 'com.balysv:material-ripple:1.0.2'
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.16'
implementation('com.crashlytics.sdk.android:crashlytics:2.8.0#aar') {
transitive = true
}
// Google Lifecycle Components
implementation "android.arch.lifecycle:extensions:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
}
build.gradle of sdkA
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'org.webrtc:google-webrtc:1.0.28032'
implementation('io.socket:socket.io-client:1.0.0') {
exclude group: 'org.json', module: 'json'
}
implementation('com.crashlytics.sdk.android:crashlytics:2.8.0#aar') {
transitive = true
}
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
// Google Lifecycle Components
implementation "android.arch.lifecycle:extensions:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
implementation 'androidx.appcompat:appcompat:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
build.gradle of sdkB
dependencies {
implementation "com.android.support:appcompat-v7:27.1.1"
implementation "com.android.support:recyclerview-v7:27.1.1"
}
How can solve it?
Thanks
Ok I've been tackling this issue myself for the last day or so. nightmare.
but I have it working now at least for the project I've been working on which is quite large and with lots of additional android dependencies.
See this issue where Mike Hardy has been a great help. https://github.com/mikehardy/jetifier/issues/27
I would recommend to avoid AndroidX until 0.60.0 has landed.
SOURCE OF THE PROBLEM
the source of the problem for most of us is the + range selector in gradle dependencies.
as shown here as an example in react-native-google-analytics-bridge:
compile "com.google.android.gms:play-services-analytics:${safeExtGet('googlePlayServicesVersion', '+')}"
compile "com.google.android.gms:play-services-tagmanager-v4-impl:${safeExtGet('googlePlayServicesVersion', '+')}"
for most of us we're not setting a googlePlayServicesVersion value in the top level android/build.gradle
so we'll want to specify googlePlayServicesVersion = "16.+" + because theres various other google service packages available and they're not all at the same version number. this will capture 16.X.X and not go above. 17.X.X holds further issues.
And we also want to set supportLibVersion to 28.0.0 which is used by the android support libraries: com.android.support:appcompat-v7 the most common.
SOLUTION
android/build.gradle
buildscript {
ext {
buildToolsVersion = "28.0.3"
minSdkVersion = 21
compileSdkVersion = 28
targetSdkVersion = 28
supportLibVersion = "28.0.0"
googlePlayServicesVersion = "16.+"
}
...
AndroidManifest.xml
top line:
<manifest
xmlns:tools="http://schemas.android.com/tools"
application tag:
tools:replace="android:appComponentFactory"
android:appComponentFactory="android.support.v4.app.CoreComponentFactory"
ensure your gradle-wrapper.properties using 4.10.1
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
and finally ensure you are not using android X.
gradle.properties:
android.enableJetifier=false
android.useAndroidX=false
Additional Step (You might need this)
failing that doesn't work try adding jetifier as well. We'll use this to run through your node_modules and ensure everything is using the non androidx libraries.
npm i jetifier --save-dev or yarn add jetifier --dev
then add to postinstall script
"scripts": {
"postinstall": "jetify -r"
}
Try applying the suggestion, adding
tools:replace="android:appComponentFactory"
to the application tag

cannot resolve symbol for v4 media and app classes

I'm trying to import the following classes for my media player, but my app does not recognize them:
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v7.app.NotificationCompat;
I've tried as others suggested (File->Invalidate Caches/restart), but the error remains.
gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:recyclerview-v7:28.0.0-rc02'
implementation 'com.android.support:cardview-v7:28.0.0-rc02'
implementation 'com.android.support:design:28.0.0-rc02'
implementation group: 'com.google.code.gson', name: 'gson', version: '2.7', changing: true
}
Add this dependencies to your gradle ;
com.android.support:support-v4:YOUR_VERSION
in long way ;
Project Structure --> Dependencies --> Click Plus sign -> Go to
"Library dependency"
Select which support library "support-v4
(com.android.support:support-v4:YOUR_VERSION)"
Be sure about Android Support Library has been added to your
dependencies.
Rebuild and work it.
Just Add Dependencies in our Gradle File
dependencies {
......
implementation 'com.android.support:support-v4:VERSION'
.....
}
Ex
dependencies {
......
implementation 'com.android.support:support-v4:27.0.1'
.....
}
For android x based projects, add this
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

How to import javax.annotation.Nullable?

I use Android Studio 3.x and Gradle build script.
How can I import javax.annotation.* packages?
Here is my Gradle depedencies example.
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:support-v4:27.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
implementation 'com.google.code.findbugs:jsr305:3.0.2'
You can use the following if You olny need annotation feature
implementation "javax.annotation:jsr250-api:1.0"
But need to very careful while using this, Reason being these classes are provided by multiple libraries like
implementation 'com.google.code.findbugs:jsr305:3.0.2'
implementation 'javax.inject:javax.inject:1'
implementation 'org.jetbrains.kotlin:kotlin-reflect:$versions.kotlin'
If You are using them already either You don't include that one or use exclude with them to avoid any build errors.
implementation (org.jetbrains.kotlin:kotlin-reflect:$versions.kotlin){
exclude module: 'jsr305'
exclude module: 'jsr250'
}

Program type already present com.google.gson.FieldAttributes

My android app refuses to build with the following error:
Program type already present: com.google.gson.FieldAttributes
Message{kind=ERROR, text=Program type already present: com.google.gson.FieldAttributes, sources=[Unknown source file], tool name=Optional.of(D8)}
My build.gradle dependencies
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:animated-vector-drawable:27.1.1'
implementation 'com.android.support:customtabs:27.1.1'
implementation 'com.android.support:support-media-compat:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
api 'com.github.Steveice10:MCProtocolLib:1.12.2-2'
api 'com.github.Steveice10:OpenNBT:1.0'
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.google.android.gms:play-services-ads:15.0.1'
implementation 'com.google.android.ads.consent:consent-library:1.0.6'
implementation 'com.github.GrenderG:Prefs:1.3' //Propably this cause problem
implementation('com.crashlytics.sdk.android:answers:1.4.2#aar') {
transitive = true
}
implementation('com.crashlytics.sdk.android:crashlytics:2.9.4#aar') {
transitive = true
}}
It probably started to happen when I added com.github.GrenderG:Prefs dependency
Add this line to build.gradle file.
configurations {
all*.exclude group: 'com.google.code.gson'
}
After 3 hours of googling around I finnaly found an Answer:
GPDR compilance library caused the problem
implementation ('com.google.android.ads.consent:consent-library:1.0.6') {
exclude module: 'gson'
}
add this code to your build.gradle(app) -
dependencies {
configurations {
all*.exclude group: 'com.google.code.gson'
}
This should solve your problem.
Add Dependency to build.gradle file
implementation 'com.google.code.gson:gson:2.8.2'

Categories

Resources