Using Android Studio I have an Android Module, and a Java module.
In the Java module, I want to use AutoValue to generate immutable classes.
All seems to work fine (files are generated in the Java module) but I'm unable to access those files in the Android project. Any suggestions?
app\build.gradle
apply plugin: 'com.android.application'
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':domain')
}
domain\build.gradle
plugins {
id 'net.ltgt.apt' version '0.6'
}
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.auto.value:auto-value:1.2'
apt 'com.google.auto.value:auto-value:1.2'
}
domain/MyLocation.java
import com.google.auto.value.AutoValue;
#AutoValue
abstract class MyLocation {
abstract String name();
#AutoValue.Builder
abstract static class Builder {
abstract Builder name(String _name);
abstract MyLocation build();
}
}
When I build the domain module (the java module), I see that the 'AutoValue_MyLocation' file is generated here:
\domain\build\generated\source\apt\main\my\package\domain\AutoValue_MyLocation.java
However, I am not able to use the generated class anywhere in my Android module.
Nevermind, by looking at the generated class I saw that it was not public, hence I could not access it in my Android Module.
To fix it, simple make the #AutoValue class public, like so
#AutoValue
public abstract class MyLocation {
//
}
Related
I have a core branch where following dependencies is decalared
implementation "org.parceler:parceler-api:$rootProject.ext.parcelVersion"
annotationProcessor "org.parceler:parceler:$rootProject.ext.parcelVersion"
I am declaring a dependency in one of my library module as below
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':core')
}
Here is the class that I will be parceling in my activity
#Parcel(parcelsIndex = false)
public class MyClass {
#SerializedName("validation")
public String myField;
}
Exception is thrown at the third line.
MyClass myClass = new MyClass();
myClass.myField = "bjbskas";
Parcelable parcelable =Parcels.wrap(myClass);
Exception reads as shown below
Unable to find generated Parcelable class for com.example.mylibrary.MyClass,
verify that your class is configured properly and that the Parcelable class
com.example.mylibrary.MyClass$$Parcelable is generated by Parceler.
If I try putting the parceler library in my library module directly, it gives me another error called
Program type already present: org.parceler.Parceler$$Parcels$1
Changing the version of Parceler library from 1.0.4 to 1.1.10 solved the problem. I don't know what is the reason behind this but I guess it may be because of some transitive dependencies using version 1.1.10
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.
I created an Android library that uses JavaPoet to generate classes. It works well on my local workspace ; even if I include the library module into another project.
Now I'm trying to put my project online through bintray. The project is uploaded correctly, but then when I include it in a new project and build the projet I get this message :
Error:Bad service configuration file, or exception thrown while
constructing Processor object: javax.annotation.processing.Processor:
Provider me.aflak.filter_processor.FilterProcessor could not be
instantiated: java.lang.NoClassDefFoundError:
com/squareup/javapoet/TypeName
I guess it comes from the way I manage the dependencies... Compiletime, Runtime stuff...
This is processor build.gradle :
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':filter-annotation')
api 'com.squareup:javapoet:1.9.0'
compile 'com.google.code.gson:gson:2.8.1'
compile 'com.google.auto.service:auto-service:1.0-rc3'
}
This is annotation buid.gradle :
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:2.8.1'
}
This is how I include the library in an empty project :
// build.gradle project
repositories {
maven{
url 'https://dl.bintray.com/omaflak/maven'
}
}
// build.gradle module
dependencies {
compile 'me.aflak.libraries:filter-annotation:1.0'
annotationProcessor 'me.aflak.libraries:filter-processor:1.0'
}
Could someone point me out to the right direction ? Thanks !
I finally got the solution (a bit randomly though :p).
I had to add mavenLocal() in the library module.
Trying to run instrumentation test on AS.
stuck with this Error:
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker
at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:66)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy4.isTypeMockable(Unknown Source)
ExampleInstrumentedTest.java
#RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
#Mock
Context context;
#Before
public void init(){
MockitoAnnotations.initMocks(this);
}
#Test
public void testDisabledFlag() {
ChanceValidator chanceValidator = new ChanceValidator(context);
Validator.ValidationResult result = chanceValidator.validate(2);
assertEquals(result, Validator.ValidationResult.NO_ERROR);
}
}
build.gradle
apply plugin: 'com.android.application'
android{
..
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
testOptions {
unitTests.returnDefaultValues = true
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
// Unit testing dependencies
testCompile 'junit:junit:4.12'
// Set this dependency if you want to use the Hamcrest matcher library
testCompile 'org.hamcrest:hamcrest-library:1.3'
// more stuff, e.g., Mockito
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.1.0'
compile project(':mortar')
compile project(':mockito-core-2.6.6')
}
Update:
After commenting line-
MockitoAnnotations.initMocks(this);
It is building fine(No Exception) but context mocked is now null.
This Worked in my case:
dependencies {
def mockito_version = '2.7.1' // For local unit tests on your development machine
testCompile "org.mockito:mockito-core:$mockito_version" // For instrumentation tests on Android devices and emulators
androidTestCompile "org.mockito:mockito-android:$mockito_version"
}
I didn’t comment initMocks
In my case, I was working on a project that does not use the maven build system. So this is what worked for me.
Navigated to the maven repo for mockito (used v2.26): https://mvnrepository.com/artifact/org.mockito/mockito-core/2.26.0. I downloaded the jar.
On the same page at the bottom, I looked up the dependencies. For mockito 2.26.0, these dependencies are:
Byte Buddy v.1.9.10
(https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy/1.9.10)
Byte Buddy Java Agent v1.9.10
(https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy-agent/1.9.10)
Objenesis v2.6
(https://mvnrepository.com/artifact/org.objenesis/objenesis/2.6) I
downloaded the jar files for the above mockito dependencies.
In Eclipse I created a user library containing the four jar file and added it to my project.
NB: (creating the library is optional, you can add the jars directly to your project build path)
Hope this helps someone.
Do not explicitly include mockito, let powermock pull in what it needs.
I got this problem resolved after adding transitive dependencies for 'mockito-core'.
I was facing this problem in eclipse. I was using 'mockito-core 3.8.0' along with 'mockito-junit-jupiter 3.8.0'.
At first I tried to resolve this by changing JRE to JDK in Project/ Java Build Path ((as many have posted this as resolution), but that did not solve the problem.
Then I added below 3 transitive dependencies for 'mockito-core 3.8.0' explicitly, and it worked!
1. net.bytebuddy » byte-buddy v1.10.20
2. net.bytebuddy » byte-buddy-agent v1.10.20
3. org.objenesis » objenesis v3.1
(https://mvnrepository.com/artifact/org.mockito/mockito-core/3.8.0 - see compiled dependencies)
I am using Quarkus on a big project with many people.
Most of our microservices used this dependency version
<net.bytebuddy.version>1.12.9</net.bytebuddy.version>
One microservice used:
<net.bytebuddy.version>1.11.0</net.bytebuddy.version>
Which was not compatible with our
<artifactId>quarkus-junit5-mockito</artifactId>
When I added more tests on a resource, I got the error of this question.
I changed the bytebuddy to 1.12.9 and mockito worked.
Make sure your bytebyddy's version is compatible with you mockito version.
Updated either one of them to be compatible with each other.
I try to make Roboblender work with Roboguice but the compile time processing doesn't seem to do anything, the AnnotationDatabaseImpl class is not generated. (Project builds without error.)
I even created a sample project, please see below. What do I miss?
(I know the gradle task and the second metadata would only be needed for multi module project, but it didn't work without them either).
build.gradle:
project.tasks.withType(JavaCompile) { task ->
options.compilerArgs << "-AguiceAnnotationDatabasePackageName=gk.com.roboguice_compile"
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'org.roboguice:roboguice:3.+'
provided 'org.roboguice:roboblender:3.+'
}
manifest:
<meta-data
android:name="roboguice.modules"
android:value="gk.com.roboguice_compile.RoboguiceBindings" />
<meta-data
android:name="roboguice.annotations.packages"
android:value="gk.com.roboguice_compile" />
activity:
#ContentView(R.layout.activity_main)
public class MainActivity extends RoboActivity {
#Inject
private PresentMaker presentMaker;
bindings:
public class RoboguiceBindings extends AbstractModule {
#Override
protected void configure() {
bind(PresentMaker.class).to(BirthdayPresentMaker.class);
}
}
The AnnotationDatabaseImpl was there but only under the build directory not among the source files.
My bad, probably every annotation processor work like this.
(Although strangely this project has the AnnotationDatabaseImpl generated in the project root..).