简体   繁体   English

在 Android Studio 中,如何将应用项目更改为库?

[英]in Android Studio, how can I change an app project to a library?

I'm sure this is simple, but since I just started using Android Studio today, I can't find it.我确定这很简单,但是由于我今天才开始使用 Android Studio,所以我找不到它。 I have also googled for the past hour and found no posts that specify:在过去的一个小时里,我也在谷歌上搜索,没有发现任何帖子指定:

how to change an existing application project to a library.如何将现有的应用程序项目更改为库。

Everything I found was about how to create a new library project.我发现的一切都是关于如何创建一个新的库项目。 Not how to convert one.不是如何转换一个。

A sub-question, is how can I see if a project is configured as an application or a library?一个子问题,我如何查看项目是配置为应用程序还是库? I would hope that the answer to both of these questions is the same.我希望这两个问题的答案都是一样的。

Open your build.gradle of your app (inside your app directory) and modify : 打开应用程序的build.gradle (在app目录中)并修改:

apply plugin: 'com.android.application'

with

apply plugin: 'com.android.library'

If you have an applicationId in your build.gradle remove it : 如果build.gradle中有applicationId ,请将build.gradle删除:

defaultConfig {
     applicationId "com.your.application.id"
}

then clean your project and rebuild or just sync your gradle from Android Studio 然后清理您的项目并重建或只是从Android Studio同步您的gradle

If you have added some extra gradle properties like applicationVariants.all you must replace with libraryVariants.all and vice-versa if you convert a library to application 如果您添加了一些额外的gradle属性,例如applicationVariants.all ,则必须使用libraryVariants.all替换,反之亦然,如果将库转换为应用程序

If you want to add a new step to your reconversion you can change the module name "app" created by default by Android-Studio with a name more adapted to a library module. 如果要为重新转换添加新步骤,可以更改Android-Studio默认创建的模块名称“app”,其名称更适合库模块。 You can rename the directory app with <your_module_name> . 您可以使用<your_module_name>重命名目录应用程序。 open settings.gradle file (at the root of your project) and replace app with <your_module_name> . 打开settings.gradle文件(在项目的根目录下)并用<your_module_name>替换app。 Then Go to Menu Build > Make module <your_module_name> and there you are, your module is renamed. 然后转到菜单构建>生成模块<your_module_name> ,您就可以重命名模块了。

Changing to Library更改为库

  1. Goto android project, where you need to change as Library module.转到 android 项目,您需要在其中更改为库模块。

  2. In build.gradle(:app) file,在 build.gradle(:app) 文件中,

  • change this line to将此行更改为

    plugins { id 'com.android.application' }

    to

    plugins { id 'com.android.library' }
  1. Delete the line for the applicationId in same build.gradle(:app) file删除同一 build.gradle(:app) 文件中applicationId的行

    defaultConfig { applicationId "com.project.example" //remove this line minSdk 21 targetSdk 31 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" }
  2. Now Sync Project with Gradle Files.现在将项目与 Gradle 文件同步。

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

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