# Contributing
This guide have some instructions and tips on how to create a new Tachiyomi extension. Please **read
it carefully** if you're a new contributor or don't have any experience on the required languages
and knowledges.
This guide is not definitive and it's being updated over time. If you find any issue on it, feel
free to report it through a [Meta Issue](https://github.com/keiyoushi/extensions-source/issues/new?assignees=&labels=Meta+request&template=06_request_meta.yml)
or fixing it directly by submitting a Pull Request.
## Table of Contents
1. [Prerequisites](#prerequisites)
1. [Tools](#tools)
2. [Cloning the repository](#cloning-the-repository)
2. [Getting help](#getting-help)
3. [Writing an extension](#writing-an-extension)
1. [Setting up a new Gradle module](#setting-up-a-new-gradle-module)
2. [Core dependencies](#core-dependencies)
3. [Extension main class](#extension-main-class)
4. [Extension call flow](#extension-call-flow)
5. [Misc notes](#misc-notes)
6. [Advanced extension features](#advanced-extension-features)
4. [Multi-source themes](#multi-source-themes)
1. [The directory structure](#the-directory-structure)
2. [Development workflow](#development-workflow)
3. [Scaffolding overrides](#scaffolding-overrides)
4. [Additional Notes](#additional-notes)
5. [Running](#running)
6. [Debugging](#debugging)
1. [Android Debugger](#android-debugger)
2. [Logs](#logs)
3. [Inspecting network calls](#inspecting-network-calls)
4. [Using external network inspecting tools](#using-external-network-inspecting-tools)
7. [Building](#building)
8. [Submitting the changes](#submitting-the-changes)
1. [Pull Request checklist](#pull-request-checklist)
## Prerequisites
Before you start, please note that the ability to use following technologies is **required** and
that existing contributors will not actively teach them to you.
- Basic [Android development](https://developer.android.com/)
- [Kotlin](https://kotlinlang.org/)
- Web scraping
- [HTML](https://developer.mozilla.org/en-US/docs/Web/HTML)
- [CSS selectors](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors)
- [OkHttp](https://square.github.io/okhttp/)
- [JSoup](https://jsoup.org/)
### Tools
- [Android Studio](https://developer.android.com/studio)
- Emulator or phone with developer options enabled and a recent version of Tachiyomi installed
- [Icon Generator](https://as280093.github.io/AndroidAssetStudio/icons-launcher.html)
### Cloning the repository
Some alternative steps can be followed to skip unrelated sources, which will make it faster to pull,
navigate and build. This will also reduce disk usage and network traffic.
**These steps are only needed when the repo is huge and contains a lot of sources. If the repo is
small, just do a normal full clone instead.**
Steps
1. Do a partial clone.
```bash
git clone --filter=blob:none --sparse
cd extensions/
```
2. Configure sparse checkout.
There are two modes of pattern matching. The default is cone (🔺) mode.
Cone mode enables significantly faster pattern matching for big monorepos
and the sparse index feature to make Git commands more responsive.
In this mode, you can only filter by file path, which is less flexible
and might require more work when the project structure changes.
You can skip this code block to use legacy mode if you want easier filters.
It won't be much slower as the repo doesn't have that many files.
To enable cone mode together with sparse index, follow these steps:
```bash
git sparse-checkout set --cone --sparse-index
# add project folders
git sparse-checkout add .run buildSrc core gradle lib multisrc/src/main/java/generator
# add a single source
git sparse-checkout add src//
## Getting help
- Join [the Discord server](https://discord.gg/3FbCpdKbdY) for online help and to ask questions while
developing your extension. When doing so, please ask it in the `#programming` channel.
- There are some features and tricks that are not explored in this document. Refer to existing
extension code for examples.
## Writing an extension
The quickest way to get started is to copy an existing extension's folder structure and renaming it
as needed. We also recommend reading through a few existing extensions' code before you start.
### Setting up a new Gradle module
Each extension should reside in `src//`. Use `all` as `` if your target
source supports multiple languages or if it could support multiple sources.
The `` used in the folder inside `src` should be the major `language` part. For example, if
you will be creating a `pt-BR` source, use `` here as `pt` only. Inside the source class, use
the full locale string instead.
### Loading a subset of Gradle modules
By default, all individual and generated multisrc extensions are loaded for local development.
This may be inconvenient if you only need to work on one extension at a time.
To adjust which modules are loaded, make adjustments to the `settings.gradle.kts` file as needed.
#### Extension file structure
The simplest extension structure looks like this:
```console
$ tree src///
src///
├── AndroidManifest.xml
├── build.gradle
├── res
│  ├── mipmap-hdpi
│  │  └── ic_launcher.png
│  ├── mipmap-mdpi
│  │  └── ic_launcher.png
│  ├── mipmap-xhdpi
│  │  └── ic_launcher.png
│  ├── mipmap-xxhdpi
│  │  └── ic_launcher.png
│  ├── mipmap-xxxhdpi
│  │  └── ic_launcher.png
│  └── web_hi_res_512.png
└── src
└── eu
└── kanade
└── tachiyomi
└── extension
└──
└──
└── .kt
13 directories, 9 files
```
#### AndroidManifest.xml
A minimal [Android manifest file](https://developer.android.com/guide/topics/manifest/manifest-intro)
is needed for Android to recognize an extension when it's compiled into an APK file. You can also add
intent filters inside this file (see [URL intent filter](#url-intent-filter) for more information).
#### build.gradle
Make sure that your new extension's `build.gradle` file follows the following structure:
```gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
ext {
extName = ''
pkgNameSuffix = '.'
extClass = '.'
extVersionCode = 1
isNsfw = true
}
apply from: "$rootDir/common.gradle"
```
| Field | Description |
|------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `extName` | The name of the extension. |
| `pkgNameSuffix` | A unique suffix added to `eu.kanade.tachiyomi.extension`. The language and the site name should be enough. Remember your extension code implementation must be placed in this package. |
| `extClass` | Points to the class that implements `Source`. You can use a relative path starting with a dot (the package name is the base path). This is used to find and instantiate the source(s). |
| `extVersionCode` | The extension version code. This must be a positive integer and incremented with any change to the code. |
| `libVersion` | (Optional, defaults to `1.4`) The version of the [extensions library](https://github.com/tachiyomiorg/extensions-lib) used. |
| `isNsfw` | (Optional, defaults to `false`) Flag to indicate that a source contains NSFW content. |
The extension's version name is generated automatically by concatenating `libVersion` and `extVersionCode`.
With the example used above, the version would be `1.4.1`.
### Core dependencies
#### Extension API
Extensions rely on [extensions-lib](https://github.com/tachiyomiorg/extensions-lib), which provides
some interfaces and stubs from the [app](https://github.com/tachiyomiorg/tachiyomi) for compilation
purposes. The actual implementations can be found [here](https://github.com/tachiyomiorg/tachiyomi/tree/master/app/src/main/java/eu/kanade/tachiyomi/source).
Referencing the actual implementation will help with understanding extensions' call flow.
#### DataImage library
[`lib-dataimage`](https://github.com/keiyoushi/extensions-source/tree/main/lib/dataimage) is a library
for handling [base 64 encoded image data](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs)
using an [OkHttp interceptor](https://square.github.io/okhttp/interceptors/).
```gradle
dependencies {
implementation(project(':lib-dataimage'))
}
```
#### i18n library
[`lib-i18n`](https://github.com/keiyoushi/extensions-source/tree/main/lib/i18n) is a library for handling
internationalization in the sources. It allows loading `.properties` files with messages located under
the `assets/i18n` folder of each extension, that can be used to translate strings under the source.
```gradle
dependencies {
implementation(project(':lib-i18n'))
}
```
#### Additional dependencies
If you find yourself needing additional functionality, you can add more dependencies to your `build.gradle`
file. Many of [the dependencies](https://github.com/tachiyomiorg/tachiyomi/blob/master/app/build.gradle.kts)
from the main Tachiyomi app are exposed to extensions by default.
> [!NOTE]
> Several dependencies are already exposed to all extensions via Gradle's version catalog.
> To view which are available check the `gradle/libs.versions.toml` file.
Notice that we're using `compileOnly` instead of `implementation` if the app already contains it.
You could use `implementation` instead for a new dependency, or you prefer not to rely on whatever
the main app has at the expense of app size.
> [!IMPORTANT]
> Using `compileOnly` restricts you to versions that must be compatible with those used in
> [the latest stable version of Tachiyomi](https://github.com/tachiyomiorg/tachiyomi/releases/latest).
### Extension main class
The class which is referenced and defined by `extClass` in `build.gradle`. This class should implement
either `SourceFactory` or extend one of the `Source` implementations: `HttpSource` or `ParsedHttpSource`.
| Class | Description |
|--------------------|----------------------------------------------------------------------------------------------------------------------------------|
| `SourceFactory` | Used to expose multiple `Source`s. Use this in case of a source that supports multiple languages or mirrors of the same website. |
| `HttpSource` | For online source, where requests are made using HTTP. |
| `ParsedHttpSource` | Similar to `HttpSource`, but has methods useful for scraping pages. |
#### Main class key variables
| Field | Description |
|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `name` | Name displayed in the "Sources" tab in Tachiyomi. |
| `baseUrl` | Base URL of the source without any trailing slashes. |
| `lang` | An ISO 639-1 compliant language code (two letters in lower case in most cases, but can also include the country/dialect part by using a simple dash character). |
| `id` | Identifier of your source, automatically set in `HttpSource`. It should only be manually overriden if you need to copy an existing autogenerated ID. |
### Extension call flow
#### Popular Manga
a.k.a. the Browse source entry point in the app (invoked by tapping on the source name).
- The app calls `fetchPopularManga` which should return a `MangasPage` containing the first batch of
found `SManga` entries.
- This method supports pagination. When user scrolls the manga list and more results must be fetched,
the app calls it again with increasing `page` values (starting with `page=1`). This continues while
`MangasPage.hasNextPage` is passed as `true` and `MangasPage.mangas` is not empty.
- To show the list properly, the app needs `url`, `title` and `thumbnail_url`. You **must** set them
here. The rest of the fields could be filled later (refer to Manga Details below).
- You should set `thumbnail_url` if is available, if not, `getMangaDetails` will be **immediately**
called (this will increase network calls heavily and should be avoided).
#### Latest Manga
a.k.a. the Latest source entry point in the app (invoked by tapping on the "Latest" button beside
the source name).
- Enabled if `supportsLatest` is `true` for a source
- Similar to popular manga, but should be fetching the latest entries from a source.
#### Manga Search
- When the user searches inside the app, `fetchSearchManga` will be called and the rest of the flow
is similar to what happens with `fetchPopularManga`.
- If search functionality is not available, return `Observable.just(MangasPage(emptyList(), false))`
- `getFilterList` will be called to get all filters and filter types.
##### Filters
The search flow have support to filters that can be added to a `FilterList` inside the `getFilterList`
method. When the user changes the filters' state, they will be passed to the `searchRequest`, and they
can be iterated to create the request (by getting the `filter.state` value, where the type varies
depending on the `Filter` used). You can check the filter types available [here](https://github.com/tachiyomiorg/tachiyomi/blob/master/source-api/src/commonMain/kotlin/eu/kanade/tachiyomi/source/model/Filter.kt)
and in the table below.
| Filter | State type | Description |
|--------------------|-------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `Filter.Header` | None | A simple header. Useful for separating sections in the list or showing any note or warning to the user. |
| `Filter.Separator` | None | A line separator. Useful for visual distinction between sections. |
| `Filter.Select` | `Int` | A select control, similar to HTML's `