我目前正在尝试在 Kotlin 中为 Android 编写一个注释处理器。项目结构如下:
/annotation
/src/main/kotlin/<package>
Annotation.kt
AnnotationProcessor.kt
/sample
Run Code Online (Sandbox Code Playgroud)
项目/build.gradle
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.31"
}
}
Run Code Online (Sandbox Code Playgroud)
注释/build.gradle
apply plugin: 'kotlin'
sourceCompatibility = 1.7
targetCompatibility = 1.7
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion"
}
Run Code Online (Sandbox Code Playgroud)
示例/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion"
implementation project(':annotation')
kapt project(':annotation')
}
Run Code Online (Sandbox Code Playgroud)
注释.kt
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.SOURCE)
annotation class Annotation(val comment: String = "")
Run Code Online (Sandbox Code Playgroud)
注释处理器.kt
class AnnotationProcessor : AbstractProcessor() {
override …Run Code Online (Sandbox Code Playgroud) 在 Android Studio 3.4.1 中从 Kotlin 1.3.21 更新到 1.3.30+ 时,我们收到以下构建错误:
e: java.lang.IllegalStateException: failed to analyze: java.lang.AssertionError: annotation tree hasn't been attributed yet: @kotlin.Metadata(mv = {1, 1, 15}, bv = {1, 0, 3}, k = 1, d1 = {"\u0000$\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\b&\u0018\u0000*\b\b\u0000\u0010\u0001*\u00020\u00022\b\u0012\u0004\u0012\u0002H\u00010\u0003B\u001f\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\b\b\u0001\u0010\u0006\u001a\u00020\u0007\u0012\u0006\u0010\b\u001a\u00020\t\u00a2\u0006\u0002\u0010\n\u00a8\u0006\u000b"}, d2 = {"Lde/thermomess/field/ui/viewholder/BaseViewHolder;", "T", "", "Lcom/xroot/android/ui/recyclerview/RecyclerViewHolder;", "context", "Landroid/content/Context;", "layoutResId", "", "parent", "Landroid/view/ViewGroup;", "(Landroid/content/Context;ILandroid/view/ViewGroup;)V", "app_betaDebug"})
Run Code Online (Sandbox Code Playgroud)
@kotlin.Metadata 背后的所有内容都不同,但在此之前它保持不变。对于 Kotlin 1.3.21 及更早版本,一切工作正常。
我们使用 @kotlin.Metadata 来检查一个类是否是 Gson TypeAdapterFactory 的 Kotlin 类
private val Class<*>.isKotlinClass: Boolean
get() = declaredAnnotations.find { it.annotationClass.java.name == "kotlin.Metadata" } != null
Run Code Online (Sandbox Code Playgroud)
以及通过混淆规则跳过自定义注释 …
我们目前正在通过新的 Splashscreen API 实现启动屏幕。我们正在遵循迁移指南。背景颜色设置正确(通过windowSplashScreenBackground),但图标在模拟器和物理设备上均不可见。
构建.gradle
android {
compileSdkVersion 31
...
}
dependencies {
implementation "androidx.core:core-splashscreen:1.0.0-alpha01"
...
}
Run Code Online (Sandbox Code Playgroud)
值-v31/themes.xml
<style name="AppTheme" parent="Theme.SplashScreen">
<item name="postSplashScreenTheme">@style/AppThemeCompat</item>
<item name="windowSplashScreenBackground">@android:color/black</item>
<item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item>
<item name="windowSplashScreenAnimationDuration">200</item>
</style>
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
...
</application>
Run Code Online (Sandbox Code Playgroud)
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen()
...
}
Run Code Online (Sandbox Code Playgroud)