Android StudioはVCS(Version Control System:バージョン管理システム)を使って、プロジェクトの変更履歴の管理が可能です。
Android Studioで利用可能なVCSはいくつかありますが、Gitを取り上げて、トークンを使ったリモートリポジトリのクローン(コピー)とプル(履歴の取得)の方法を紹介します。
BitbucketよりGitHubの方がシンプルで使い易いかも…
※環境:Android Studio Narwhal 3 Feature Drop | 2025.1.3
Git version is 2.50.1
※前提:リモートリポジトリの構築済み
目次
トークンによるアクセス
リポジトリの所有者以外のユーザーが、リポジトリへアクセスしたい場合は、所有者の発行するトークンを使うとアクセス出来ます。
トークンとは
トークンとは「ユーザーの権限の一部を委譲したチケット」です。


◎:誰でも無条件に可(≒全世界に公開) 〇:トークンにより可 X:出来ない


◎:誰でも無条件に可(≒全世界に公開) 〇:トークンにより可 X:出来ない
このチケットを持つアプリは、所有者に成り代わってアクセスを行えます。
アクセスの様子
トークンはGitHubのユーザーが発行します。
ですので、ユーザー(UserA)が所持するリポジトリのみに有効です。他者(UserB)が発行したトークンは無効であり、アクセスは許されません。
トークンは、「1:1(トークン:ツール)」や「1:多」といった配布が出来ます。
「1:多」の配布は、トークンの有効期限が決め難かったり(同じ期限になってしまう)、配布先毎にトークンを廃止できなかったり、許可範囲の制限が緩くなるのでお勧めしません。
手間がかかっても、配布先毎にトークンを発行する方法が良いです。
また、トークンの注意点は「所有者に成り代わってアクセスする」点です。トークンはユーザーに関連付けられています。
委譲される権限の内容次第で、ユーザーが所有する全てのリポジトリへアクセス出来る場合があります。
2種類のトークン
GitHubは、表のような2種類のトークンが発行出来ます。
トークン | 特徴 |
---|---|
Tokens(Classic) | ・発行元のユーザーはサインインユーザーになる ・有効期間が指定できる ・全てのリポジトリが対象になる ・権限の範囲指定が大雑把 |
Fine-grained tokens | ・発行元のユーザー(共有アカウントを含む)が選択できる ・有効期間が指定できる ・対象のリポジトリが指定できる、一部または全て ・権限の範囲指定が詳細 |
GitHubのドキュメントでは「Fine-grained tokens」の利用が推奨されています。
両者の特徴は異なりますが、利用方法は同じです。この記事は「Fine-grained tokens」を利用する方法のみを記載します。
—
また、Tokens(Classic)にしか出来ない項目があるようです。その場合は、Tokens(Classic)を使う必要があります。詳細はGitHubドキュメントを参照して下さい。
トークンの発行
GitHubのサイトへサインイン後、Settingsページの「Developer Settings」を開きます。
「Generate new token」ボタンを押して、トークンの作成を開始します。
トークンの情報を入力していきます。
ここでは、対象のリポジトリを限定したトークンを作成しています。
また、許可する権限に「Contents」を指定し、アクセスに「Read and Write」を指定すると、リポジトリに対してRead/Writeが可能になります。つまり、Clone/Push/Pullが出来ます。
「Medadata」は自動的に付与される権限です。そのままで良いです。
再確認のダイアログが表示されるので、問題なければボタンを押します。
最後に、作成されたトークンが表示されます。
ここで、表示されているトークンを、どこかに保存または書き留めて下さい。この表示を閉じると、以後、トークンの再表示は出来ません。
クローンの実施
リポジトリのクローンを行う方法は、次の2通りがあります


「Clone」ボタンの押下でクローンが開始されます。
この後、トークンによるユーザー認証の手続きがあります。
ユーザー認証
ユーザー認証(Log in)のダイアログが表示されたら、トークンの利用を選びます。
トークンを求められるので、発行済みのトークン(発行時に書き留めた物)を入力します。
プルの実施
プルによりリモートリポジトリから最新の履歴が取得できます。
リポジトリのプルは「Git」メニューから行います。
「Pull」ボタンの押下でプルが開始されます。
リモートの定義
クローン後のAndroid Studioプロジェクトは、リモートリポジトリが定義されています。
> git config --local --list
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
submodule.active=.
remote.origin.url=https://github.com/ユーザ名/SampleApp.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
クローンを行った時に指定したURLが、そのまま指定されています。
トークンの保持
発行されたトークンはAndroid Studio内に保持されるので、クローン後のアクセスは、ユーザ認証が不要です。
トークンの状況は、Android Studioの「Settings->Version Control->GitHub」で確認できます。
Applendix:トークンの許可項目
トークンを作成する際に、「Permissions」欄で許可する権限を選びます。
その際に「Learn more about permissions」を参照すると、権限の詳細を確認できるのですが、Web APIのコマンドが表示されるだけです。コマンドと権限が結び付かないので、非常に解り難いです。
ですので、権限の概要を抽出して、表にしました。参考にしてください。
項目 | 概要 |
---|---|
Actions | Workflows, workflow runs and artifacts. |
Administration | Repository creation, deletion, settings, teams, and collaborators. |
Attestations | Create and retrieve attestations for a repository. |
Code scanning alerts | View and manage code scanning alerts. |
Codespaces | Create, edit, delete and list Codespaces. |
Codespaces lifecycle admin | Manage the lifecycle of Codespaces, including starting and stopping. |
Codespaces metadata | Access Codespaces metadata including the devcontainers and machine type. |
Codespaces secrets | estrict Codespaces user secrets modifications to specific repositories. |
Commit statuses | Commit statuses. |
Contents | Repository contents, commits, branches, downloads, releases, and merges. |
Custom properties | Read and write repository custom properties values at the repository level, when allowed by the property. |
Dependabot alerts | Retrieve Dependabot alerts. |
Dependabot secrets | Manage Dependabot repository secrets. |
Deployments | Deployments and deployment statuses. |
Discussions | Discussions and related comments and labels. |
Enviroments | Manage repository environments. |
Issues | Issues and related comments, assignees, labels, and milestones. |
Merge queues | Manage a repository's merge queues |
Metadata | Search repositories, list collaborators, and access repository metadata. |
Pages | Retrieve Pages statuses, configuration, and builds, as well as create new builds. |
Pull requests | Pull requests and related comments, assignees, labels, milestones, and merges. |
Repository security advisories | View and manage repository security advisories. |
Secret scanning alart dismissal requests | View and manage secret scanning alert dismissal requests |
Secret scanning alerts | View and manage secret scanning alerts. |
Secret scanning push protection bypass requests | Review and manage repository secret scanning push protection bypass requests. |
Secrets | Manage Actions repository secrets. |
Variables | Manage Actions repository variables. |
Webhooks | Manage the post-receive hooks for a repository. |
Workflows | Update GitHub Action workflow files. |
項目 | 概要 |
---|---|
Block another user | View and manage users blocked by the user. |
Codespaces user secrets | Manage Codespaces user secrets. |
Copilot Chat | This application will receive your GitHub ID, your GitHub Copilot Chat session messages (not including messages sent to another application), and timestamps of provided GitHub Copilot Chat session messages. This permission must be enabled for Copilot Extensions. |
Copilot Editor Context | This application will receive bits of Editor Context (e.g. currently opened file) whenever you send it a message through Copilot Chat. |
Copilot Requests | Send Copilot requests. |
Email addresses | Manage a user's email addresses. |
Events | View events triggered by a user's activity. |
Followers | A user's followers |
GPG keys | View and manage a user's GPG keys. |
Gists | Create and modify a user's gists and comments. |
Git SSH keys | Git SSH keys |
Interaction limits | Interaction limits on repositories |
Knowledge bases | View knowledge bases for a user. |
Models | Allows access to GitHub Models. |
Plan | View a user's plan. |
Private repository invitations | View a user's invitations to private repositories |
Profile | Manage a user's profile settings. |
SSH signing keys | View and manage a user's SSH signing keys. |
Starring | List and manage repositories a user is starring. |
Watching | List and change repositories a user is subscribed to. |
関連記事: