Gradle 'implementation' dependency isn't compile with correct code - java

I have a compilation issue with implementation dependency when compiling with Gradle the following project that contains of 3 modules:
test-impl
test-lib
my-test
My error:
/Users/igor/projects/my-test/src/main/java/MyTest.java:4: error: cannot access TestImpl
lib.foo("");
^
class file for TestImpl not found
It is compiled when I change implemention to api or if I rename any of foo methods in TestLib class.
Gradle 6.0.1, Java 1.8.0_271-b09, OSX
Doesn't it look like a bug? Where to report?
All build.gradle files:
Module test-impl build.gradle:
apply plugin: 'java-library'
Module test-lib build.gradle:
apply plugin: 'java-library'
dependencies {
implementation project(':test-impl')
}
Module my-test build.gradle:
apply plugin: 'java-library'
dependencies {
api project(':test-lib')
}
All source codes:
TestImpl.java in test-impl module:
public class TestImpl {
}
TestLib.java in test-lib module:
public class TestLib {
public void foo(String s) {
}
private void foo(TestImpl impl) {
}
}
MyTest.java in my-test module:
public class MyTest {
public void test() {
TestLib lib = new TestLib();
lib.foo("");
}
}

Related

Microservice- error in bringing up config server org.springframework.boot.context.properties.bind.BindResult

I'm getting below error when trying to bring up config server. Looks incorrect jar version
any suggestions please
Error
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.springframework.cloud.config.server.composite.CompositeEnvironmentBeanFactoryPostProcessor.bindProperties(CompositeEnvironmentBeanFactoryPostProcessor.java:81)
The following method did not exist:
'java.lang.Object org.springframework.boot.context.properties.bind.BindResult.orElseCreate(java.lang.Class)'
The method's class, org.springframework.boot.context.properties.bind.BindResult, is available from the following locations:
*jar:file:/C:/Users/GOPU/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot/2.4.0-M1/503562062a9baee246bb7311526d08d16ae05758/spring-boot-2.4.0-M1.jar!/org/springframework/boot/context/properties/bind/BindResult.class*
The class hierarchy was loaded from the following locations:
org.springframework.boot.context.properties.bind.BindResult: file:/C:/Users/GOPU/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot/2.4.0-M1/503562062a9baee246bb7311526d08d16ae05758/spring-boot-2.4.0-M1.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.boot.context.properties.bind.BindResult
Build.gradle
plugins {
id 'org.springframework.boot' version '2.4.0-M1'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.cloud'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '13'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.cloud:spring-cloud-config-server'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.boot:spring-boot-starter'
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Hoxton.RELEASE'
}
}
test {
useJUnitPlatform()
}
Mainfile
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
#EnableConfigServer
#EnableDiscoveryClient
#SpringBootApplication
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}

Dagger 2 - cannot be provided without an #Inject constructor or an #Provides-annotated method

I have a problem with Dagger 2 dependency injection. I'm using it in Android project but in a java-library module and in unit testing.
ApiTestModule.kt
#Module
class ApiTestModule {
#Provides
#Named("testDatasource")
fun provideGithubTestDatasource(): GithubDatasourceImpl {
return GithubDatasourceImpl(Mockito.mock(GithubService::class.java))
}
}
TestComponent.kt
#Singleton
#Component(modules = [ApiTestModule::class])
interface TestComponent {
fun inject(test: GithubDatasourceImplTest)
}
GithubDatasourceImplTest.kt
class GithubDatasourceImplTest {
#set:[Inject Named("testDatasource")]
lateinit var datasource: GithubDatasourceImpl
#Before
fun setUp() {
val component = DaggerTestComponent.builder()
.apiTestModule(ApiTestModule())
.build()
component.inject(this)
}
#Test
fun test_create() {
checkNotNull(datasource)
}
}
module build.gradle
apply plugin: 'java-library'
apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'
kapt {
generateStubs = true
}
dependencies {
rootProject.ext.apiGithubDependencies.each {
add(it.configuration, it.dependency)
}
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
My error
error:
[Dagger/MissingBinding] hr.thekarlo95.api.github.GithubDatasourceImpl cannot be provided without an #Inject constructor or an #Provides-annotated method.
public abstract void inject(#org.jetbrains.annotations.NotNull()
^
hr.thekarlo95.api.github.GithubDatasourceImpl is injected at
hr.thekarlo95.api.github.GithubDatasourceImplTest.setDatasource(p0)
hr.thekarlo95.api.github.GithubDatasourceImplTest is injected at
hr.thekarlo95.api.github.di.TestComponent.inject(hr.thekarlo95.api.github.GithubDatasourceImplTest)
I cannot get this to work and error message doesn't really help. If I remove fun inject(test: GithubDatasourceImplTest) from TestComponent everything compiles but then I cannot inject dependencies to my unit test.

Cannot resolve symbol generated AutoValue_Foo class inside pure java module in android project

I am trying to use AutoValue in a module that is written in pure Java that acts the domain module for my Android application. My application is composed of 3 layers, presentation, domain and data. Presentation and Data both have android dependencies, but domain does not.
Here is the AutoValue class data implementation :
import com.google.auto.value.AutoValue;
import org.jetbrains.annotations.Nullable;
import java.util.Date;
#AutoValue
public abstract class Trip {
public abstract int id();
public abstract String name();
public abstract int totalDistance();
#Nullable public abstract Date startDate();
#Nullable public abstract Date endDate();
public static Builder builder() {
return new AutoValue_Trip.Builder().totalDistance(0);
}
#AutoValue.Builder
public abstract static class Builder {
public abstract Builder id(int id);
public abstract Builder name(String name);
public abstract Builder totalDistance(int totalDistance);
public abstract Builder startDate(Date startDate);
public abstract Builder endDate(Date endDate);
public abstract Trip build();
}
}
Android Studio cannot find the generated AutoValue_Trip class, so it marks it as an error however, the project builds and runs just fine.
Here is my build.gradle of the domain module
buildscript {
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'me.tatarka:gradle-retrolambda:3.2.3'
classpath "net.ltgt.gradle:gradle-apt-plugin:0.12"
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: "net.ltgt.apt"
apply plugin: 'me.tatarka.retrolambda'
//noinspection GroovyUnusedAssignment
sourceCompatibility = 1.8
//noinspection GroovyUnusedAssignment
targetCompatibility = 1.8
configurations {
provided
}
sourceSets {
main {
compileClasspath += configurations.provided
}
}
dependencies {
def domainDependencies = rootProject.ext.domainDependencies
def domainTestDependencies = rootProject.ext.domainTestDependencies
apt domainDependencies.autoValue
compileOnly domainDependencies.autoValue
provided domainDependencies.javaxAnnotation
compile domainDependencies.javaxInject
compile domainDependencies.rxJava
compile domainDependencies.arrow
testCompile domainTestDependencies.junit
testCompile domainTestDependencies.mockito
testCompile domainTestDependencies.assertj
}
I have tried using a sourceSet as follows to add the generated build files and folders to the source path:
main {
java {
srcDirs += 'src/../build/generated'
}
}
But then I was getting compileJava errors.
Has anyone ran into this issue and how did you correct it? Additional info will be provided if necessary.

Dagger not generating Component classes

I have tried a great deal debugging this issue but unable to find the cause. Dagger simply doesn't create the DaggerComponent classes. I've checked SO for duplicates but none of the solutions provided worked.
project's build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'me.tatarka:gradle-retrolambda:3.2.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'me.tatarka:gradle-retrolambda:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
jcenter {
url "http://jcenter.bintray.com"
}
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
app's build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'me.tatarka.retrolambda'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
applicationId "com.hr.crux"
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
retrolambda {
jvmArgs '-noverify'
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
provided 'javax.annotation:jsr250-api:1.0'
apt 'com.google.dagger:dagger-compiler:2.2'
compile 'com.google.dagger:dagger:2.2'
provided 'javax.annotation:jsr250-api:1.0'
testCompile 'junit:junit:4.12'
}
HttpModule.java
#Module
public class HttpModule {
#Provides
#Singleton
Retrofit getRetrofit() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://maps.googleapis.com/maps/api/place/")
.build();
return retrofit;
}
}
HttpComponent.java
#Singleton
#Component(modules = {HttpModule.class})
public interface HttpComponent {
void inject(MainActivity activity);
}
Application.java
public class Application extends android.app.Application {
private static Application application;
private HttpComponent appComponent;
#Override
public void onCreate() {
super.onCreate();
application = this;
appComponent = //Cannot find DaggerHttpComponent
}
public static Application getInstance() {
return application;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Inject
Retrofit retrofit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Dagger is failing to generate the component class in my Application class. I've tried clean building, I've tried invalidating cache but nothing works.
You can start a fresh project by yourself instead of following the tutorial project. If you do so, here is the solution.
These two lines are responsible to generate the compile-time framework in Dagger 2.
compile 'com.google.dagger:dagger:2.14.1'//generates framework in compile time
annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1' //generates framework in compile time based on the annotations you provided.
Full setup Dagger
//dagger 2
compile 'com.google.dagger:dagger:2.14.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'
//to enable DaggerActivity, DaggerBroadcastReceiver, DaggerFragment etc classes
compile 'com.google.dagger:dagger-android:2.14.1'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.14.1'
//support libraries with dagger 2
compile 'com.google.dagger:dagger-android-support:2.14.1'
Note: You need to configure Annotation Process as provided in the screenshot below. You can do this File>Other Settings>Default Settings>search"Annotation processor"
As Other threads' Answers does NOT work for me:
I've answered here
Pref -> Editor -> File Types -> Ignore Files And Folders -> Remove "Dagger*.java;"

Dagger 2 "Dagger" prefix component not able to compile? auto generated class

Im trying to use Dagger 2 on android. I previously had it working and i had an appModule injecting dependencies into specific classes in the app. My Issue is that iam getting the error
Error:(14, 55) error: cannot find symbol class DaggerAppComponent
which attempting to import. this is an autogenerated class
below are my Dagger specific dependencies in my build.gradle file
compile 'com.google.dagger:dagger-compiler:2.0.2'
compile 'com.google.dagger:dagger:2.0.2'
provided 'javax.annotation:jsr250-api:1.0'
Ive tried cleaning and rebuilding the app numerous times but the class wont generate. Ive also tried using
compile 'org.glassfish:javax.annotation:10.0-b28'
for my annotations but Iam having no luck still? If anyone can help me out id appreciate. Its kind of difficult to see exactly what is going on for me at present? Thanks
EDIT: Component code
this was working before and i just added 1 extra class to inject into?
#Singleton
#Component(modules = AppModule.class)
public interface AppComponent {
void inject(RegHelper reghelper);
void inject(headerFooterRecViewAdapter headadapter);
void inject(SectionListExampleActivity seclistactivity);
}
This did the trick for me with the (current) latest dagger dependecies.
`dependencies{
...
compile 'com.google.dagger:dagger:2.11'
compile 'com.google.dagger:dagger-android-support:2.11'
annotationProcessor "com.google.dagger:dagger-compiler:2.11"
}`
Please add
apt 'com.google.dagger:dagger-compiler:2.x'
to your app build.gradle file
Setting up a stand-alone project in Android Studio 2.3, I updated the default gradle files as follows to get the generated Component file. Added lines have comment // dagger2 addition
PROJECT build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
// dagger2 addition
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
// dagger2 addition
mavenCentral()
maven{
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
APP MODULE build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt' // dagger2 addition
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.demo.dagger2demo"
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.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
// dagger2 addition
compile 'com.google.dagger:dagger:2.+'
apt "com.google.dagger:dagger-compiler:2.+"
}
You need to install this plugin https://bitbucket.org/hvisser/android-apt in order for Android Studio to see the Dagger Components.
I was having a similar issue with Dagger 2. I had an AppComponent and an ActivityComponent (being a subcomponent). And as soon as I would add a new inject() function in the AppComponent, I would get the above errors.
There was more errors besides the 'cannot find symbol' error but they were very vague and I couldn't debug my issues. After digging and researching stackoverflow and different tutorials, I realized I was using Dagger incorrectly. Specifically the way my AppComponent and ActivityComponent was setup.
I was under the assumption that I could inject my 'Activity' or 'Fragment' with both my AppComponent and ActivityComponent. This turned out to be wrong, at least I found out that it wasn't the right way of using Dagger.
My Solution:
AppComponent
#Singleton
#Component(modules = {AppModule.class})
public interface AppComponent {
void inject(MyApp application);
void inject(ContextHelper contextHelper);
// for exports
MyApp application();
PrefsHelper prefsHelper();
}
App Module
#Module
public class AppModule {
private final MyApp application;
public AppModule(MyApp application) {
this.application = application;
}
#Provides #Singleton
public MyApp application() {
return this.application;
}
#Provides #Singleton
public PrefsHelper providePrefsHelper() {
PrefsHelper prefsHelper = new PrefsHelper(application);
return prefsHelper;
}
}
ActivityComponent
#ActivityScope
#Component (dependencies = {AppComponent.class}, modules = {ActivityModule.class})
public interface ActivityComponent {
void inject(MainActivity activity);
void inject(OtherActivity activity);
void inject(SomeFragment fragment);
}
ActivityModule
#Module
public class ActivityModule {
private final MyActivity activity;
public ActivityModule(MyActivity activity) {
this.activity = activity;
}
#Provides #ActivityScope
public ContextHelper provideContextHelper(MyApp application) {
// My ContextHelper depends on certain things from AppModule
// So I call appComponent.inject(contextHelper)
AppComponent appComponent = application.getAppComponent();
ContextHelper contextHelper = new ContextHelper(activity);
appComponent.inject(contextHelper);
return contextHelper;
}
}
Application
public class MyApp extends Application {
private AppComponent appComponent;
#Override
public void onCreate() {
super.onCreate();
initializeDepInj();
}
private void initializeDepInj() {
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
appComponent.inject(this);
}
public LockAppComponent getAppComponent() {
return appComponent;
}
}
Activity
public class MainActivity extends AppCompatActivity {
// I get it from ActivityModule
#Inject
ContextHelper contextHelper;
// I get it from AppModule
#Inject
PrefsHelper prefsHelper;
ActivityComponent component;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupInjection();
}
protected void setupInjection() {
MyApp application = (MyApp) getApplication();
component = DaggerActivityComponent.builder()
.appComponent(application.getAppComponent())
.activityModule(new ActivityModule(this))
.build();
component.inject(this);
// I was also doing the following snippet
// but it's not the correct way since the ActivityComponent depends
// on AppComponent and therefore ActivityComponent is the only
// component that I should inject() with and I'll still get classes
// that are provided by the AppComponent/AppModule
// application.getAppComponent().inject(this); // this is unneccessary
}
public ContextHelper getContextHelper() {
return contextHelper;
}
}
I don't know if it directly resolves your issue but it should at least shed some light on how to use Dagger properly.
Hope it helps.
I had the same problem on my setup, Android Studio 2.2 within the following application class:
public class NetApp extends Application {
private NetComponent mNetComponent;
#Override
public void onCreate() {
super.onCreate();
// Dagger%COMPONENT_NAME%
mNetComponent = DaggerNetComponent.builder()
// list of modules that are part of this component need to be created here too
.appModule(new AppModule(this)) // This also corresponds to the name of your module: %component_name%Module
.netModule(new NetModule("https://api.github.com"))
.build();
// If a Dagger 2 component does not have any constructor arguments for any of its modules,
// then we can use .create() as a shortcut instead:
// mNetComponent = com.codepath.dagger.components.DaggerNetComponent.create();
}
public NetComponent getNetComponent() {
return mNetComponent;
}
}
I'm using the following gradle declaration for dagger 2:
//Dagger 2
// apt command comes from the android-apt plugin
apt 'com.google.dagger:dagger-compiler:2.7'
compile 'com.google.dagger:dagger:2.7'
provided 'javax.annotation:jsr250-api:1.0'
I could solve the problem by rebuilding the complete project (with errors) and then adding the import of the DaggerNetComponent that was missing before.
Just add #Module to Api class & rebuild the project.
Hope you all doing well.
I was facing the same problem and spent a lot of time over stack overflow. At last I go through this and able to find solution. Briefly, You have to make some changes in your module level Gradle file. Please remove
apply plugin: 'com.neenbedankt.android-apt'
at the top of the file. And replace
apt 'com.google.dagger:dagger-compiler:2.11'
with
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
After that rebuild your project and you will be able to import your Dagger prefix classes. Hopw it will help you out.

Categories