ErikBrandsma
ErikBrandsma

Reputation: 1719

Self created library with dependency to another self created library

I am trying to build an app which is composed out of separate library projects.

To do this, I'm trying to make a proof of concept which is supposed to be as following:
enter image description here

I tried to keep the project as simple as possible. The projects contents do not matter!
All that matters is the dependencies between the projects!

The result should be that MainProject will print out Something Another String!

I have tried all from .JAR files to .AAR files, but the best I got was
with the dependency in red. I added the StringExtender.aar file to StringReturner, and then the StringReturner.aar file to the MainProject.

When I do this I get the following Exception:

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/example/erik/stringextender/StringExtender;

What is the right way to setup a simple proof of concept like this? I can't seem to find anything related to a library project having a dependency. It's all 1 level deep!

Any help is welcome!

EDIT SHOWING GRADLE BUILD FILES
StringReturner:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile project(':StringExtender-lib-debug')
}

MainProject:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.erik.erikpoc10"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile project(':StringReturnerLib-debug')
}

Upvotes: 3

Views: 155

Answers (1)

Zarathes
Zarathes

Reputation: 126

Use the following tutorial - Creating libraries for Android applications

See a working example project on my Github

The most important steps taken were:

  • 8.3. Create library module.
  • 8.6. Define dependency to the library project.

Do not forget to import the library class you want to use, for example:
In MainProject: import com.example.stringreturner.StringReturner. In StringReturner: import com.example.stringreturner.StringExtender.

The info below is based on the image you provided:

The library methods are not static so don't forget to make an actual object. So StringReturner should make a StringExtender object, And MainProject should make a StringReturner object first!

And finally, I think it's a typo but both libraries have the class StringReturner. This will not work in the StringReturner library for obvious reasons.

I should also note that I used Android Studio 2.0 and I did not touch .JAR files nor .AAR files. I merely created two library modules and one app. Then configured the Project Structure -> Dependencies by adding Module Dependencies.

Upvotes: 2

Related Questions