简体   繁体   English

如何从具有gradle依赖性的代码创建Android库?

[英]How to create an Android library from the code that have gradle dependency?

I am creating an Android library having dependencies based on the following components. 我正在创建一个基于以下组件的依赖项的Android库。

  1. My library code 我的图书馆代码
  2. layout and resources file 布局和资源文件
  3. Gradle dependencies to third parties. Gradle对第三方的依赖。

First I think of creating .jar file for my library but then I figured out that laulyoit and resources files are not compiled with the jar. 首先,我想到为我的库创建.jar文件,但后来我发现laulyoit和资源文件不是用jar编译的。 Then i decided to create AAR file. 然后我决定创建AAR文件。 Please suggest if this one is right move or is there any other alternative? 请建议这个是正确的移动还是有其他选择?

Now the main problem I am stuck with gradle dependencies. 现在我遇到了gradle依赖的主要问题。 We are creating a paid library that uses some third parties like google only available through Gradle. 我们正在创建一个付费图书馆,使用Google等仅通过Gradle提供的第三方。 Now since I am creating an AAR file, there would be no Manifest.xml and Gradle file, how can I integrate the third parties with my own code. 现在,因为我正在创建一个AAR文件,所以没有Manifest.xml和Gradle文件,如何将第三方与我自己的代码集成。 And We have the strict instructions that we must not tell the users of library to include that third parties to include. 我们有严格的指示,我们不能告诉图书馆的用户包括要包括的第三方。 In simple words, we cannot let the users know what kind of third parties our library is using. 简单来说,我们不能让用户知道我们的库正在使用哪种第三方。 So is there any way I can create an AAR file that also have the gradle dependencies pre-compiled with this without revealing the user what services we are using? 那么有什么方法可以创建一个AAR文件,该文件也具有预先编译的gradle依赖项,而不会向用户显示我们正在使用的服务?

Update 1 更新1

Here is my build.gradle for Library 这是我的库的build.gradle

apply plugin: 'com.android.library'

android {
   compileSdkVersion 25
   buildToolsVersion "25.0.2"
   defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner         "android.support.test.runner.AndroidJUnitRunner"
}
   dexOptions {
        preDexLibraries = false
        javaMaxHeapSize "4g" // 2g should be also OK
   }
   buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.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:+'

   compile 'com.google.android.gms:play-services-vision:10.2.1'

   testCompile 'junit:junit:4.12'
}

After the I am moving my .aar file to local maven and creating then using the output file in my application Project. 在我将.aar文件移动到本地maven之后,然后使用我的应用程序Project中的输出文件创建。 Here is the gradle for same. 这是同样的gradle。

apply plugin: 'com.android.application'
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
    applicationId "com.jarvis.myapplication"
    minSdkVersion 15
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
    buildTypes {
        release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.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.company:mylibrary:1.0@aar')
        {
            transitive = true
        }
    compile 'com.android.support:appcompat-v7:25.3.0'
    testCompile 'junit:junit:4.12'
}

If you create an .aar project there is a manifest file and it will get merged with the apps' manifest upon compile. 如果您创建一个.aar项目一个清单文件,它会被用在编译的应用程序清单合并。 That all works fine. 一切正常。 You can use any gradle dependency in the .aar project, even proguard obfuscation and signing. 您可以在.aar项目中使用任何gradle依赖项,甚至是proguard模糊处理和签名。

To compile an .aar library in the main app, put it like this in your dependencies of your build.grade file: 要在主应用程序中编译.aar库,请将它放在build.grade文件的dependencies中:

dependencies {
    compile(name:'your_lib_name', ext:'aar')
}

You have to copy the .aar file to the libs folder in your app folder. 你必须在.aar文件的复制libs文件夹中app文件夹中。

But one thing is clear: If someone uses your .aar file, he or she can look at the merged manifest file in android studio any time and you can not hide your dependencies . 但有一件事是清楚的:如果有人使用你的.aar文件,他或她可以随时查看android studio中的合并清单文件, 你无法隐藏你的依赖关系 At least, as far as I know. 至少,据我所知。 Android is driven strongly by open source libraries (even through maven) and in most cases you even have to include the licenses of your used external libraries, visible to the user as menu item or link in your app. Android受开源库(甚至通过maven)的强烈驱动,在大多数情况下,您甚至必须包含已使用外部库的许可证,用户可以将其看作应用程序中的菜单项或链接。 Otherwise you violate licenses and maybe even Terms of Use in some cases. 否则您在某些情况下违反许可证甚至可能违反使用条款。 You may not hide them. 你可能不会隐藏它们。

Hi @user3572586, 嗨@ user3572586,

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

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

  • You should use a maven repository (you have to publish the library in a private or public maven repo), you will not have the same issue. 您应该使用maven存储库(您必须在私有或公共maven存储库中发布库),您将不会遇到相同的问题。

  • In this case, gradle downloads the dependencies using the pom file which will contains the dependencies list. 在这种情况下,gradle使用包含依赖项列表的pom文件下载依赖项。

To hide paid lib from user better create package for individual library inside your project then generate .aar file. 要隐藏来自用户的付费lib,可以更好地为项目中的单个库创建包,然后生成.aar文件。 Myself i create one live stream lib same issue face on that time dependencies module's are move to inside my project then make .aar file. 我自己创建了一个实时流lib同一问题面,那时依赖模块移动到我的项目内,然后生成.aar文件。

You can create an *.aar file with all required dependencies in your Library project. 您可以在Library项目中创建包含所有必需依赖项的*.aar文件。

Now add this *.aar file either manually or as a dependency module to the targeted project. 现在手动或作为依赖模块将此* .aar文件添加到目标项目。 Once the main project is combined, this will give merged Android Manifest file . 一旦主项目合并,这将给出合并的Android Manifest file

The following structure is having Library project ( libraryModule ) added as a dependency to main project. 以下结构将库项目( libraryModule )添加为主项目的依赖项。 Again the libraryModule is having internal dependency for internallib 同样, libraryModuleinternallib具有内部依赖性

MainProject ===> libraryModule ===> internallib

在此输入图像描述

As per your comment, the multidex issue can be resolved by adding the following in build.gradle (Module: app) 根据您的评论,可以通过在build.gradle添加以下内容来解决multidex问题(模块:app)

defaultConfig {

 .....
 multiDexEnabled true
 .....
}

First: AAR files have manifests. 第一:AAR文件有清单。 And, if you use gradle to build them, they'll have gradle build scripts. 而且,如果你使用gradle来构建它们,它们将拥有gradle构建脚本。

So, the first part of your problem is moot. 所以,问题的第一部分是没有实际意义的。 Just create an AAR library and add the dependencies using the gradle-maven plugin (gradle) or the maven-android plugin (maven). 只需创建一个AAR库并使用gradle-maven插件(gradle)或maven-android插件(maven)添加依赖项。

For the second part, is a bit more tricky. 对于第二部分,有点棘手。 What you want is essentially a shaded AAR or Uber-AAR, an AAR file which brings the dependencies bundled into itself. 你想要的本质上是一个阴影的AAR或Uber-AAR,一个AAR文件,它将捆绑的依赖项带入其自身。 You can achieve that by using the maven-shade plugin or the gradle-shade plugin (depending on which system do you use to create the aar). 您可以通过使用maven-shade插件或gradle-shade插件来实现这一点(取决于您使用哪个系统来创建aar)。 With that approach, your clients can have a serious problem. 通过这种方法,您的客户可能会遇到严重问题。 You see, in java, your project works with the concept of dependency scopes and classpath. 您会看到,在java中,您的项目使用依赖范围和类路径的概念。 Scopes means the moment in time when the dependency is needed and who brings it. 范围是指需要依赖关系以及由谁带来依赖关系的时刻。 Classpath is the whole collection of classes used by a project, both the ones from dependencies or the ones from the project itself (so a client will always be able to know what are you using, because the compiler also needs to know). Classpath是项目使用的类的整个集合,包括来自依赖项的类或来自项目本身的类(因此客户端总是能够知道你在使用什么,因为编译器也需要知道)。 Therefore, you can't have several versions of a dependency in the path, because the classes would clash, rendering the project impossible to compile. 因此,您不能在路径中拥有多个版本的依赖项,因为这些类会发生冲突,导致项目无法编译。 That's why we usually don't attempt to hide which dependencies we are using; 这就是为什么我们通常不会试图隐藏我们正在使用的依赖项; it allows easier resolution of conflicts. 它可以更轻松地解决冲突。 If you use a popular dependency (like google maps) you risk your library becoming a problem for your clients and therefore, being discarded. 如果您使用流行的依赖项(如谷歌地图),您的库存就会成为客户的问题,因此被丢弃。 It would be best for you to develop the library with the designed contracts, then, a secondary plugin with a particular implementation using certain dependencies. 您最好使用设计的合同开发库,然后使用某些依赖项创建具有特定实现的辅助插件。 That way you'll project would be easier to maintain and not tied to the dependencies. 这样,您的项目将更容易维护,而不依赖于依赖项。 In that case, if the worst happens and your implementation's dependencies are incompatible with your client's projects, it will be easier to create a secondary implementation without compromising your code. 在这种情况下,如果最糟糕的情况发生并且您的实现的依赖项与客户端的项目不兼容,那么在不影响代码的情况下创建辅助实现将更容易。

Link to the maven shade plugin: https://maven.apache.org/plugins/maven-shade-plugin/ 链接到maven shade插件: https//maven.apache.org/plugins/maven-shade-plugin/

Link to the gradle shade plugin: https://github.com/zawn/android-shade-plugin 链接到gradle shade插件: https//github.com/zawn/android-shade-plugin

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

相关问题 如何在gradle依赖库代码中调试 - How to debug in gradle dependency library code 当用户添加gradle依赖项时如何使我的Android库源代码可见(因此不必在Android Studio中反编译代码) - How to make my Android library source code visible when a user adds the gradle dependency (so code doesn't have to be decompiled in Android Studio) 是否可以从Gradle依赖项修改Android Studio中的库文件源代码 - Is it possible to modify library file's source code in Android Studio from Gradle dependency 如何使用 gradle 创建 *.aar Android 库 - How to create *.aar Android library with gradle 将Gradle项目依赖项编译为Android库Arr - Compile gradle project dependency into android library arr 如何将gradle库依赖项添加到codenameone项目中 - How to add gradle library dependency to a codenameone project 创建一个 Gradle 设置以在 Jitpack 上发布与源代码的依赖关系? (等级 5.6.2) - Create a Gradle setup to publish dependency with source code on Jitpack ? (Gradle 5.6.2) 如何解决库根依赖项上的 gradle 依赖项冲突? - How to resolve gradle dependency conflicts on library root dependency? 创建gradle依赖项以从git导入 - Create a gradle dependency to import from git Gradle:如何仅从项目的一部分创建库? - Gradle: How to create a library from only part of a project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM