'Compile the Kotlin android file through the command line compiler, show unresolved reference: synthetic
I used kotlin-android-extensions the kotlin code is simple
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
contentTV.text="new"
Toast.makeText(this,"夸父注入成功22",Toast.LENGTH_SHORT).show()
println("hello world1234")
}
}
I use the command:
kotlinc -classpath /Users/skateboard/AndroidStudioProjects/KuaFuTest/app/build/intermediates/javac/demoDebug/compileDemoDebugJavaWithJavac/classes:/Users/skateboard/AndroidStudioProjects/KuaFuTest/app/build/generated/source/apt/demo/debug:/Users/skateboard/AndroidStudioProjects/KuaFuTest/app/build/tmp/kotlin-classes/demoDebug:/Users/skateboard/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-android-extensions-runtime/1.4.0/98be06f8dfc5750d45b887d2b3330ef49390aeb7/kotlin-android-extensions-runtime-1.4.0.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/3e4b08cac9c41e8795af42a5b815b321/jars/classes.jar:/Users/skateboard/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.4.0/63e75298e93d4ae0b299bb869cf0c627196f8843/kotlin-stdlib-1.4.0.jar:/Users/skateboard/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.4.0/1c752cce0ead8d504ccc88a4fed6471fd83ab0dd/kotlin-stdlib-common-1.4.0.jar:/Users/skateboard/.gradle/caches/modules-2/files-2.1/org.jetbrains/annotations/13.0/919f0dfe192fb4e063e7dacadee7f8bb9a2672a9/annotations-13.0.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/8c61b603f350980f81417f4d3f853902/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/fe7696ac9a68e8ce0f0e0cc1b174b4c9/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/a0713d6b50505595f83734d3cfae9bed/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/925f9893c711f0c25548d940c9b7f168/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/b28b597b828d5b0aeacc49c107fd71db/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/10037c6171d23644c0b6060e1867b5a7/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/72f6b9e34a5eeae66c71ca9f86b4a6c3/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/9b40b231da712362bd8f03e85be0e8dd/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/4c94092ade43ee5bb6dd2adca8dd738d/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/ffa9732c899005350cbfd5e94b2e49f1/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/cfcfabbe3e4822088f431961ddb26265/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/f711abbaf1b6d3d1831f1a052db77b4f/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/d0fabd256c1613080926726dda0223e8/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/64ca1f0e2236ba245c5e08248d5aa195/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/907fc8f19fa97ba58e92a498b93f0e57/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/50ffe37981838fbb4bccc1654a350512/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/270ae6c5b28e9db49e35b2699e204e59/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/35a80db54afda52720cd558c2d512dac/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/6b3778bff2390065292489fa15d05f88/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/c5dc31390ef26854c632d1c31875ad52/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/5d6910a19b706ec657a4b8c64d20b5d0/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/267071f0ebeb7460627cedc63598d9f2/jars/classes.jar:/Users/skateboard/.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife-annotations/10.2.3/45f0790cb0130b877d1269fc56e1a294b90dabc1/butterknife-annotations-10.2.3.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/809910e5428f0e79fda45e7c15454285/jars/classes.jar:/Users/skateboard/.gradle/caches/modules-2/files-2.1/androidx.collection/collection/1.1.0/1f27220b47669781457de0d600849a5de0e89909/collection-1.1.0.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/24750ac89d4631677434276211cfdded/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/4346a3a0a738fa8458ff093199b5caa7/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/b60a76ab931c63b09190246a47776e74/jars/classes.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/d7e19ab7adc2577331058703432889ca/jars/classes.jar:/Users/skateboard/.gradle/caches/modules-2/files-2.1/androidx.lifecycle/lifecycle-common/2.1.0/c67e7807d9cd6c329b9d0218b2ec4e505dd340b7/lifecycle-common-2.1.0.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/d032e6d96fe3ad1b9e9c73f1efdd0309/jars/classes.jar:/Users/skateboard/.gradle/caches/modules-2/files-2.1/androidx.arch.core/core-common/2.1.0/b3152fc64428c9354344bd89848ecddc09b6f07e/core-common-2.1.0.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/492fba0096cb18fd2acb20d364f7892a/jars/classes.jar:/Users/skateboard/.gradle/caches/modules-2/files-2.1/androidx.annotation/annotation/1.1.0/e3a6fb2f40e3a3842e6b7472628ba4ce416ea4c8/annotation-1.1.0.jar:/Users/skateboard/.gradle/caches/transforms-2/files-2.1/00650ab3c966493d1ba1f2b3849514c1/jars/classes.jar:/Users/skateboard/.gradle/caches/modules-2/files-2.1/androidx.constraintlayout/constraintlayout-solver/2.0.4/1f001d7db280a89a6c26b26a66eb064bb6d5efeb/constraintlayout-solver-2.0.4.jar:/Users/skateboard/AndroidStudioProjects/KuaFuTest/mylibrary/build/intermediates/compile_library_classes/debug/classes.jar:/Users/skateboard/AndroidStudioProjects/KuaFuTest/kuafuapi/build/intermediates/compile_library_classes/debug/classes.jar:/Users/skateboard/AndroidStudioProjects/KuaFuTest/app/build/tmp/kapt3/classes/demoDebug:/Users/skateboard/Library/Android/sdk/platforms/android-30/android.jar -d /Users/skateboard/AndroidStudioProjects/KuaFuTest/app/build/kuafu/classes -Xplugin=/Users/skateboard/.sdkman/candidates/kotlin/current/lib/android-extensions-compiler.jar -P plugin:org.jetbrains.kotlin.android:package=com.skateboard.kuafutest /Users/skateboard/AndroidStudioProjects/KuaFuTest/app/src/main/java/com/skateboard/kuafutest/MainActivity.kt
It showed compile error:
app/src/main/java/com/skateboard/kuafutest/MainActivity.kt:7:24: error: unresolved reference: synthetic
import kotlinx.android.synthetic.main.activity_main.*
^
app/src/main/java/com/skateboard/kuafutest/MainActivity.kt:15:9: error: unresolved reference: contentTV
contentTV.text="new"
^
app/src/main/java/com/skateboard/kuafutest/MainActivity.kt:15:19: error: variable expected
contentTV.text="new"
^
wujiaxin:KuaFuTest skateboard$
Solution 1:[1]
Finally, I found the answer by decompiling the gradle compiler. When you want to compile and support kotlin-android-extensions on the command line, you need to add the parameter -Xplugin=lib/android-extensions-compiler.jar -P plugin:org.jetbrains.kotlin .android:package=${package_name} -P plugin:org.jetbrains.kotlin.android:variant='${flovar};${resource_package}'
Xplugin specifies the location of the jar package of the kotlin compiler plugin, flavor specifies the build variant such as main, and resource_package specifies the resource path such as /Users/dd/projectName/app/src/main/res
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | skateboard1991 |