4

Android依赖笔记

 2 years ago
source link: https://aprildown.xyz/2022/06/02/android-library-dependency/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Android依赖笔记

2022-06-02Android

整理自The definitive guide to Android library development by Jeroen Mols, Plaid EN

  • 相比JAR(代码压缩包?),AAR还有Resources和Manifest,但二者都没有签名且不包括依赖。它们各自所需的依赖定义于pom文件中。

  • 如果所需的依赖是本地文件,则需要方法来绕过限制:

    1. 把依赖发布到Maven
    2. FAR AAR插件
    3. 把依赖内容复制到Library中
  • 限制多个依赖间的内部类

    1. package com.example.internal.database
    2. 混淆为package com.example.internal.a
    3. 把依赖内容复制到Library中
  • Transitive Dependency及在pom文件中定义的依赖

    Grdle会默认选择所有版本最高的依赖。调整方法有

    1. 忽略依赖的Transitive Dependency

      implementation ('com.example:library:1.0.0') {
      exclude group: 'com.squareup.okhttp3', module: 'okhttp'
      }
    2. 全局指定依赖版本

      configurations.all {
      resolutionStrategy {
      force 'com.squareup.okhttp3:okhttp:3.12.0'
      }
      }

    这些方法可能会造成编译或运行时失败,尽量避免它们。

  • 开发Library时,使用稳定版本(几个月到一两年)的依赖,最好不要更新。

  • res/values/public.xml声明后,只是Android Studio不再自动补全了。

    android {
    resourcePrefix 'mylib_'
    }

    来限定名称空间。不过只是让Android Studio警告一下。

  • 设置为必须声明Class Visibility

    android {
    kotlinOptions {
    freeCompilerArgs += '-Xexplicit-api=strict'
    }
    }

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK