简体   繁体   English

从开源创建 Android AAR 文件并将其添加到我自己的项目中

[英]Creating Android AAR file from open source and add it to my own project

Hey all I am having a difficult time figuring out why the export of an.aar file does not seem to be working when I add it to my own project.嘿,我很难弄清楚为什么当我将 an.aar 文件的导出添加到我自己的项目中时它似乎不起作用。

I am using the Expanable-tab project and it compiles the library it has to an.aap file via the build.gradle:我正在使用Expanable-tab项目,它通过 build.gradle 将它拥有的库编译为 .aap 文件:

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

ext.versionMajor = 1
ext.versionMinor = 2
ext.versionPatch = 1

android {
    compileSdkVersion 30

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 30
        versionCode versionMajor * 100000 + versionMinor * 100 + versionPatch * 1
        versionName "$versionMajor.$versionMinor.$versionPatch"
        setProperty("archivesBaseName", "expandable-fab")

        // Changes the name of generated AAR artifacts to be more descriptive
        libraryVariants.all { variant ->
            variant.outputs.all { output ->
                if (outputFileName.endsWith('.aar')) {
                    outputFileName = "${archivesBaseName}-${versionName}.aar"
                }
            }
        }
    }

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

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
}

apply from: 'generate-docs-and-update-website.gradle'
apply from: 'publish-artifacts-to-maven-repo.gradle'

When I rebuild the project I navigate to the expandable-fab-master\library\build\outputs\aar directory where expandable-fab-1.2.1.aar file has been produced.当我重建项目时,我导航到expandable-fab-master\library\build\outputs\aar目录,其中生成了expandable-fab-1.2.1.aar文件。

在此处输入图像描述

I then copy the.aap file to my libs folder in my own project:然后我将 .aap 文件复制到我自己项目中的 libs 文件夹中:

在此处输入图像描述

Then I proceed to add this to the build.gradle:然后我继续将其添加到 build.gradle:

在此处输入图像描述

All of this compile when selecting the rebuild option - no errors.选择重建选项时所有这些编译 - 没有错误。

However, when it gets to the point in my app that uses the expandable-fab code it crashes with this as the error:但是,当它到达我的应用程序中使用可扩展工厂代码的地步时,它会崩溃并出现以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.myapp, PID: 19960
    android.view.InflateException: Binary XML file line #36 in com.myapp:layout/_fragvideoplay1: Binary XML file line #36 in com.myapp:layout/_fragvideoplay1: Error inflating class com.nambimobile.widgets.efab.ExpandableFabLayout
    Caused by: android.view.InflateException: Binary XML file line #36 in com.myapp:layout/_fragvideoplay1: Error inflating class com.nambimobile.widgets.efab.ExpandableFabLayout
    Caused by: java.lang.reflect.InvocationTargetException

And the code its referencing (videoPlay.java:99):及其引用的代码(videoPlay.java:99):

在此处输入图像描述

If I comment out the implementation files('libs/expandable-fab-1.2.1.aar') and uncomment the original implementation 'com.nambimobile.widgets:expandable-fab:1.2.1' and then run the app it works as it should...如果我注释掉implementation files('libs/expandable-fab-1.2.1.aar')并取消注释原始implementation 'com.nambimobile.widgets:expandable-fab:1.2.1' ,然后运行它的应用程序它应该...

What am I missing here?我在这里想念什么?

This is because of how you're using the aar file as a gradle dependency.这是因为您如何将 aar 文件用作 gradle 依赖项。 Since you already have既然你已经有了

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

You don't have to do你不必做

implementation("libs/library_name")

instead do this:而是这样做:

implementation(name: "library_name", ext: "aar")

I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM