wordpress-mobile / AztecEditor-Android

A reusable native Android rich text editor component.
Mozilla Public License 2.0
673 stars 114 forks source link

Unresolved reference: lazy. How to properly build: AztecEditor-Android? #1052

Open mx1234567 opened 9 months ago

mx1234567 commented 9 months ago

How to properly build AztecEditor-Android, so that visual errors are not displayed in Android Studio, in the files: \app\src\main\kotlin\org\wordpress\aztec\demo\MainActivity.kt And \build.gradle

For example Unresolved reference: lazy, see below.

The project, despite these errors, can be launched and works. I would like to remove the display of these errors from Android Studio.

I would like to receive recommendations on what preliminary steps should be taken in order to correctly assemble this project? Perhaps I'm doing something wrong?

Android Studio Version Here are the details of my version of Android Studio:

Android Studio Flamingo | 2022.2.1 Patch 2
Build #AI-222.4459.24.2221.10121639, built on May 12, 2023
Runtime version: 17.0.6+0-b2043.56-9586694 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 8
Registry:
     external.system.auto.import.disabled=true
     ide.text.editor.with.preview.show.floating.toolbar=false
     ide.instant.shutdown=false
     gradle.version.catalogs.dynamic.support=true

Non Bundled Plugins:
     com.intellij.marketplace (222.4459.28)
     izhangzhihao.rainbow.brackets(2023.3.1-ij)

My steps

I downloaded the archive with the project from: wordpress-mobile/AztecEditor-Android: A reusable native Android rich text editor component. (github.com)

Unzipped.

Changed to directory: cd c:\Projects\AztecEditor-Android-trunk-original-1

Ran the command: gradle build

The builder returned Build failed: FAILURE: Build failed with an exception.

*What went wrong: Could not determine the dependencies of task ':app:compileReleaseJavaWithJavac'.

SDK location not found. Define a valid SDK location with an ANDROID_HOME environment variable or by setting the sdk.dir path in your project's local properties file at 'C:\Projects\AztecEditor-Android-trunk-original-1\local.properties'.

BUILD FAILED in 32s

Setting the path to the SDK Looked in Android Studio, where is the path to the SDK: C:\Users\user1\AppData\Local\Android\Sdk

File: local.properties

Created a file: local.properties, in the root directory of the project, with the following line in it:

sdk.dir=C:/Users/user1/AppData/Local/Android/Sdk

After that the assembly of the project passed without errors. The collector issued at the end:

BUILD SUCCESSFUL in 8m 27s 626 actionable tasks: 626 executed

6 Problems in Android Studio Opened file in Android Studio: \app\src\main\kotlin\org\wordpress\aztec\demo\MainActivity.kt

On the tab: Problems, there are 6 Errors.

Here are some of them:

error: Unresolved reference: lazy

For a line of code: private val isRunningTest: Boolean by lazy {


error: Unresolved reference: indices

For a line of code: for (i in grantResults.indices) {

error: Unresolved reference: isNotEmpty

For a line of code: val mediaPending = aztec.visualEditor.getAllElementAttributes(uploadingPredicate).isNotEmpty()


error: Unresolved reference: let

For a line of code: url?.let {

Changes in Project Structure ktlint In Project Structure, on the Suggestions tab updated ktlint by clicking the button: Update.

:app» com.github.shyiko:ktlint:0.9.2 Newer version available: 0.31.0

It did not help, the errors remained.

androidx.appcompat:appcompat Next in the Project Structure (Ctrl+Alt+Shift+S), on the Suggestions tab, according to the instructions on the page: https://stackoverflow.com/questions/59814070/android-studio-unresolved-reference-for-lazy-kotlin

updated androidx.appcompat:appcompat by clicking the button: Update.

androidx.appcompat:appcompat:1.0.0 Newer version available: 1.6.1

This helped to eliminate the display of these errors in Android Studio, but a new one appeared: error: Overriding method should call super.onSaveInstanceState

For lines of code:

override fun onSaveInstanceState(outState: Bundle) {
     super.onSaveInstanceState(outState)

I note that the function call that this error asks to add is already present below: super.onSaveInstanceState(outState)

Looks like some kind of linter glitch.

File: build.gradle There was a strange error in the build.gradle file: Cannot resolve symbol 'KotlinCompile'

to the line: import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

I fixed it according to this “use class full path instead” recommendation: kotlin - Gradle cannot resolve symbol while it does - Stack Overflow https://stackoverflow.com/questions/55705585/gradle-cannot-resolve-symbol-while-it-does

Initially it was like this:

tasks.withType(KotlinCompile).all {
     kotlinOptions {
         allWarningsAsErrors = true
     }
}

I did the following steps.

Removed the line: import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

Set: allWarningsAsErrors = false

Specified full path in: tasks.withType(KotlinCompile) by setting: tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).

As a result, it turned out:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
     kotlinOptions {
         allWarningsAsErrors = false
     }
}

allWarningsAsErrors = false, I set it to disable warnings preventing the project from building when I changed the JDK to version 17, see below.

My questions Does anyone know what this glitch with KotlinCompile is? Is this a linter bug in Android Studio? Is there any other way to fix it?

Changing the JDK Version

I also tried to change in Settings (Ctrl+Alt+S), on the Gradle tab, the JDK version, in the field: Gradle JDK. I changed the version of JDK 11 that I had set there to JDK 17, but that didn't help either.

Full path to settings: file | settings | Build, Execution, Deployment | BuildTools | gradle

New warnings just appeared, preventing the project from building:

Task :app:compileDebugKotlin FAILED e: warnings found and -Werror specified w: C:\Projects\AztecEditor-Android-trunk-original-1\app\src\main\kotlin\org\wordpress\aztec\demo\MainActivity.kt: (314, 15): 'onActivityResult(Int, Int, Intent?): Unit' is deprecated. Overrides deprecated member in 'androidx.activity.ComponentActivity'. Deprecated in Java w: C:\Projects\AztecEditor-Android-trunk-original-1\app\src\main\kotlin\org\wordpress\aztec\demo\MainActivity.kt: (683, 17): 'startActivityForResult(Intent, Int): Unit' is deprecated. Deprecated in Java …

Removed those warnings to build the project as described above by setting: allWarningsAsErrors = false.

branch upgrade-gradle-to-8.1.1-and-agp-to-8.0.2 I also tested it on the upgrade-gradle-to-8.1.1-and-agp-to-8.0.2 branch, but it didn't help either, the errors remained the same: git checkout -b upgrade-gradle-to-8.1.1-and-agp-to-8.0.2 origin/build/upgrade-gradle-to-8.1.1-and-agp-to-8.0.2

mx1234567 commented 9 months ago

Regarding compileKotlin, I also found the following information:

https://stackoverflow.com/questions/55456176/unresolved-reference-compilekotlin-in-build-gradle-kts https://github.com/gradle/kotlin-dsl-samples/issues/1368 https://youtrack.jetbrains.com/issue/KT-42241 https://youtrack.jetbrains.com/issue/KT-31077

Perhaps you need to change the code in the project repository in accordance with these recommendations?