Android: Can't import class from another module - android

I have an Android app module, an Android library module :foo, and a pure java module :bar. In :foo, I have a Foo class, and in :bar, I have a Bar class.
I would like to reference Foo from my Bar class. When I hover over the Foo variable, Android Studio gives me the following suggestion:
Add dependency on module 'MyApplication.foo.main'
When I do as Android Studio suggests, it adds a dependency in build.gradle for the :bar module. After adding the suggested code, this is what my build.gradle looks like:
build.gradle (:bar)
plugins {
id 'java-library'
}
java {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dependencies {
implementation project(path: ':foo')
}
Except there's one small problem; Foo still can't be resolved! Does anyone know how I can import it?
Here is what my project structure looks like:

So it turns out you cannot add an Android module as a dependency to a pure java module, which I guess makes sense.
Android Studio IDE was just suggesting something that cannot be done, which is misleading.

Related

is it possible to use kotlin on the JVM or Android without a dependency on kotlin-stdlib?

I have an android library that I distribute as a jar file; Currently it has zero dependencies (other than the things built into Java itself). I like the idea of porting it to kotlin, however I haven't been able to figure out how to do this without forcing a dependency on kotlin-stdlib (or kotlin-stdlib-jre7, etc)
I'm somewhat wary about doing this, because kotlin-stdlib gets updates very frequently (multiple times per month sometimes) and I don't want to cause a future compatibility problem.
Obviously if I use any of the kotlin specific functions or types (e.g. listOf, mapOf, etc) then of course I'd need the stdlib, but if I stick to pure java types and functions, then is this possible? And if so, how?
I am not sure this is what you are looking for but I was able to build and run run this simple program using IntelliJ Idea 2018.3
fun main() {
System.out.println("hello world")
}
and the following build.gradle file
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.11'
}
apply plugin: 'application'
mainClassName = "MainKt"
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
The only thing I saw in the jar file, other than a META-INF folder was my MainKt.class
I was able to run this on the jvm using
java -classpath simple.jar MainKt
I think if you don't include the standard library you cant even use Kotlin's println function, so you have to resort to java's System.out.println method

Gradle grouping dependencies

I was experimenting with my project's build.gradle. Currently my project consist of several modules, each of these module have common dependencies, such as android support or network library. I'm Experimenting with gradle dependencies.
I've declared a group of dependencies named lib_mandatory() in file lib-group.gradle, but when I tried to include it in my app's build.gradle the gradle sync failed.
Error:Could not find method lib_mandatory() for arguments [] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
How can I fix this? or any hints about what this error means?
Update:
Here's the lib-group.gradle
def dependencyGroup(Closure closure) {
closure.delegate = dependencies
return closure
}
def lib_mandatory = dependencyGroup{
implementation libraries.rxjava
implementation libraries.rxandroid
}
and here's the app's build.gradle
apply from: '../lib-group.gradle'
dependencies {
lib_mandatory()
}
The approach that is used in that article is a bit different, the article used the function inside the lib-group.gradle and only applied (apply from ...) the lib-group.gradle in the app's build.gradle.
In your approach, you're trying to use the function in the app's build.gradle.
If you want your functions to be accessed from other files, you should use ext instead of def. You might want to read this:
Gradle def vs ext

Android Studio Lambda expressions are not allowed at this language level

I am working on a chat application. I got a demo app from github, the chat app that is working. In some classes they used lambda expressions but it's working fine, but when I copy those code mine is giving this error " Lambda expressions are not allowed at this language level ". Some people said that android studio does not support lambda expressions but the demo app is working on my phone.
in build.gradle there should be
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
And you should probably use RetroLambda. Look at the demo project's build.gradle file
In their Android project they are using RetroLambda -
a plugin that allows lambdas in lower versions of Java,
which includes Android (see the plugin at the top --> https://github.com/NaikSoftware/StompProtocolAndroid/blob/master/example-client/build.gradle)
Follow the setup here: https://github.com/evant/gradle-retrolambda

Android switch to gradle doesn't compile JRE7 code features

I moved my android app over to Android Studio without switching to Gradle. Now I want to move to Gradle. The app compiles in Android Studio before switching to Gradle, but now that I have Gradle all set up, it won't compile the String Switch Statements or the diamond operators. The error I am getting is
Gradle: error: strings in switch are not supported in -source 1.6
(use -source 7 or higher to enable strings in switch)
I have made sure that I am running on JRE 7 by printing the
System.getProperty("java.version")
in a task. The output is
1.7.0_25
What confuses me most is the discrepancy between "-source 1.6" and "use -source 7". But I know that both of these are names for Java sdk's so maybe the titles are just being mixed up.
Is there a Gradle setting I need to set? or is this not possible in Gradle? If not it is confusing why it works without Gradle.
It should be noted that the without Gradle version of my project runs the default Android Studio build. I didn't write an ant script or maven script for building it. One of those may be the way it is being built, but I don't have any project specific files for them. Just the Android Studio .iml files.
UPDATE
I tried adding the following to the build.gradle android{} section
compileOptions {
sourceCompatibility = org.gradle.api.JavaVersion.VERSION_1_7
targetCompatibility = org.gradle.api.JavaVersion.VERSION_1_7
}
but the .class files failed to build and it weren't included in the apk. See the "Android Projects Need Libraries Compiled with Java 1.6" section on this post
You can upgrade an existing Android app/library module to Java 7 by adding the following in the android section of your build.gradle:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
For a Java (non-android) module, you should add the following to build.gradle just after apply plugin: 'java':
sourceCompatibility = 1.7
targetCompatibility = 1.7
For both types of modules, you will need to manually change the language level of your project in File -> Project Structure -> Project (or you can manually edit the config in .idea/misc.xml from JDK_1_6 to JDK_1_7).
You might also need to add the following to .idea/compiler.xml, in the <component name="CompilerConfiguration"> block (but see how you get on without it first):
<bytecodeTargetLevel target="1.7" />

"already added" exception with ormlite and gradle

I'm using android studio 0.2.3 with gradle 0.5 and added the ormlite dependency to the build.gradle file as follows:
compile 'com.j256.ormlite:ormlite-android:4.9'
Gradle downloaded two jar files: ormlite-android.jar and ormlite-core.jar. The problem is, that the jar files contain identically named classes. So I get the following well known exception:
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/j256/ormlite/dao/BaseDaoImpl$1;
Some other solution for the same problem with maven exists, suggesting to exclude the ormlite-core.jar. This should work if all classes from ormlite-core.jar are included in ormlite-android.jar - I didn't check this btw. In that case, I don't understand why the ormlite-core is in this ormlite android dependency package... I'm explicitly adding ormlite-android, as you can see in the snippet above.
But how to exclude the ormlite-core.jar in gradle. Everything I found was for gradle 1.6, but android studio uses gradle 0.5 - or is this just the version of the android gradle wrapper?
.:EDIT:.
To make the dependencies clearer, I add my build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile 'com.android.support:support-v4:18.0.+'
compile 'com.android.support:appcompat-v7:18.0.+'
compile 'com.google.android.gms:play-services:3.1.+'
compile 'com.j256.ormlite:ormlite-android:4.9'
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
}
Why I think there are same classes in the two ormlite libs? ==> See the screenshot.
Aren't the opened packages identical? Even the source is. The only distinct classes I found were SqliteAndroidDatabaseType and those in the com.j256.ormlite.android package.
The ormlite-android jar you're using is definitely wrong. My guess is that someone built it incorrectly with ormlite-core exported, which is why you're getting the merge conflicts. If you look at the source for ormlite-android, it isn't supposed to have most of those packages/classes included.
I'm not sure how the ormlite-android versioning works, but it looks like 4.46 is the actual latest version (updated 29-Jul-2013), not 4.9 (updated 26-Jan-2011). I'd recommend using 4.46 instead (that's what works for me) with:
'com.j256.ormlite:ormlite-android:4.46'
Thanks to #cproinger for the answer in another related question.
WOW, now it works! I misunderstood the versioning (my fault). But anyway, the newer version (4.46) contains only the android specific classes in the ormlite-android.jar and everything else in the ormlite-core.jar. Great, thanks #cproinger!

Categories

Resources