Skip to content

Commit

Permalink
修复打包BUG,打包编辑框自动填充BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
wilinz committed Aug 15, 2022
1 parent bb3ffdf commit 8031a8e
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 263 deletions.
46 changes: 30 additions & 16 deletions app/src/main/java/org/autojs/autojs/build/ApkBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,35 +106,32 @@ class ApkBuilder(
if (PFiles.isDir(path)) {
copyDir(path, "assets/project/")
} else {
replaceFile(oldFile = File(path), "assets/project/main.js")
if (projectConfig!!.mainScript != "main.js") {
File(workspacePath, "assets/project/main.js").renameTo(
File(
workspacePath,
"assets/project/${projectConfig!!.mainScript}"
)
)
}
replaceFile(oldFile = File(path), "assets/project/${projectConfig!!.mainScript}")
}
return this
}

fun copyDir(srcPath: String, relativeTargetPath: String) {
fun copyDir(
srcPath: String,
relativeTargetPath: String,
ignoredPath: List<String> = projectConfig!!.ignoredDirs,
ignoredName: List<String> = emptyList()
) {
val fromDir = File(srcPath)
val toDir = File(workspacePath, relativeTargetPath)
toDir.mkdirs()
val children = fromDir.listFiles() ?: return
for (child in children) {
val ignored = ignoredPath.contains(child.path) || ignoredName.contains(child.name)
if (ignored) continue
if (child.isFile) {
if (child.name.endsWith(".js")) {
encryptToDir(child, toDir)
} else {
FileInputStream(child).copyToAndClose(FileOutputStream(File(toDir, child.name)))
child.copyTo(File(toDir, child.name), true)
}
} else {
if (!projectConfig!!.ignoredDirs.contains(child)) {
copyDir(child.path, PFiles.join(relativeTargetPath, child.name + "/"))
}
copyDir(child.path, PFiles.join(relativeTargetPath, child.name + "/"))
}
}
}
Expand Down Expand Up @@ -184,7 +181,10 @@ class ApkBuilder(

private fun copyAssets(config: ProjectConfig) {
config.assets.forEach {
val form = it.form
var form = it.form
if (!form.matches(Regex("^.*?://.*|^/.*")) || form.startsWith("./")) {
form = File(projectConfig!!.projectDirectory!!, form).path
}
val relativeTo = File("assets", it.to)
val to = File(workspacePath, relativeTo.path)

Expand All @@ -194,6 +194,19 @@ class ApkBuilder(
return@forEach
}

if (it.to == "/${Constant.Assets.PROJECT}") {
val file = File(form)
copyDir(
srcPath = file.path,
relativeTargetPath = relativeTo.path,
ignoredPath = listOf(
File(projectConfig!!.projectDirectory, ProjectConfig.CONFIG_FILE_NAME).path,
projectConfig!!.sourcePath!!
)
)
return@forEach
}

val file = File(form)
if (file.isFile) {
file.copyTo(to, overwrite = true)
Expand All @@ -203,6 +216,7 @@ class ApkBuilder(
relativeTargetPath = relativeTo.path,
)
}

}
}

Expand Down Expand Up @@ -247,7 +261,7 @@ class ApkBuilder(
_progressState.emit(BuildState.BUILD)
manifestEditor?.commit()
manifestEditor?.writeTo(FileOutputStream(manifestFile))
val dir = File(projectConfig!!.sourcePath!!).parentFile
val dir = projectConfig!!.projectDirectory
buildArsc(
onReplaceIcon = { key, path ->
val icon: String? = when (key) {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/org/autojs/autojs/ui/build/BuildPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ private fun RunConfigCard(model: BuildViewModel) {
Text(text = stringResource(id = R.string.text_splash_icon))

val modifier = Modifier
.padding(8.dp)
.size(64.dp)
.clickable {
selectIconLauncher.launch(null)
Expand Down Expand Up @@ -441,6 +442,7 @@ private fun ConfigCard(model: BuildViewModel) {
Text(text = stringResource(id = R.string.text_icon))
Box {
val modifier = Modifier
.padding(8.dp)
.size(64.dp)
.align(Alignment.Center)
.clickable {
Expand Down
85 changes: 48 additions & 37 deletions app/src/main/java/org/autojs/autojs/ui/build/BuildViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.autojs.autojs.build.ApkSigner
import org.autojs.autojs.model.explorer.ExplorerFileItem
import org.autojs.autojs.model.explorer.Explorers
import org.autojs.autojs.model.script.ScriptFile
import org.autojs.autojs.tool.getRandomString
import org.autojs.autojs.tool.parseUriOrNull
import org.autojs.autojs.tool.saveIcon
import java.io.File
Expand Down Expand Up @@ -244,6 +245,7 @@ class BuildViewModel(private val app: Application, private var source: String) :
val viewModel = this
projectConfig.apply {
sourcePath = viewModel.sourcePath
projectDirectory = directory!!
outputPath = viewModel.outputPath
displaySplash = viewModel.displaySplash
assets = getAssets()
Expand Down Expand Up @@ -272,6 +274,39 @@ class BuildViewModel(private val app: Application, private var source: String) :
}
}

fun syncViewModelByConfig(projectConfig: ProjectConfig) {

projectConfig.sourcePath?.takeIf { it.isNotBlank() }?.let { sourcePath = it }
projectConfig.outputPath?.takeIf { it.isNotBlank() }?.let { outputPath = it }
projectConfig.name?.takeIf { it.isNotBlank() }?.let { appName = it }
projectConfig.packageName?.takeIf { it.isNotBlank() }?.let { packageName = it }
versionName = projectConfig.versionName
versionCode = projectConfig.versionCode.toString()
icon = projectConfig.icon?.let {
getUri(it)
}
mainScriptFile = projectConfig.mainScript ?: getMainScriptName()
isStableMode = projectConfig.launchConfig.isStableMode
isHideLauncher = projectConfig.launchConfig.isHideLauncher
isHideLogs = projectConfig.launchConfig.isHideLogs
isVolumeUpControl = projectConfig.launchConfig.isVolumeUpControl
splashText = projectConfig.launchConfig.splashText
serviceDesc = projectConfig.launchConfig.serviceDesc
splashIcon = projectConfig.launchConfig.splashIcon?.let {
getUri(it)
}

val signConfig = projectConfig.signingConfig
if (!signConfig.keyStore.isNullOrEmpty()) {
appSignKeyPath = signConfig.keyStore
keyStore = ApkSigner.loadApkKeyStore(signConfig.keyStore)
}
setPermissions(projectConfig)
setLibs(projectConfig)
setAssets(projectConfig)
setAbis(projectConfig)
}

private fun Uri.toRelativePathOrString(): String {
val uriString = this.toString()
if (uriString.startsWith("file://")) {
Expand Down Expand Up @@ -303,6 +338,14 @@ class BuildViewModel(private val app: Application, private var source: String) :
)
)
}
if (!isSingleFile) {
assetsList.add(
Asset(
form = directory!!,
to = "/${Constant.Assets.PROJECT}"
)
)
}
return assetsList
}

Expand All @@ -327,38 +370,6 @@ class BuildViewModel(private val app: Application, private var source: String) :
return permissionList
}

fun syncViewModelByConfig(projectConfig: ProjectConfig) {

projectConfig.sourcePath?.let { sourcePath = it }
projectConfig.outputPath?.let { outputPath = it }
projectConfig.name?.let { appName = it }
projectConfig.packageName?.let { packageName = it }
versionName = projectConfig.versionName
versionCode = projectConfig.versionCode.toString()
icon = projectConfig.icon?.let {
getUri(it)
}
mainScriptFile = projectConfig.mainScript ?: getMainScriptName()
isStableMode = projectConfig.launchConfig.isStableMode
isHideLauncher = projectConfig.launchConfig.isHideLauncher
isHideLogs = projectConfig.launchConfig.isHideLogs
isVolumeUpControl = projectConfig.launchConfig.isVolumeUpControl
splashText = projectConfig.launchConfig.splashText
serviceDesc = projectConfig.launchConfig.serviceDesc
splashIcon = projectConfig.launchConfig.splashIcon?.let {
getUri(it)
}

val signConfig = projectConfig.signingConfig
if (!signConfig.keyStore.isNullOrEmpty()) {
appSignKeyPath = signConfig.keyStore
keyStore = ApkSigner.loadApkKeyStore(signConfig.keyStore)
}
setPermissions(projectConfig)
setLibs(projectConfig)
setAssets(projectConfig)
setAbis(projectConfig)
}

private fun getMainScriptName(): String {
return if (source.endsWith(".js")) {
Expand Down Expand Up @@ -459,7 +470,7 @@ class BuildViewModel(private val app: Application, private var source: String) :
if (!checkPackageNameValid(packageName)) {
packageName = app.getString(
R.string.format_default_package_name,
"test"
getRandomString(6)
)
}
setSource(file)
Expand All @@ -481,6 +492,9 @@ class BuildViewModel(private val app: Application, private var source: String) :
oldProjectConfig?.let {
isOldProjectConfigExist = true
projectConfig = it.copy()
it.mainScript?.let { mainScript ->
sourcePath = File(directory!!, mainScript).path
}
syncViewModelByConfig(projectConfig)
} ?: kotlin.run {
outputPath = if (!isSingleFile) File(source, projectConfig.buildDir).path
Expand All @@ -498,7 +512,7 @@ class BuildViewModel(private val app: Application, private var source: String) :
String.format("%s_v%s.apk", projectConfig.name, projectConfig.versionName)
)
withContext(Dispatchers.IO) {
callApkBuilder(tmpDir, outApk, projectConfig)
callApkBuilder(tmpDir, outApk, projectConfig.copy())
}
}

Expand Down Expand Up @@ -574,9 +588,6 @@ class BuildViewModel(private val app: Application, private var source: String) :
onBuildFailed(e)
} finally {
apkBuilder.finish()
if (isOldProjectConfigExist) {
saveConfig{}
}
}
}
}
Expand Down
Loading

0 comments on commit 8031a8e

Please sign in to comment.