Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fix: configure Dagger Hilt for DI
Browse files Browse the repository at this point in the history
  • Loading branch information
itsaky committed Jun 14, 2024
1 parent d98aaa9 commit 874d32b
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 18 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ plugins {
alias(libs.plugins.gradle.publish) apply false
alias(libs.plugins.protobuf) apply false
alias(libs.plugins.benchmark) apply false
alias(libs.plugins.hilt) apply false
}

buildscript {
Expand Down
1 change: 1 addition & 0 deletions composite-builds/build-logic/plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
implementation(projects.buildLogic.propertiesParser)

implementation("com.android.tools.build:gradle:${libs.versions.agp.asProvider().get()}")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${libs.versions.kotlin.asProvider().get()}")
implementation(libs.maven.publish)

implementation(libs.common.jkotlin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.itsaky.androidide.plugins

import com.android.build.gradle.BaseExtension
import com.itsaky.androidide.build.config.isFDroidBuild
import com.itsaky.androidide.plugins.util.isAndroidModule
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -39,10 +40,10 @@ class AndroidIDEPlugin : Plugin<Project> {
return@run
}

val isAndroidModule = plugins.hasPlugin("com.android.application") ||
plugins.hasPlugin("com.android.library")
// Apply dependency injection for all modules
plugins.apply(DIConfigPlugin::class.java)

if (isAndroidModule && !isFDroidBuild) {
if (this.isAndroidModule && !isFDroidBuild) {
// setup signing configuration
plugins.apply(SigningConfigPlugin::class.java)
}
Expand All @@ -57,7 +58,7 @@ class AndroidIDEPlugin : Plugin<Project> {
}

val taskName = when {
isAndroidModule -> "testDebugUnitTest"
this.isAndroidModule -> "testDebugUnitTest"
else -> "test"
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/

package com.itsaky.androidide.plugins

import org.gradle.api.Plugin
import org.gradle.api.Project

/**
* Configures dependency injection in a module.
*
* @author Akash Yadav
*/
class DIConfigPlugin : Plugin<Project> {

override fun apply(target: Project) = target.run {
dependencies.add("implementation", "javax.inject:javax.inject:1")
Unit
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/

package com.itsaky.androidide.plugins.util

import org.gradle.api.Project

val Project.isAndroidModule: Boolean
get() = isAndroidApplication || isAndroidLibrary

val Project.isAndroidApplication: Boolean
get() = plugins.hasPlugin("com.android.application")

val Project.isAndroidLibrary: Boolean
get() = plugins.hasPlugin("com.android.library")
6 changes: 6 additions & 0 deletions core/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ plugins {
id("realm-android")
id("androidx.navigation.safeargs.kotlin")
id("com.itsaky.androidide.desugaring")
id("com.google.dagger.hilt.android")
}

apply {
Expand Down Expand Up @@ -59,6 +60,8 @@ kapt {
arguments {
arg("eventBusIndex", "${BuildConfig.packageName}.events.AppEventsIndex")
}

correctErrorTypes = true
}

desugaring {
Expand All @@ -77,6 +80,7 @@ dependencies {
// Annotation processors
kapt(libs.common.glide.ap)
kapt(libs.google.auto.service)
kapt(libs.hilt.compiler)
kapt(projects.annotation.processors)

implementation(libs.common.editor)
Expand All @@ -93,6 +97,8 @@ dependencies {
implementation(libs.google.gson)
implementation(libs.google.guava)

implementation(libs.hilt.android)

// Git
implementation(libs.git.jgit)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ import com.itsaky.androidide.viewmodel.EditorViewModel
import com.itsaky.androidide.xml.resources.ResourceTableRegistry
import com.itsaky.androidide.xml.versions.ApiVersionsRegistry
import com.itsaky.androidide.xml.widgets.WidgetTableRegistry
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import org.greenrobot.eventbus.Subscribe
Expand All @@ -118,6 +119,7 @@ import kotlin.math.roundToLong
* @author Akash Yadav
*/
@Suppress("MemberVisibilityCanBePrivate")
@AndroidEntryPoint
abstract class BaseEditorActivity : EdgeToEdgeIDEActivity(), TabLayout.OnTabSelectedListener,
DiagnosticClickListener {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ abstract class ProjectHandlerActivity : BaseEditorActivity() {

buildServiceConnection.onConnected = null
editorViewModel.isBoundToBuildSerice = true
Lookup.getDefault().update(BuildService.KEY_BUILD_SERVICE, service)

service.setEventListener(mBuildEventListener)

if (!service.isToolingServerStarted()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import com.itsaky.androidide.utils.VMUtils
import com.itsaky.androidide.utils.flashError
import com.termux.app.TermuxApplication
import com.termux.shared.reflection.ReflectionUtils
import dagger.hilt.android.HiltAndroidApp
import io.github.rosemoe.sora.widget.schemes.EditorColorScheme
import io.realm.Realm
import kotlinx.coroutines.DelicateCoroutinesApi
Expand All @@ -71,7 +72,7 @@ import java.lang.Thread.UncaughtExceptionHandler
import java.time.Duration
import kotlin.system.exitProcess


@HiltAndroidApp
class IDEApplication : TermuxApplication() {

private var uncaughtExceptionHandler: UncaughtExceptionHandler? = null
Expand Down
30 changes: 30 additions & 0 deletions core/app/src/main/java/com/itsaky/androidide/di/ActivityModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/

package com.itsaky.androidide.di

import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityComponent

/**
* @author Akash Yadav
*/
@Module
@InstallIn(ActivityComponent::class)
abstract class ActivityModule {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/

package com.itsaky.androidide.di

import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityComponent

/**
* @author Akash Yadav
*/
@Module
@InstallIn(ActivityComponent::class)
abstract class EditorActivityModule {
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import com.itsaky.androidide.tooling.api.models.ToolingServerMetadata
import com.itsaky.androidide.tooling.events.ProgressEvent
import com.itsaky.androidide.utils.Environment
import com.termux.shared.termux.shell.command.environment.TermuxShellEnvironment
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -78,6 +79,7 @@ import java.util.concurrent.TimeUnit
*
* @author Akash Yadav
*/
@AndroidEntryPoint
class GradleBuildService : Service(), BuildService, IToolingApiClient,
ToolingServerRunner.Observer {

Expand All @@ -100,7 +102,8 @@ class GradleBuildService : Service(), BuildService, IToolingApiClient,
private var eventListener: EventListener? = null

private val buildServiceScope = CoroutineScope(
Dispatchers.Default + CoroutineName("GradleBuildService"))
Dispatchers.Default + CoroutineName("GradleBuildService")
)

private val isGradleWrapperAvailable: Boolean
get() {
Expand All @@ -122,7 +125,6 @@ class GradleBuildService : Service(), BuildService, IToolingApiClient,
}

companion object {

private val log = LoggerFactory.getLogger(GradleBuildService::class.java)
private val NOTIFICATION_ID = R.string.app_name
private val SERVER_System_err = LoggerFactory.getLogger("ToolingApiErrorStream")
Expand All @@ -138,8 +140,10 @@ class GradleBuildService : Service(), BuildService, IToolingApiClient,
return isToolingServerStarted && server != null
}

private fun showNotification(message: String,
@Suppress("SameParameterValue") isProgress: Boolean) {
private fun showNotification(
message: String,
@Suppress("SameParameterValue") isProgress: Boolean
) {
log.info("Showing notification to user...")
createNotificationChannels()
startForeground(NOTIFICATION_ID, buildNotification(message, isProgress))
Expand All @@ -149,7 +153,8 @@ class GradleBuildService : Service(), BuildService, IToolingApiClient,
val buildNotificationChannel = NotificationChannel(
BaseApplication.NOTIFICATION_GRADLE_BUILD_SERVICE,
getString(string.title_gradle_service_notification_channel),
NotificationManager.IMPORTANCE_LOW)
NotificationManager.IMPORTANCE_LOW
)
NotificationManagerCompat.from(this)
.createNotificationChannel(buildNotificationChannel)
}
Expand Down Expand Up @@ -315,7 +320,8 @@ class GradleBuildService : Service(), BuildService, IToolingApiClient,

override fun checkGradleWrapperAvailability(): CompletableFuture<GradleWrapperCheckResult> {
return if (isGradleWrapperAvailable) CompletableFuture.completedFuture(
GradleWrapperCheckResult(true)) else installWrapper()
GradleWrapperCheckResult(true)
) else installWrapper()
}

internal fun setServerListener(listener: OnServerStartListener?) {
Expand All @@ -335,8 +341,10 @@ class GradleBuildService : Service(), BuildService, IToolingApiClient,

private fun doInstallWrapper(): GradleWrapperCheckResult {
val extracted = File(Environment.TMP_DIR, "gradle-wrapper.zip")
if (!ResourceUtils.copyFileFromAssets(ToolsManager.getCommonAsset("gradle-wrapper.zip"),
extracted.absolutePath)
if (!ResourceUtils.copyFileFromAssets(
ToolsManager.getCommonAsset("gradle-wrapper.zip"),
extracted.absolutePath
)
) {
log.error("Unable to extract gradle-plugin.zip from IDE resources.")
return GradleWrapperCheckResult(false)
Expand All @@ -358,8 +366,10 @@ class GradleBuildService : Service(), BuildService, IToolingApiClient,
}

private fun doUpdateNotification(message: String, isProgress: Boolean) {
(getSystemService(NOTIFICATION_SERVICE) as NotificationManager).notify(NOTIFICATION_ID,
buildNotification(message, isProgress))
(getSystemService(NOTIFICATION_SERVICE) as NotificationManager).notify(
NOTIFICATION_ID,
buildNotification(message, isProgress)
)
}

override fun metadata(): CompletableFuture<ToolingServerMetadata> {
Expand All @@ -368,7 +378,8 @@ class GradleBuildService : Service(), BuildService, IToolingApiClient,
}

override fun initializeProject(
params: InitializeProjectParams): CompletableFuture<InitializeResult> {
params: InitializeProjectParams
): CompletableFuture<InitializeResult> {
checkServerStarted()
Objects.requireNonNull(params)
return performBuildTasks(server!!.initialize(params))
Expand Down Expand Up @@ -481,7 +492,8 @@ class GradleBuildService : Service(), BuildService, IToolingApiClient,
}

outputReaderJob = buildServiceScope.launch(
Dispatchers.IO + CoroutineName("ToolingServerErrorReader")) {
Dispatchers.IO + CoroutineName("ToolingServerErrorReader")
) {
val reader = input.bufferedReader()
try {
reader.forEachLine { line ->
Expand Down
9 changes: 9 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ logback = "1.5.3"
realm = "10.18.0"
benchmark = "1.2.4"
benchmark-junit4 = "1.2.4"
hilt = "2.51.1"

[libraries]

Expand Down Expand Up @@ -75,6 +76,13 @@ common-hiddenApiBypass = { module = "org.lsposed.hiddenapibypass:hiddenapibypass
common-termuxAmLib = { module = "com.termux:termux-am-library", version = "v2.0.0" }
common-charts = { module = "com.github.AppDevNext:AndroidChart", version = "3.1.0.21" }

# Dependency Injection
## DO NOT REMOVE
## Accessed programmatically in DIConfigPlugin
## Update DIConfigPlugin if renamed
hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hilt" }
hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "hilt" }

# AndroidX
androidx-annotation = { module = "androidx.annotation:annotation", version = "1.7.1" }
androidx-appcompat = { module = "androidx.appcompat:appcompat", version = "1.6.1" }
Expand Down Expand Up @@ -179,3 +187,4 @@ maven-publish = { id = "com.vanniktech.maven.publish.base", version.ref = "maven
gradle-publish = { id = "com.gradle.plugin-publish", version = "1.2.1" }
protobuf = { id = "com.google.protobuf", version = "0.9.4" }
benchmark = { id = "androidx.benchmark", version.ref = "benchmark" }
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }

0 comments on commit 874d32b

Please sign in to comment.