2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

最近Gradleで遊んでるので備忘録的に書いていく
Androidのプロジェクトを作る時、大抵New Projectから作っていくと思うが、今回は1からbuild.gradleを書いてAndroidプロジェクトを作ってみる

gradleプロジェクトを作る

とはいえgradleの機能は使っていく
gradleを用いたプロジェクトはgradle initで作れる

$ mkdir gradle-playground && cd gradle-playground
$ gradle init

Select type of build to generate:
  1: Application
  2: Library
  3: Gradle plugin
  4: Basic (build structure only)
Enter selection (default: Application) [1..4] 4

Project name (default: gradle-playground): 

Select build script DSL:
  1: Kotlin
  2: Groovy
Enter selection (default: Kotlin) [1..2] 1

Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] 


> Task :init
Learn more about Gradle by exploring our Samples at https://docs.gradle.org/8.13/samples

BUILD SUCCESSFUL in 6s
1 actionable task: 1 executed

とりあえずこれでgradleプロジェクトの雛形ができた

/gradle
  /wrapper
    gradle-wrapper.jar
    gradle-wrapper.properties
build.gradle.kts
gradle.properties
gradlew
gradlew.bat
settings.gradle.kts

appモジュールを作る

ルートにappディレクトリを作る
これだとまだモジュールとして認識されないので、settings.gradle.ktsに追記する

include(":app")

これでappはモジュールとして読み込まれたが、Androidアプリケーションモジュールではなく単なるgradleモジュールとして読み込まれているだけ

Android Gradle Pluginのセットアップ

Androidアプリケーションモジュールとして読み込ませるためにはcom.android.applicationをappモジュールでapplyする必要がある

とりあえずsettings.gradle.ktsに追記していく

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

これでgoogle, mavenCentral, gradlePluginPortalのMavenリポジトリからGradle Pluginを引っ張ってこれるようになった

ので、ルートのbuild.gradle.ktsに追記

build.gradle.kts
plugins {
    id("com.android.application") version "8.9.2" apply false
}

次にこのpluginをappモジュールにも適用したいのでappの直下にbuild.gradle.ktsを作成し、以下を記載

app/build.gradle.kts
plugins {
    id("com.android.application")
}

これでappがAndroidアプリケーションモジュールとして認識されましたね
Android Studioで開けばappが緑の丸ポチがついているのがわかると思います

仕上げ

Kotlin Androidプラグインを適用します

build.gradle.kts
plugins {
    id("com.android.application") version "8.9.2" apply false
+   id("org.jetbrains.kotlin.android") version "2.1.20" apply false
}
app/build.gradle.kts
plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
}

ここでgradle syncをするとFailed to resolve: org.jetbrains.kotlin:kotlin-stdlib:2.1.20となってしまうので、setting.gradle.ktsに以下を追記します

setting.gradle.kts
dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
    }
}

あとはandroidの設定をしたら終わりです

app/build.gradle.kts
android {
    namespace = "io.github.dosukoi_android.gradle_playground"
    compileSdk = 36
    defaultConfig {
        applicationId = "io.github.dosukoi_android.gradle_playground"
        minSdk = 24
        targetSdk = 36
        versionCode = 1
        versionName = "1.0.0"
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_21
        targetCompatibility = JavaVersion.VERSION_21
    }

    kotlinOptions {
        jvmTarget = "21"
    }
}

終わりに

あとはdependenciesに好きなライブラリを突っ込んで遊んでください
Gradleは最初は非常にとっつきづらいですが、慣れてくると可愛く思えてきますね
みなさんもGradleくんと遊んでみましょう

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?