Can't programmatically get resource from another module - android

I have an android project with several gradle modules. Dependencies beetween modules look like this:
app <-- coremodule <-- featuremodule
There are a resources in coremodule (strings and colors).
When I use them in layout from featuremodule everything is OK, they are avaliable and work as expected.
But when I try to get them programmaticully in Activity from featuremodule I get an exception: Unresolved reference: R
So android:text="#string/res_from_core_module" works and myTextView.setText(R.string.res_from_core_module) doesn't work.
Have anyone ideas why it happens and how to solve this?

I think the R points to the Resources of your app. Check the imports at the beginning of the file.
You should explicitly point the Resource folder in the method like this:
myTextView.setText(com.coremodule.R.string.res_from_core_module)

The reason for such behavior was adding 'coremodule' dependency with compileOnly.
build.gradle for app module looked like:
dependencies {
...
compileOnly project(path: ':coremodule')
...
}
if change compileOnly with implementation (or api) everything is OK
dependencies {
...
implementation project(path: ':coremodule')
...
}

Add this to all modules to share resources, classes, functions
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
...
}

Latest answer !!! It will make everything accessible to your app module.
dependencies {
...
api project(': coremodule')
...
}

Please use like this:
myTextView.setText(getString(R.string.res_from_core_module));

Related

Duplicate class com.google.android.gms.internal.firebase_messaging.zza found in modules

Just started this error on Android build. Please help.
"Duplicate class com.google.android.gms.internal.firebase_messaging.zza found in modules jetified-firebase-iid-19.0.0-runtime.jar (com.google.firebase:firebase-iid:19.0.0) and jetified-firebase-messaging-22.0.0-runtime.jar (com.google.firebase:firebase-messaging:22.0.0)"
In build.gradle:
implementation "com.google.firebase:firebase-iid:21.1.0"
None of the answers above is correct. And none of them solves the problem.
The problem is in transitive dependencies.
You need to check which libs are causing the issue and do something like:
implementation 'com.google.firebase:firebase-messaging:22.0.0'
implementation ('com.google.firebase:firebase-iid:21.1.0') {
transitive = true
}
I resolved this problem by using the Firebase Android BoM
In my case the problem depends on the coexistence of messaging and functions.
So from this:
implementation 'com.google.firebase:firebase-messaging:22.0.0'
implementation 'com.google.firebase:firebase-functions-ktx:20.0.0'
To this:
implementation platform('com.google.firebase:firebase-bom:28.2.1')
implementation 'com.google.firebase:firebase-functions-ktx'
implementation 'com.google.firebase:firebase-messaging'
BOM 31.x does have currently this problem.
See issue here:
https://github.com/firebase/firebase-android-sdk/issues/4206
Resolved via issue here but not yet official released:
https://github.com/firebase/firebase-android-sdk/pull/4225
Comment:
it was not resolved yet, 31.0.1 only contained a fix to crashlytics crash bug. That's why this issue is still open. The team here will make a fix close the issue, then do a release likely with release notes indicating it is fixed, when it is fixed https://firebase.google.com/support/release-notes/android - here
A current workaround is to add the iid version explicitly until a new BOM version is released:
implementation platform('com.google.firebase:firebase-bom:31.0.0')
implementation 'com.google.firebase:firebase-functions'
implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.firebase:firebase-iid:21.1.0'
I encountered the same problem.
I fixed it by redefining my dependencies implementations like this:
In build.gradle:
implementation 'com.google.firebase:firebase-analytics:19.0.0'
implementation 'com.google.firebase:firebase-auth:17.0.0'
implementation 'com.google.firebase:firebase-messaging:22.0.0'
Please note my configuration in buildscript: com.android.tools.build:gradle:4.2.1
Then, clean and rebuild project.
Can you share more information about your setup?
Using react-native-push-notification ^3.1.9, I've got the same error.
To fix it :
in project/build.gradle:
buildscript {
ext {
googlePlayServicesVersion = "17.0.0"
firebaseMessagingVersion = "20.1.0"
firebaseVersion = "20.1.0"
// other settings
}
// other settings
}
in project/app/build.gradle:
dependencies {
implementation "com.google.firebase:firebase-messaging:20.1.0"
// other implementations
}
The reason : react-native-push-notification package specify dependency to firebase-messaging:+ : which ask to get the last version.
So, gradle upload the latest com.google.firebase:firebase-messaging:22.x.x which declare already existing classes in firebase-iid.
If you don't want to use firebase-bom because you don't trust firebase to keep all the proper versions in check then you can remove the duplicate.
Run ./gradlew app:dependencies (or replace app with your module)
Search for iid:19.0.0 (or whatever version is giving you issues)
Scroll up in the list to see where the library originates from.
You should see something like \--- com.google.firebase:firebase-iid:19.0.0
Keep scrolling until you find the dependency and module it originates in, marked with a + eg (+--- com.google.firebase:firebase-ml-vision)
Locate the dependency in your project and remove the duplicate iid
api("com.google.firebase:firebase-ml-vision:$version") {
exclude group: 'com.google.firebase', module: 'firebase-iid'
}

Found more than one jar in the 'lintChecks' configuration

I want to add custom lint rules to my projects, but I get error while syncing project.
Execution failed for task ':app:prepareLintJar'.
Found more than one jar in the 'lintChecks' configuration. Only one file is supported. If using a separate Gradle project, make sure compilation dependencies are using compileOnly
How can I check which library or module is adding another jar?
I fixed problem. My dependencies in my custom rules module was
dependencies {
api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
compileOnly 'com.android.tools.lint:lint-api:26.5.3'
compileOnly 'com.android.tools.lint:lint-checks:26.5.3'
}
I changed kotlin dependency to compileOnly and it worked
dependencies {
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
compileOnly 'com.android.tools.lint:lint-api:26.5.3'
compileOnly 'com.android.tools.lint:lint-checks:26.5.3'
}

Library module doesn't include dependencies in Android

I am working on a multi-module project and this project should have a Java Library module called test-shared to provide common test classes. However, I am facing an issue in which my module doesn't recognize dependencies and I cannot write my classes.
Things have been done
Enable offline mode and disable again.
Delete .gradle folder and sync.
Googled identical issues.
Invalidate cache & restart.
As a result, I don't have a working project. Your help would be welcome.
build.gradle
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2"
implementation "androidx.test:rules:1.2.0"
implementation "androidx.lifecycle:lifecycle-runtime:2.1.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.1.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.1.0"
}
sourceCompatibility = "8"
targetCompatibility = "8"
Example class
class Co {
val a = LiveData<Co>()
val b = Observer<Co>()
}
In this example, neither LiveData nor Observer are recognized. Could you please help me fix this weird issue?
Thanks,
If this is not the entirety of your library's gradle file, then you are missing the android plugins.
Please add the following at the top of your library's gradle file.
apply plugin: "com.android.library"
I will also recommend having a look at the documentation: https://developer.android.com/studio/projects/android-library

Android Studio dependencies of module not visible in other module

I have a module called "Common" as library, this module has few dependencies like: com.badlogicgames.gdx, com.squareup.wire etc. And it works fine, I use them inside of this module.
And I have another module called "Tracking", where in the gradle I have:
dependencies {
compile project(':Common')
}
And if I try there to import any public class of module "Common", it works fine, but if I try to import any class of library com.badlogicgames.gdxor com.squareup.wire, it says me "Cannot resolve symbol ..." and hightlight it red. And no code autocompleting for such classes.
However the project compiles and starts on the device without errors.
Has somebody any idea? I tried "clean and rebuild" for project, "invalidate cashes and restart" for Android Studio. Nothing helps.
in the module common you need to declare those transitive dependencies as api to expose them to other modules:
e.g. common/build.gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
api 'com.squareup.wire'
}
https://jeroenmols.com/blog/2017/06/14/androidstudio3/
Solution
Change compile to api
dependencies {
api project(':Common')
}
Reason
Because compile is deprecated, so it is been treated as implementation.
FYI compile and api (new keyword for compile) are same in which all internal modules are visible.
But new gradle project having compile keyword are treated as implementation. and in implementation internal modules are not visible to main project.
Suggestion
You should declare dependency in your gradle because it is not good to make leak of internal modules.

Cannot resolve symbol ViewModelProviders

I am working on Android ViewModel architecture component but I am getting the above mentioned error when trying to initialize a ViewModel in an AppCompatActivity.
import android.arch.lifecycle.ViewModelProviders;
ViewModelProviders.of(this).get(CounterViewModel.class);
There are a few questions and articles related to this, and they pointed towards adding the lifecycle:extensions and lifecycle:viewmodel dependencies in the app gradle file, but I am still getting the error.
implementation "android.arch.lifecycle:extensions:1.1.1"
implementation "android.arch.lifecycle:viewmodel:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
The package android.arch.lifecycle does not contain the class ViewModelProviders and it only has ViewModelProvider class.
What else needs to be added to access the ViewModelProviders class?
Edit :
Dependencies in app/build.gradle:
dependencies {
implementation project(':lifecycle')
implementation project(':base')
implementation "android.arch.lifecycle:extensions:1.1.1"
implementation "android.arch.lifecycle:viewmodel:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
}
android.arch.lifecycle:extensions:1.1.1 definitely has android.arch.lifecycle.ViewModelProviders. You can see it in Android Studio, if you open the "External Libraries" portion of the project tree and examine the library contents:
Some possible reasons for not finding the import include:
You have implementation "android.arch.lifecycle:extensions:1.1.1" in the wrong place (it should be in the dependencies closure of the module's build.gradle file, such as app/build.gradle)
You did not sync Android Studio with the Gradle build files (you are normally prompted to do this, but you can do it manually from File > Sync Project with Gradle Files from the Android Studio main menu)
You do not need both lifecycle:extensions and lifecycle:viewmodel in your build.gradle file, remove
implementation "android.arch.lifecycle:viewmodel:1.1.1"
and it should be fine now. Also, you may want to migrate to AndroidX and use the 2.0.0 versions of the library.
ViewModelProviders is now deprecated. Use ViewModelProvider instead.
If you are configuring in libary, you can modify the implementation to api

Categories

Resources