ReactNativeの息抜きにkotlinの勉強始めました。遠藤です。
今回はkotlinの環境構築について共有していきます。
個人的にはAndroidStudioより、IntelliJ IDEAをプッシュしていきたいので、今回はIntelliJ IDEAで環境を整えていきます。 前提としてmacの環境構築としてますが、基本的にwindowsやLinuxでも同じはず。
IntelliJの導入
以下公式URLから対象OSのファイルをダウンロード https://www.jetbrains.com/idea/download/#section=mac
無事ダウンロードできたらIntelliJ IDEAを起動。
起動するとポリシーが表示されるので、何も考えずContinueします。

続いてこの画面になるのでDont send

この画面はstart trialしてcontinue。 もしログインする必要があればアカウント作成してログインしてください。

最後にこの画面が表示されたらOK

それではNew ProjectしてJDKのインストールをしていきます!
JDKのインストール
Download JDKで最新のJDKをダウンロード

続いてアンドロイドSDKインストール

これらのインストールがおわれば環境構築完了!
プロジェクト作成してビルドしてみる


これでプロジェクトの作成完了です!
あとは各々思いのまま実装してください。
もしビルド時にbuild tool versionでエラーが出た場合
Installed Build Tools revision 31.0.0 is corrupted. Remove and install again using the SDK Manager.
Android12の最新のSDKのBuild Toolを指定しているとエラーが発生するみたいなので、 少し古いバージョンのBuild Tookを指定することで回避することができます。
app/build.gradle
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 31 // 変更
buildToolsVersion "30.0.3" // 変更
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
上記のようにcompileSdkVersionとbuildToolsVersionを変更することでbuild可能になります。
ただバージョンを下げて対応するのはできないって場合もあるかと思います。その場合は以下のコマンドで対応可能です。
cd ~/Library/Android/sdk/build-tools/31.0.0 \
&& mv d8 dx \
&& cd lib \
&& mv d8.jar dx.jar
このコマンドを打つことで、ビルドが成功するようになります。
ちなみに31.0.0以降のビルドをする場合は、AndroidManifest.xmlに
android:exported=”true”かandroid:exported=”false”を設定しないと新たにエラーが発生するので注意してください
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication2">
<activity
android:exported="true" <=これを追加
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
これで無事31.0.0でもビルドができました!
環境構築おわり。