如何在库中使用Android Annotations(Android Studio)?

时间:2016-10-20 18:28:35

标签: android android-studio android-annotations

我在android studio中有一个android项目。其中一个模块是使用android注释的库。另一个是使用该库的应用程序。通过大量的调整和谷歌搜索,我设法让图书馆项目生成" "文件。但是,当我运行该应用程序时,它不包括我可以看到的文件,因为当它无法找到应用程序" "时它会立即崩溃。类。

图书馆build.gradle

apply plugin: 'com.android.library'
apply plugin: 'android-apt'
def AAVersion = '3.0.1'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}

configurations {
    apt
}

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName 'com.hps.mobuyle.core'
        library "true"
    }
}


android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 23

        // Enabling multidex support.
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    packagingOptions {
        exclude 'META-INF/license.txt'
        exclude 'META-INF/notice.txt'
    }

    sourceSets {
        main {
            jniLibs.srcDir 'libs'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:23.1.0'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.android.support:appcompat-v7:23.1.0'

    // android annotations
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}

应用程序build.gradle

apply plugin: 'com.android.application'

apply plugin: 'android-apt'
def AAVersion = '3.0.1'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}

configurations {
    apt
}

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName 'com.hps.mobuyle.core'
    }
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.hps.mobuyle.restaurant"
        minSdkVersion 16
        targetSdkVersion 23

        // Enabling multidex support.
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

}


dependencies {
    compile 'com.android.support:appcompat-v7:23.1.0'
    // android annotations
    apt "org.androidannotations:androidannotations" + ":$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
    compile project(':libraryProject')
}

项目settings.gradle

include ':libraryProject'
include ':applicationProject'

完整错误输出

10-24 13:54:41.890 17670-17670/? E/Zygote: v2
10-24 13:54:41.890 17670-17670/? E/Zygote: accessInfo : 0
10-24 13:54:41.980 17670-17670/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.hps.mobuyle.restaurant, PID: 17670
                                                   java.lang.RuntimeException: Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication: java.lang.IllegalStateException: java.lang.ClassNotFoundException: com.hps.mobuyle.core.MPApplication_
                                                       at android.app.LoadedApk.makeApplication(LoadedApk.java:676)
                                                       at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6289)
                                                       at android.app.ActivityThread.access$1800(ActivityThread.java:221)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1860)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:158)
                                                       at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                       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)
                                                    Caused by: java.lang.IllegalStateException: java.lang.ClassNotFoundException: com.hps.mobuyle.core.MPApplication_
                                                       at com.android.tools.fd.runtime.BootstrapApplication.createRealApplication(BootstrapApplication.java:220)
                                                       at com.android.tools.fd.runtime.BootstrapApplication.attachBaseContext(BootstrapApplication.java:239)
                                                       at android.app.Application.attach(Application.java:211)
                                                       at android.app.Instrumentation.newApplication(Instrumentation.java:1020)
                                                       at android.app.Instrumentation.newApplication(Instrumentation.java:1004)
                                                       at android.app.LoadedApk.makeApplication(LoadedApk.java:666)
                                                       at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6289) 
                                                       at android.app.ActivityThread.access$1800(ActivityThread.java:221) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1860) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:158) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:7224) 
                                                       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) 
                                                    Caused by: java.lang.ClassNotFoundException: com.hps.mobuyle.core.MPApplication_
                                                       at java.lang.Class.classForName(Native Method)
                                                       at java.lang.Class.forName(Class.java:324)
                                                       at java.lang.Class.forName(Class.java:285)
                                                       at com.android.tools.fd.runtime.BootstrapApplication.createRealApplication(BootstrapApplication.java:209)
                                                       at com.android.tools.fd.runtime.BootstrapApplication.attachBaseContext(BootstrapApplication.java:239) 
                                                       at android.app.Application.attach(Application.java:211) 
                                                       at android.app.Instrumentation.newApplication(Instrumentation.java:1020) 
                                                       at android.app.Instrumentation.newApplication(Instrumentation.java:1004) 
                                                       at android.app.LoadedApk.makeApplication(LoadedApk.java:666) 
                                                       at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6289) 
                                                       at android.app.ActivityThread.access$1800(ActivityThread.java:221) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1860) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:158) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:7224) 
                                                       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) 
                                                    Caused by: java.lang.ClassNotFoundException: Didn't find class "com.hps.mobuyle.core.MPApplication_" on path: DexPathList[[zip file "/data/app/com.hps.mobuyle.restaurant-2/base.apk"],nativeLibraryDirectories=[/data/app/com.hps.mobuyle.restaurant-2/lib/arm64, /vendor/lib64, /system/lib64]]
                                                       at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                       at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                       at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                       at java.lang.Class.classForName(Native Method) 
                                                       at java.lang.Class.forName(Class.java:324) 
                                                       at java.lang.Class.forName(Class.java:285) 
                                                       at com.android.tools.fd.runtime.BootstrapApplication.createRealApplication(BootstrapApplication.java:209) 
                                                       at com.android.tools.fd.runtime.BootstrapApplication.attachBaseContext(BootstrapApplication.java:239) 
                                                       at android.app.Application.attach(Application.java:211) 
                                                       at android.app.Instrumentation.newApplication(Instrumentation.java:1020) 
                                                       at android.app.Instrumentation.newApplication(Instrumentation.java:1004) 
                                                       at android.app.LoadedApk.makeApplication(LoadedApk.java:666) 
                                                       at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6289) 
                                                       at android.app.ActivityThread.access$1800(ActivityThread.java:221) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1860) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:158) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:7224) 
                                                       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) 
                                                    Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.hps.mobuyle.core.MPApplication_" on path: DexPathList[[dex file "/data/data/com.hps.mobuyle.restaurant/files/instant-run/dex/slice-support-annotations-23.1.0_510f17cdc40968bee04a55acc3a74b072e08e08e-classes.dex", dex file "/data/data/com.hps.mobuyle.restaurant/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.hps.mobuyle.restaurant/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.hps.mobuyle.restaurant/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.hps.mobuyle.restaurant/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.hps.mobuyle.restaurant/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.hps.mobuyle.restaurant/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.hps.mobuyle.restaurant/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.hps.mobuyle.restaurant/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.hps.mobuyle.restaurant/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.hps.mobuyle.restaurant/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.hps.mobuyle.restaurant/files/instant-run/dex/slice-slf4j-api-1.7.10_57fc178fc2dace08d430739b21b0d50456c561f2-classes.dex", dex file "/data/data/com.hps.mobuyle.restaurant/files/instant-run/dex/slice-logback-android-core-1.1.1-3_79a748caea787f82e8fa3c69f01e9a3885b7ec32-classes.dex", dex file "/data/data/com.hps.mobuyle.restaurant/files/instant-run/dex/slice-logback-android-classic-1.1.1-3_c1b46ea01a968
10-24 13:54:42.010 3432-17684/? E/android.os.Debug: ro.product_ship = true
10-24 13:54:42.010 3432-17684/? E/android.os.Debug: ro.debug_level = 0x4f4c
10-24 13:54:42.010 3432-17684/? E/android.os.Debug: sys.mobilecare.preload = false

制作警告

Warning:The following options were not recognized by any processor: '[androidManifestFile, resourcePackageName]'

1 个答案:

答案 0 :(得分:2)

看了你的库build.gradle后,我发现了一个可能的问题。您应始终将字符串传递给注释处理选项,true可能无效,"true"应该有效。

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName 'com.hps.mobuyle.core'
        library "true"
    }
}

我在GitHub上检查了被剥离的project

主要问题是项目具有非常规布局,但是不正确地配置了不同的项目位置。这就是为什么Gradle根本不知道源代码的原因,并且不包括APK中的编译类。

这是配置此项目布局所需的:

设置清单文件位置: manifest.srcFile 'app/src/main/AndroidManifest.xml'

添加Java源文件夹: java.srcDirs += 'app/src/main/java'

配置JNI文件夹:jniLibs.srcDirs += 'app/libs'

添加本地依赖关系jar:compile fileTree(dir: 'app/libs', include: ['*.jar'])(请注意这些是 app 文件夹

我还更新了android-apt插件的版本,我还删除了不需要的android.applicationVariants.each块,这是插件之前配置注释处理的方式。现在它由插件本身完成。

我还修复了app项目中的AA注释处理参数,因为它错误地指向了lib项目包名: resourcePackageName 'com.hps.mobuyle.restaurant'

可以在this commit。

中找到更改的填充集