Skip to content

Commit

Permalink
Feature/agp7.4 (#17)
Browse files Browse the repository at this point in the history
* MOD: compatible with agp7.4

* FIX: DefaultLifecycleObserver compatibility problems

* ADD: Build apk check

* ADD: agp test case

* MOD: add generated code to srcDir logic

Co-authored-by: jiangjunxiang <[email protected]>
  • Loading branch information
RicardoJiang and jiangjunxiang authored Jan 17, 2023
1 parent 172972d commit 2ae282c
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 33 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@ jobs:

- name: Run local tests
run: ./gradlew :kace-compiler:test

- name: Publish To Local
run: ./gradlew publishToMavenLocal -PVERSION_NAME=0.0.0-SNAPSHOT

- name: Set up JDK 11
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 11

- name: Test AGP compatibility
run: chmod +x ./test_agp.sh && ./test_agp.sh
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ kotlin.code.style=official
android.nonTransitiveRClass=true

KOTLIN_PLUGIN_ID=com.kanyun.kace
VERSION_NAME=1.0.2
VERSION_NAME=1.7.0-1.0.3-SNAPSHOT

GROUP=com.kanyun.kace

Expand Down
1 change: 1 addition & 0 deletions kace-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
implementation(kotlin("stdlib-jdk8"))

compileOnly("com.android.tools.build:gradle:4.2.0")
compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0")
implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import com.android.build.gradle.api.BaseVariant
import com.kanyun.kace.BuildConfig
import com.kanyun.kace.gradle.utils.addCustomVariantLayoutDir
import com.kanyun.kace.gradle.utils.addSourceSetLayoutDir
import com.kanyun.kace.gradle.utils.configSourceSetDir
import com.kanyun.kace.gradle.utils.getApplicationPackage
import com.kanyun.kace.gradle.utils.withAllPlugins
import java.io.File
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

class KaceGradlePlugin : Plugin<Project> {
override fun apply(target: Project) {
Expand Down Expand Up @@ -57,9 +57,9 @@ class KaceGradlePlugin : Plugin<Project> {
val compileKotlin = target.tasks.getByName("compile${variantCapitalizeName}Kotlin")
val sourceOutputDir =
File(target.buildDir, "generated/source/kace/${variant.dirName}")
val sourceSet = extension.sourceSets.getByName(variant.name)
configSourceSetDir(sourceSet, sourceOutputDir, target.logger)

val relativePath =
target.projectDir.toPath().relativize(sourceOutputDir.toPath()).toString()
(compileKotlin as KotlinCompile).source(relativePath)
val task = target.tasks.register(
"generate${variantCapitalizeName}KaceCode", KaceGenerateTask::class.java
) { task ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@

package com.kanyun.kace.gradle.utils

import com.android.build.api.dsl.AndroidSourceDirectorySet
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.api.AndroidSourceSet
import com.kanyun.kace.gradle.LayoutDir
import java.io.File
import java.lang.reflect.Field
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.logging.LogLevel
import org.gradle.api.logging.Logger

internal fun Project.withAllPlugins(vararg pluginIds: String, action: (List<Plugin<*>>) -> Unit) {
return withAllPlugins(pluginIds.toList(), action)
Expand Down Expand Up @@ -118,18 +114,3 @@ private fun getField(clazz: Class<*>, name: String): Field {
}
}
}

internal fun configSourceSetDir(
sourceSet: AndroidSourceSet,
sourceOutputDir: File,
logger: Logger
) {
try {
val kotlinSourceSet = getFieldValue(sourceSet, "kotlin") as? AndroidSourceDirectorySet
kotlinSourceSet?.srcDir(sourceOutputDir)
return
} catch (e: Exception) {
logger.log(LogLevel.INFO, "sourceSet has no kotlin field")
}
sourceSet.java.srcDir(sourceOutputDir)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package com.kanyun.kace
import android.app.Activity
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner

sealed interface AndroidExtensionsComponent {
Expand All @@ -44,7 +43,7 @@ class AndroidExtensionsActivity(

init {
if (activity is LifecycleOwner) {
activity.lifecycle.addObserver(object : DefaultLifecycleObserver {
activity.lifecycle.addObserver(object : KaceLifecycleObserver() {
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
onDestroy()
Expand All @@ -65,7 +64,7 @@ class AndroidExtensionsFragment(

init {
fragment.viewLifecycleOwnerLiveData.observe(fragment) {
it?.lifecycle?.addObserver(object : DefaultLifecycleObserver {
it?.lifecycle?.addObserver(object : KaceLifecycleObserver() {
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
onDestroy()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2022 KanYun
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.kanyun.kace

import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner

internal open class KaceLifecycleObserver : DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {}

override fun onStart(owner: LifecycleOwner) {}

override fun onResume(owner: LifecycleOwner) {}

override fun onPause(owner: LifecycleOwner) {}

override fun onStop(owner: LifecycleOwner) {}

override fun onDestroy(owner: LifecycleOwner) {}
}
10 changes: 6 additions & 4 deletions kace-sample/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id("com.android.application") version "4.2.0" apply false
id("com.android.library") version "4.2.0" apply false
id("com.android.application") apply false
id("com.android.library") apply false
id("org.jetbrains.kotlin.android") version "1.7.10" apply false
id("com.kanyun.kace") apply false
id("com.kanyun.kace") version "0.0.0-SNAPSHOT" apply false
}

tasks.register<Delete>(name = "clean") {
Expand All @@ -13,8 +13,10 @@ tasks.register<Delete>(name = "clean") {

allprojects {
repositories {
if (extra["testAgp"] == "true") {
mavenLocal()
}
google()
mavenCentral()
mavenLocal()
}
}
3 changes: 3 additions & 0 deletions kace-sample/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ vgoTargetSdkVersion = 26

kotlin_version = 1.7.0
kotlin_coroutine_version = 1.6.3

testAgp=false
agpVersion=4.2.0
21 changes: 19 additions & 2 deletions kace-sample/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
pluginManagement {
val testAgp: String by extra
val agpVersion: String by extra

repositories {
if (testAgp == "true") {
mavenLocal()
}
mavenCentral()
gradlePluginPortal()
google()
}

resolutionStrategy {
eachPlugin {
val requestedId = requested.id.id
if (requestedId == "com.android.application" || requestedId == "com.android.library") {
useVersion(agpVersion)
}
}
}
}


include(":sample-lib")
include(":app")

rootProject.name = "kace-sample"

includeBuild("../../Kace")
val testAgp: String by extra
if (testAgp != "true") {
includeBuild("../../Kace")
}
29 changes: 29 additions & 0 deletions test_agp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function setGradleVersion() {
local distributionBase=https\://services.gradle.org/distributions/

local GradleVersion=$1
echo ./gradlew -PSetGradleVersion=true wrapper --gradle-distribution-url ${distributionBase}gradle-$GradleVersion-bin.zip
./gradlew -PSetGradleVersion=true wrapper --gradle-distribution-url ${distributionBase}gradle-$GradleVersion-bin.zip
}

function testUnderAGPVersion() {
local TestAGPVersion=$1
./gradlew clean

echo ./gradlew :app:assembleMinApi21DemoDebug -PtestAgp=true -PagpVersion=$TestAGPVersion
./gradlew :app:assembleMinApi21DemoDebug -PtestAgp=true -PagpVersion=$TestAGPVersion
}

cd kace-sample

setGradleVersion 6.7.1
testUnderAGPVersion 4.2.0

setGradleVersion 7.3.3
testUnderAGPVersion 7.2.0

setGradleVersion 7.4
testUnderAGPVersion 7.3.0

setGradleVersion 7.5
testUnderAGPVersion 7.4.0

0 comments on commit 2ae282c

Please sign in to comment.