使用另一个AAR文件和JAR文件创建Android Studio AAR

11

我正在使用另一个aar和jar文件依赖项创建AAR文件。我已经成功创建了.aar发布文件。

接下来,我将我的新AAR文件导入示例项目中。该项目运行良好。当尝试访问aar和jar类时,显示“NoClassDefFound”错误。

注意:首先我想知道现在是否可以在另一个AAR文件中嵌套AAR文件。有人能帮我解决这个新问题吗?

我还参考了此链接。它说transitive = true。但是不明白在第三方应用程序中使用还是在创建AAR内部的AAR项目依赖关系中使用该标志。

提前感谢。

我的小型项目Gradle文件:

Mini Project ,仅转换为.AAR文件。然后,只需将此文件用于另一个NewProject

Mini Project Build.gradle 文件

/*
This would be enabled for development purpose and to debug and check the same
*/

apply plugin: 'com.android.application'

/*
This would be enabled for exporting as aar file
apply plugin: 'com.android.library'
*/

//apply plugin: 'com.android.library'

android {

    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    dexOptions {
        javaMaxHeapSize "4g"
        preDexLibraries = false
    }
    defaultConfig {

        /*
        This application id would be used for development purpose and to debug and check the same.
        This would be commented when exporting as library
        */
        applicationId 'com.pss.pssplayer'
        minSdkVersion 18
        targetSdkVersion 23
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
    }

    aaptOptions {
        cruncherEnabled = false
    }

    buildTypes {

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

        }
    }

    productFlavors {
    }
}

dependencies {

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

    testCompile 'junit:junit:4.12'

    compile 'com.android.support:appcompat-v7:23.1.1'

    compile 'com.android.support:support-v4:23.1.1'

    compile files('libs/ksoap2-android-assembly-3.2.0-jar-with-dependencies.jar')

    compile files('libs/secure.jar')

    compile(project(':getSubProject')) {

        transitive = true

    }// this getSubProject also contains one classes jar
}

在你的主项目中添加 transitive=true 到你所包含的依赖项中。 - Vipul Asri
我在MainProject中添加了transitive=true。但是,我仍然遇到相同的错误。一个特定的类从导入的aar中显示noclassdeffound错误。 - harikrishnan
请发布 build.gradle 的代码。 - Vipul Asri
我的Gradle已经粘贴了Vipul Asri。你能帮我检查一下吗? - harikrishnan
2个回答

8
当您在依赖中引用aar和jar时,您可以使用对第一个的引用来编译新的aar。但是它们并没有嵌入在您的aar中。相反,使用“big aar”的应用程序还需要依赖于前两个。为此,您可以将所有aar部署在类似maven这样的存储库中,并告诉maven“big aar”依赖于前两个(->pom.xml)。在您的应用程序build.gradle文件中,您可以只添加对“big aar”的依赖,并使用transitive=true告诉系统查看依赖项的依赖关系。 编辑: 您至少可以合并jars。请参阅以下答案:gradle - how do I build a jar with a lib dir with other jars in it?Can Gradle jar multiple projects into one jar? 合并aar更加困难,因为您必须合并所有资源、清单等...

让我试试这个选项吧。我将把我的基本AAR与JAR一起发布到Maven中央仓库,并引用MiniProject的依赖项。然后,我将转换为.AAR并在新项目中使用和检查。 - harikrishnan
嗨Bipi,我有供应商的AAR和Jar文件。无论如何,他们不允许发布到jcenter。根据规定,他们只会提供.aar和.jar文件。 - harikrishnan
合并aar和jar不可能,除非使用像android-fat-aar这样的工具,但有一些限制。您可以考虑设置本地的maven仓库来处理依赖问题吗?我们公司正在使用Artifactory。 - Bipi
2
能否在没有Maven的情况下将一个aar包含到另一个aar中? - Pablo Cegarra
@PabloCegarra 我不这么认为。你必须使用像Maven这样的仓库,或者将aar文件合并成一个文件。 - Bipi
显示剩余2条评论

0

上述提到的fat-aar方法只能在使用Android Studio创建AAR库时成功地在Android Native中工作。 - harikrishnan
嗨,Nicole,Android aar构建在2.2.2版本的studio中运行良好。如果我们升级到android studio 3.0.1,AAR无法正常构建。有关此问题的任何更新吗?当将此AAR集成到其他项目中时,它无法正常工作。它与AppCombat支持7相关。目标、编译、构建工具是26及以上版本。 - harikrishnan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接