0

Intro

I have an app and I currently started working om some backend code. To prevent code duplication, I want to move some of my code into a library.

This is what I did:

  • For my library I created a jar file, using IntelliJ IDEA (Project Structure - Artifacts - etc.)
  • I copied the jar file into the lib folder of my Android App
  • I included the dependency (in the build.gradle I put implementation files('libs/numbers-lib.jar'))

Problem

Now when I try to use one of the classes I get Cannot resolve symbol 'Level' (see also screenshot). What am I missing?

 

EDIT: I already cleaned/rebuilt my project and invalidated caches.

EDIT2: Here is my gradle (upon request)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.my.example"
        minSdkVersion 21
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        targetSdkVersion 27
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'junit:junit:4.12'
    implementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    implementation 'postgresql:postgresql:9.0-801.jdbc4'
    implementation files('libs/numbers-lib.jar')
}

EDIT3: Here is a screenshot. You can see that the library has the correct Class... enter image description here

MWB
  • 1,830
  • 1
  • 17
  • 38

3 Answers3

1

Put your lib jar file in app/libs, in your project gradle file append this line to dependencies :

compile fileTree(dir: 'libs', include: ['*.jar'])
Koorosh Ghorbani
  • 507
  • 4
  • 14
  • This line is already in my gradle file (I updated my post to add my gradle file). – MWB Dec 16 '18 at 11:26
0

change

implementation fileTree(include: ['*.jar'], dir: 'libs')

to

implementation fileTree(dir: 'libs', include: ['*.jar'])

and don't forgot to place your jar in libs folder.

Zaid Mirza
  • 3,540
  • 2
  • 24
  • 40
  • @ZaidMriza: The order of the two does not matter. I tried your version as well, double checked that the `jar` was in the `libs` folders, but still... same error. – MWB Dec 16 '18 at 11:46
  • try to remove this line implementation files('libs/numbers-lib.jar') and then try – Zaid Mirza Dec 16 '18 at 11:49
  • Tried it, but no luck... (I also cleaned/rebuilt again, just to be sure) – MWB Dec 16 '18 at 11:53
0

It turned out I had to put my library classes into a package before creating the library.

MWB
  • 1,830
  • 1
  • 17
  • 38
  • 1
    Not much to explain I guess. I made a package in my project, moved the class files into this package and created the library – MWB Jun 22 '20 at 08:18