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

How to use Android Annotations in a library (Android Studio)?

我在 android 工作室中有一个 android 项目。其中一个模块是使用 android 注释的库。另一个是使用库的应用程序。通过大量调整和谷歌搜索,我设法让库项目生成“”文件。但是,当我 运行 应用程序时,它不包括文件,我可以看到,因为当找不到应用程序“” class.[=17= 时它会立即崩溃]

图书馆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.access00(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.access00(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.access00(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.access00(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]'

查看您的图书馆后 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 提交中找到更改的填充集。