Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Android Library AAR - No Class Definition

I am using Android Studio. I am writing an Android Library called detector. It runs perfectly in the project from which it was created.

compile project(':detector')

I want to import the AAR file it generates into another project. I do so:

File > New > New Module > Import .JAR/.AAR Package

I then add the same line in the new projects app build.gradle:

compile project(':detector')

After cleaning and building, I run the app and get this error:

11-18 06:32:14.151 21283-21283/testing.com.test D/ResourcesManager: For user 0 new overlays fetched Null
11-18 06:32:14.161 21283-21283/testing.com.test W/System: ClassLoader referenced unknown path: /data/app/testing.com.test-1/lib/arm
11-18 06:32:15.131 21283-21283/testing.com.test W/System: ClassLoader referenced unknown path: /data/app/testing.com.test-1/lib/arm
11-18 06:32:15.131 21283-21283/testing.com.test D/ContextRelationManager: ContextRelationManager() : FEATURE_ENABLED=true
11-18 06:32:15.131 21283-21283/testing.com.test D/RelationGraph: garbageCollect()
11-18 06:32:15.301 21283-21283/testing.com.test D/RelationGraph: garbageCollect()
11-18 06:32:15.501 21283-21283/testing.com.test W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.621 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test I/art: Rejecting re-init on previously-failed class java.lang.Class<detector.Manager>
11-18 06:32:15.631 21283-21283/testing.com.test D/AndroidRuntime: Shutting down VM
11-18 06:32:15.631 21283-21283/testing.com.test E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: testing.com.test, PID: 21283
                                                                         java.lang.NoClassDefFoundError: detector.Manager
                                                                             at detector.Detector.<clinit>(Detector.java:39)
                                                                             at testing.com.test.MainActivity.onCreate(MainActivity.java:17)
                                                                             at android.app.Activity.performCreate(Activity.java:6904)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
                                                                             at android.app.ActivityThread.access$1100(ActivityThread.java:229)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:7325)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

I unzip the AAR and decompile the Classes.jar file to find the Manager file does exist:

Manager.class
Manager$3.class
Manager$2.class
Manager$1.class

Here is the libraries build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile ('org.altbeacon:android-beacon-library:2.8.1')
    compile ('com.google.android.gms:play-services-ads:9.6.1')
    compile 'com.android.support:appcompat-v7:23.4.0'
}

buildscript {
    repositories {
        jcenter()
        mavenCentral()
        flatDir {
            dirs 'libs'
        }
    }
}

Additionally, I am able to deploy the library to jCenter and am able to successfully run the library in other projects when linking to the jCenter installation. It is just the direct AAR import that is causing this issue.

Please advise on building an Android Library AAR and importing it into another Android Project.

======UPDATE======

I have found due to help from @bwt that the issue is solved if I include the libraries dependencies inside the apps build.gradle that is bootstrapping the AAR. How can I ensure that the dependencies ( which are google-play and altbeacon ) are included inside the AAR? Setting them as transitive dependencies before building the AAR has not helped.

like image 938
John Foley Avatar asked Nov 08 '16 17:11

John Foley


3 Answers

I am not sure the problem is that the class are not imported (otherwise you could not compile) but just in case what happen if you import the local aar without an additional module ?

  1. Put the aar in a directory inside the module where you want to use it (e.g. aarlibs in the app module)

  2. Declare this directory as a repository (in the app's build.gradle) :

    repositories{
        flatDir{
            dirs 'aarlibs'
        }
    }
    
    android {
    ...
    
  3. Reference it as a dependency (also in the app's build.gradle) :

    dependencies {
        compile(name:'aar_file_name', ext:'aar')
    ...
    
like image 158
bwt Avatar answered Oct 15 '22 02:10

bwt


The aar file does not have the transitive dependencies and doesn't have a pom file that describes the dependencies used by the library.

means, if you are importing a aar file then you have to specify the dependencies also in your project.

You should use a maven repository (you have to publish the library in a private or public maven repo).

like image 2
Sanat Chandravanshi Avatar answered Oct 15 '22 00:10

Sanat Chandravanshi


The way of importing .AAR/JAR in Android Studio v-4.2 above has changed, so use this https://developer.android.com/studio/projects/android-library#psd-add-aar-jar-dependency

like image 2
Denny Avatar answered Oct 15 '22 02:10

Denny