refactor app_id generate

This commit is contained in:
JarvanMo
2023-05-15 23:47:53 +08:00
parent 0176e8c3f5
commit 98790f7cfb
4 changed files with 46 additions and 23 deletions

View File

@ -3,6 +3,8 @@ import org.yaml.snakeyaml.Yaml
group 'com.jarvan.fluwx'
version '1.0-SNAPSHOT'
Map projectYaml = loadPubspec()
buildscript {
ext.kotlin_version = '1.7.10'
repositories {
@ -31,14 +33,14 @@ android {
compileSdk 31
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
main.java.srcDirs += ['src/main/kotlin',"${buildDir}/generated/main/kotlin"]
test.java.srcDirs += 'src/test/kotlin'
}
defaultConfig {
minSdkVersion 16
consumerProguardFiles 'consumer-proguard-rules.txt'
manifestPlaceholders = loadManifestPlaceholder()
manifestPlaceholders = loadManifestPlaceholder(projectYaml)
}
dependencies {
@ -64,12 +66,16 @@ android {
}
}
Map loadManifestPlaceholder() {
Map loadPubspec() {
def path = rootProject.projectDir.parent + File.separator + "pubspec.yaml"
InputStream input = new FileInputStream(new File(path))
Yaml yaml = new Yaml()
Map projectConfig = yaml.load(input)
String appId = ""
return projectConfig
}
static def loadManifestPlaceholder(Map projectConfig) {
String interruptWxRequest = "true"
String flutterActivity = ""
String debugLogging = "disabled"
@ -95,8 +101,37 @@ Map loadManifestPlaceholder() {
}
return ["WeChatAppId": appId,
"InterruptWeChatRequestByFluwx": interruptWxRequest,
"FluwxFlutterActivity": flutterActivity,
"WeChatDebugLogging": debugLogging]
return ["InterruptWeChatRequestByFluwx": interruptWxRequest,
"FluwxFlutterActivity" : flutterActivity,
"WeChatDebugLogging" : debugLogging]
}
tasks.register("generateFluwxHelperFile") {
Map config = loadPubspec()
Map fluwx = (Map) config.get("fluwx")
if (fluwx) {
String appId = (String) fluwx.get("app_id")
if (appId == null) {
appId = ""
}
File generateFolder = new File("${buildDir}/generated/main/kotlin/com/jarvan/fluwx")
String template = "package com.jarvan.fluwx\n" +
"// auto generated\n" +
"object FluwxHelper {\n" +
" val appId:String =\"&&PLACEHOLDER&&\";\n" +
"}"
if (!generateFolder.exists()) {
generateFolder.mkdirs()
}
file("${generateFolder.absolutePath}/FluwxHelper.kt").text = template.replace("&&PLACEHOLDER&&", appId)
}
}
tasks.named("build").configure {
dependsOn("generateFluwxHelperFile")
}

View File

@ -12,10 +12,6 @@
<application>
<meta-data
android:name="WeChatAppId"
android:value="${WeChatAppId}" />
<meta-data
android:name="InterruptWeChatRequestByFluwx"
android:value="${InterruptWeChatRequestByFluwx}" />

View File

@ -19,6 +19,7 @@ import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import com.jarvan.fluwx.FluwxHelper
import com.jarvan.fluwx.handlers.FluwxResponseHandler
import com.jarvan.fluwx.handlers.FluwxRequestHandler
import com.jarvan.fluwx.handlers.WXAPiHandler
@ -39,9 +40,8 @@ open class FluwxWXEntryActivity : Activity(), IWXAPIEventHandler {
try {
if (!WXAPiHandler.wxApiRegistered) {
val appInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)
val wechatAppId = appInfo.metaData.getString("WeChatAppId")
if (wechatAppId != null) {
val wechatAppId = FluwxHelper.appId
if (wechatAppId.isNotBlank()) {
WXAPiHandler.setupWxApi(wechatAppId,this)
WXAPiHandler.coolBoot = true
} else {

View File

@ -16,14 +16,6 @@
android:icon="@mipmap/ic_launcher"
android:label="fluwx_example">
<meta-data
android:name="weChatAppId"
android:value="12345678" />
<meta-data
android:name="handleWeChatRequestByFluwx"
android:value="true" />
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"