简体   繁体   English

Android aar 库不包含依赖项

[英]Android aar library doesn't contain dependencies

i saw similar questions, but not found accepted answers.我看到了类似的问题,但没有找到公认的答案。 Problem - i have my own android library with some tiny functions.问题 - 我有自己的带有一些小功能的 android 库。 My library uses others - fe Hawk (no sql database).我的图书馆使用其他人 - fe Hawk(没有 sql 数据库)。 My library gradle file:我的图书馆gradle文件:

apply plugin: 'com.android.library'
buildscript {
    repositories {
        maven { url "https://www.jitpack.io" }
    }
}
android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'library.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.0.2'
    compile 'com.github.orhanobut:hawk:1.23'
    testCompile 'junit:junit:4.12'
}

Library works fine.图书馆工作正常。 And if i use it as project inside another project - it work too.如果我将它用作另一个项目中的项目 - 它也可以工作。 But when i generate *.aar file (with gradle -> assembleRelease ) and include into separate project - it fails.但是当我生成*.aar文件(使用gradle -> assembleRelease )并包含到单独的项目中时 - 它失败了。 Project see ONLY MY library's class.项目只能看到我的图书馆的课程。 Hawk com.orhanobut.hawk package and ofc others (if i will use then) are not visible. Hawk com.orhanobut.hawk 包和其他一些(如果我会使用的话)是不可见的。 So ClassNotFoundException comes.于是ClassNotFoundException来了。 If i remove如果我删除

minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'library.pro'

the result doesnt change.结果不会改变。 I tried to add the following line into proguard file (library.pro)我试图将以下行添加到 proguard 文件 (library.pro)

-keep class com.orhanobut.hawk {public *;}
-keep class com.orhanobut.hawk.* {public *;}

Result is the same.结果是一样的。

So i have two question: 1 - what should i do to make my main project see my library's third party dependencies?所以我有两个问题:1 - 我应该怎么做才能让我的主项目看到我的库的第三方依赖项? 2 - is is possible to obfuscate my library's code (only mine, not dependencies)? 2 - 是否可以混淆我的库代码(只有我的,不是依赖项)?

what should i do to make my main project see my library's third party dependencies?我该怎么做才能让我的主项目看到我的库的第三方依赖项?

The aar file doesn't contain the transitive dependencies and doesn't have a pom file which describes the dependencies used by the module. aar文件不包含传递依赖项,也没有描述模块使用的依赖项的 pom 文件。

It means that, if you are importing a aar file using a flatDir repository you have to specify the dependencies also in your project .这意味着,如果您使用flatDir存储库导入 aar 文件,则还必须在项目中指定依赖项

You should use a maven repository , private or public, to avoid the issue.您应该使用Maven 存储库(私有或公共)来避免该问题。
In this case, gradle downloads the dependencies using the pom file which will contains the dependencies list.在这种情况下,gradle 使用包含依赖项列表的 pom 文件下载依赖项。

Another way to solve the problem of dependencies is to get the jar files of the dependencies you want to use and place them in the libs folder of your module.另一种解决依赖问题的方法是获取你要使用的依赖的jar文件,放到你的模块的libs文件夹中。 This will copy all the your dependency jars into your library's jar or aar.这会将您的所有依赖项jar复制到您的库的 jar 或 aar 中。

Note that I have emphasized jar because you cannot include aar file in libs folder, gradle still doesn't support aar file inside aar out of the box.请注意,我强调了 jar,因为您不能在 libs 文件夹中包含 aar 文件,gradle 仍然不支持开箱即用的 aar 内的 aar 文件。 There are a few gradle plugins like fataar which solve that problem.有一些像 fataar 这样的 gradle 插件可以解决这个问题。

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

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