Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
Moduły to kontener na kod źródłowy aplikacji, pliki zasobów i ustawienia na poziomie aplikacji, np. plik kompilacji na poziomie modułu i plik manifestu Androida.
Każdy moduł można niezależnie tworzyć, testować i debugować.
Android Studio wykorzystuje moduły ułatwiające dodawanie nowych urządzeń do projektu. Wykonując kilka prostych czynności w Android Studio,
możesz utworzyć moduł z kodem dostosowanym do typu urządzenia, np. Wear OS lub Androida TV. Android Studio automatycznie tworzy katalogi modułów, takie jak katalogi źródeł i zasobów, oraz domyślny plik build.gradle odpowiedni dla typu urządzenia.
Android Studio tworzy też moduły urządzeń z zalecanymi konfiguracjami kompilacji, np. korzysta z biblioteki TalkBack dla modułów Androida TV.
Na tej stronie opisujemy, jak dodać nowy moduł dla określonego urządzenia.
Android Studio ułatwia też
dodawanie biblioteki lub modułu Google Cloud do projektu. Szczegółowe informacje o tworzeniu modułu biblioteki znajdziesz w artykule o tworzeniu modułu biblioteki.
Tworzenie nowego modułu
Aby dodać do projektu nowy moduł dla nowego urządzenia, wykonaj te czynności:
Kliknij File (Plik) > New (Nowy) > New Module (Nowy moduł).
W wyświetlonym oknie Utwórz nowy moduł Android Studio zawiera te moduły urządzeń:
Moduł telefonu i tabletu
Moduł Wear OS
Moduł Android TV
Moduł szklany
Wybierz moduł dla wybranego urządzenia i kliknij Dalej.
W formularzu Skonfiguruj nowy moduł wpisz te informacje:
Nazwa aplikacji: ta nazwa będzie używana jako tytuł ikony programu uruchamiającego aplikacje dla nowego modułu.
Nazwa modułu: ten tekst jest używany jako nazwa folderu, w którym widoczne są pliki kodu źródłowego i plików zasobów.
Nazwa pakietu: jest to przestrzeń nazw Javy dla kodu w Twoim module. Jest on dodawany jako atrybut package w pliku manifestu Androida modułu.
Minimalny pakiet SDK: to ustawienie wskazuje najniższą wersję platformy Androida obsługiwaną przez moduł aplikacji. Ta wartość ustawia w pliku build.gradle atrybut minSdkVersion, który możesz później edytować.
Następnie kliknij Dalej.
W zależności od wybranego modułu urządzenia na następnej stronie pojawią się odpowiednie szablony kodu, których możesz użyć jako głównego działania. Kliknij szablon aktywności, od którego chcesz zacząć, a potem kliknij Dalej. Jeśli nie potrzebujesz żadnej aktywności, kliknij kolejno Dodaj brak aktywności i Zakończ.
Jeśli wybierzesz szablon aktywności, podaj jego ustawienia na stronie Dostosuj aktywność. Większość szablonów wymaga podania nazwy działania, nazwy układu, tytułu i języka źródłowego, ale każdy szablon ma ustawienia związane z daną aktywnością. Kliknij Zakończ. Po utworzeniu modułu aplikacji z szablonem aktywności możesz go natychmiast uruchomić i przetestować na swoim urządzeniu.
Android Studio utworzy wszystkie pliki potrzebne dla nowego modułu i zsynchronizuje projekt z plikami Gradle nowego modułu. Dodanie modułu dla nowego urządzenia powoduje też dodanie wszystkich wymaganych zależności urządzenia docelowego do pliku kompilacji modułu.
Po zakończeniu synchronizacji projektu Gradle nowy moduł pojawi się w oknie Projekt po lewej stronie. Jeśli nie widzisz folderu nowego modułu, sprawdź, czy w oknie wyświetla się widok Androida.
Importowanie modułu
Aby zaimportować do projektu istniejący moduł, wykonaj te czynności:
Kliknij Plik > Nowy > Importuj moduł.
W polu Katalog źródłowy wpisz lub wybierz katalog modułów, które chcesz zaimportować:
Jeśli importujesz jeden moduł, podaj jego katalog główny.
Jeśli importujesz wiele modułów z projektu, wskaż folder projektu. Dla każdego modułu w folderze pojawi się pole z danymi Lokalizacja źródłowa oraz Nazwa modułu. Sprawdź, czy przy każdym module, który chcesz zaimportować, zaznaczone jest pole Importuj.
Jeśli Twoje moduły mają inne zależności, znajdziesz je na liście do zaimportowania w sekcji Dodatkowe wymagane moduły.
Wpisz odpowiednie nazwy modułów w polach Nazwa modułu.
Kliknij Zakończ.
Po zaimportowaniu modułu pojawi się on w oknie projektu
po lewej stronie.
Dalsze kroki
Po dodaniu nowego modułu możesz zmodyfikować jego kod i zasoby, skonfigurować ustawienia kompilowania modułów i utworzyć moduł. Możesz go uruchomić i debugować
jak każdą inną aplikację.
Przydatny będzie też kod i zasoby, które pozwolą na prawidłową obsługę nowego urządzenia. Więcej informacji o tworzeniu modułów aplikacji na różne typy urządzeń znajdziesz w odpowiedniej dokumentacji:
Przy tworzeniu nowego modułu możesz utworzyć kod niezależny od urządzenia, który jest już zduplikowany w innym module aplikacji. Zamiast duplikować kod, rozważ przeniesienie udostępnionego kodu do modułu biblioteki i dodanie tej biblioteki jako zależności do modułów aplikacji.
Więcej informacji o tworzeniu modułu biblioteki i dodawaniu go jako zależności znajdziesz w artykule Tworzenie biblioteki na Androida.
Treść strony i umieszczone na niej fragmenty kodu podlegają licencjom opisanym w Licencji na treści. Java i OpenJDK są znakami towarowymi lub zastrzeżonymi znakami towarowymi należącymi do firmy Oracle lub jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2024-04-02 UTC.
[[["Łatwo zrozumieć","easyToUnderstand","thumb-up"],["Rozwiązało to mój problem","solvedMyProblem","thumb-up"],["Inne","otherUp","thumb-up"]],[["Brak potrzebnych mi informacji","missingTheInformationINeed","thumb-down"],["Zbyt skomplikowane / zbyt wiele czynności do wykonania","tooComplicatedTooManySteps","thumb-down"],["Nieaktualne treści","outOfDate","thumb-down"],["Problem z tłumaczeniem","translationIssue","thumb-down"],["Problem z przykładami/kodem","samplesCodeIssue","thumb-down"],["Inne","otherDown","thumb-down"]],["Ostatnia aktualizacja: 2024-04-02 UTC."],[],[],null,["# Add a module for a new device\n\nModules provide a container for your app's\nsource code, resource files, and app level settings, such as the\nmodule-level build file and Android manifest file.\nEach module can be independently built, tested, and debugged.\n\nAndroid Studio uses modules to make it easy to add new devices to\nyour project. By following a few simple steps in Android Studio,\nyou can create a module to contain code that's specific to a device type,\nsuch as Wear OS or Android TV. Android Studio automatically\ncreates module directories, such as source and resource directories, and\na default `build.gradle` file appropriate for the device type.\nAlso, Android Studio creates device modules with recommended build configurations,\nsuch as using the Leanback library for Android TV modules.\n\nThis page describes how to add a new module for a specific device.\n\nAndroid Studio also makes it easy to add a library or\nGoogle Cloud module to your project. For details on creating a library module,\nsee [Create a\nLibrary Module](/studio/projects/android-library#CreateLibrary).\n\n\nCreate a new module\n-------------------\n\nTo add a new module to your project for a new device, proceed as follows:\n\n1. Click **File** \\\u003e **New** \\\u003e **New Module**.\n2. In the **Create New Module** window that appears, Android Studio offers the following device modules:\n - Phone \\& Tablet Module\n - Wear OS Module\n - Android TV Module\n - Glass Module\n\n Select the module for the device you want, and then click **Next**.\n3. In the **Configure your new module** form, enter the following details:\n - **Application Name**: This name is used as the title of your app launcher icon for the new module.\n - **Module Name**: This text is used as the name of the folder where your source code and resources files are visible.\n - **Package Name** : This is the Java namespace for the code in your module. It is added as the [`package`](/guide/topics/manifest/manifest-element#package) attribute in the module's [Android manifest file](/guide/topics/manifest/manifest-intro). **Important:** Module package names must be globally unique. You can't have two modules with the same package name in the same project.\n - **Minimum SDK** : This setting indicates the lowest version of the Android platform that the app module supports. This value sets the `minSdkVersion` attribute in the `build.gradle` file, which you can edit later.\n\n Then click **Next**.\n4. Depending on which device module you selected, the following page displays a selection of appropriate code templates you can select to use as your main activity. Click an activity template with which you want to start, and then click **Next** . If you don't need an activity, click **Add No Activity** , click **Finish**, and then you're done.\n5. If you chose an activity template, enter the settings for your activity on the **Customize\n the Activity** page. Most templates ask for an **Activity Name** , **Layout\n Name** , **Title** , and **Source Language** , but each template has activity-specific settings. Click **Finish**. When you create an app module with an activity template, you can immediately run and test the module on your device.\n\nAndroid Studio creates all the necessary files for the new module and\nsyncs the project with the new module gradle files. Adding a module\nfor a new device also adds any required dependencies for the target\ndevice to the module's build file.\n\nOnce the Gradle project sync completes, the new module appears in\nthe **Project** window on the left. If you don't see the new module\nfolder, make sure the window is displaying the [Android view](/studio/projects#ProjectFiles).\n\nImport a module\n---------------\n\nTo import an existing module into your project, proceed as follows:\n\n1. Click **File \\\u003e New \\\u003e Import Module**.\n2. In the **Source directory** box, type or select the directory of the module(s) that you want to import:\n - If you are importing one module, indicate its root directory.\n - If you are importing multiple modules from a project, indicate the project folder. For each module inside the folder, a box appears and indicates the **Source location** and **Module name** . Make sure the **Import** box is checked for each module that you want to import.\n\n If your module(s) have other dependencies, they will be listed to import under **Additional required modules**.\n3. Type your desired module name(s) in the **Module name** field(s).\n4. Click **Finish.**\n\nOnce the module is imported, it appears in the [**Project** window](https://developer.android.com/studio/projects/index.html#ProjectFiles) on the left.\n\nNext steps\n----------\n\nOnce you've added a new module, you can modify the module code and resources,\nconfigure module build settings, and build the module. You can also run and debug\nthe module like any other app.\n\n- To learn about build settings for a module, see [The Module-level Build File](/studio/build#module-level).\n- To build and run a specific module, see [Select and build a different module](/studio/run#build-module).\n\nYou'll also want to add code and resources to properly support the new\ndevice. For more information about how to develop app modules for different\ndevice types, see the corresponding documentation:\n\n- For Wear OS modules: [Creating and Running a Wearable App](https://developer.android.com/training/wearables/apps/creating.html)\n- For Android TV modules: [Get Started with TV Apps](https://developer.android.com/training/tv/start/start.html)\n- For Glass modules: [GDK Quick Start](https://developers.google.com/glass/develop/gdk/quick-start#for_android_experts)\n\nAs you develop your new module, you might create device independent code that is already\nduplicated in a different app module. Instead of maintaining duplicate code, consider moving\nthe shared code to a library module and adding the library as a dependency to your app modules.\nFor more information on creating a library module and adding it as a dependency, see\n[Create an Android Library](/studio/projects/android-library)."]]