简体   繁体   English

Android 项目转库项目

[英]Android convert project to library project

I am trying to convert an Android Studio project as a library.我正在尝试将 Android Studio 项目转换为库。

What is the best way to do this and the steps I need to follow?执行此操作的最佳方法是什么以及我需要遵循的步骤?

What file can I remove from that library project, so that the library look cleaner?我可以从该库项目中删除什么文件,以便库看起来更干净?

An Android library is structurally the same as an Android app module. Android 库在结构上与 Android 应用模块相同。 It can include everything needed to build an app, including source code, resource files, and an Android manifest .它可以包含构建应用程序所需的一切,包括源代码、资源文件和 Android 清单 However, instead of compiling into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module.但是,Android 库不会编译为在设备上运行的 APK,而是编译为 Android 存档 (AAR) 文件,您可以将其用作 Android 应用模块的依赖项。 Unlike JAR files, AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.与 JAR 文件不同,AAR 文件可以包含 Android 资源和清单文件,除了 Java 类和方法之外,它还允许您捆绑共享资源,如布局和可绘制对象。

Convert an app module to a library module将应用模块转换为库模块

Open the build.gradle file for the existing app module.打开现有应用程序模块的 build.gradle 文件。 At the top, you should see the following:在顶部,您应该看到以下内容:

apply plugin: 'com.android.application'

Change the plugin assignment as shown here:更改插件分配,如下所示:

apply plugin: 'com.android.library'

Also in this file, you have to delete this line同样在这个文件中,你必须删除这一行

applicationId "your.application.id"

Click Sync Project with Gradle Files.单击将项目与 Gradle 文件同步。

Check this for more details https://developer.android.com/studio/projects/android-library.html检查此以获取更多详细信息https://developer.android.com/studio/projects/android-library.html

You need to do THREE things:你需要做三件事:

1- change the apply plugin: 'com.android.application' in the build.gradle of the module to apply plugin: 'com.android.library' 1- 将模块的build.gradle中的apply plugin: 'com.android.application'更改为apply plugin: 'com.android.library'

2-delete applicationId "your.application.id" in the module's build.gradle 2-删除模块build.gradle中的applicationId "your.application.id"

3-remove <application and it its content from your module's AndroidManifest.xml file 3-从模块的AndroidManifest.xml文件中删除<application及其内容

4-also remove the resources in mipmap directories and the themes and colors.xml (or any other resource file that might conflict with the parent or app module) to avoid any surprises and crashes when using this library module from a module that has the same files.(I have wasted 4 hours to know the reason of app crash at startup with no helpful logs). 4-同时删除mipmap目录和themes中的资源和colors.xml (或任何其他可能与父或应用程序模块冲突的资源文件)以避免从具有相同文件的模块使用此库模块时出现任何意外和崩溃。 (我浪费了 4 个小时才知道应用程序在启动时崩溃的原因,但没有有用的日志)。

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

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