mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-08 14:52:13 -04:00

* We have implemented Play Asset Delivery to include the ZIM file within the Android App Bundle (AAB). * To achieve this, we've created Gradle tasks to automatically download the ZIM file and place it inside the assets folder. * In order to utilize this asset file, we've refactored our custom application code. * We haven't removed the functionality of uploading the APK, as this approach remains static for now. Thus, we are retaining both codebases.
146 lines
5.1 KiB
Kotlin
146 lines
5.1 KiB
Kotlin
/* Kiwix Android
|
|
* Copyright (c) 2019 Kiwix <android.kiwix.org>
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package custom
|
|
|
|
import com.android.build.gradle.api.ApkVariantOutput
|
|
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
|
|
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
|
|
import com.google.api.client.http.FileContent
|
|
import com.google.api.client.http.javanet.NetHttpTransport
|
|
import com.google.api.client.json.GenericJson
|
|
import com.google.api.client.json.jackson2.JacksonFactory
|
|
import com.google.api.services.androidpublisher.AndroidPublisher
|
|
import com.google.api.services.androidpublisher.AndroidPublisherScopes
|
|
import com.google.api.services.androidpublisher.model.ExpansionFile
|
|
import com.google.api.services.androidpublisher.model.ExpansionFilesUploadResponse
|
|
import com.google.api.services.androidpublisher.model.Track
|
|
import com.google.api.services.androidpublisher.model.TrackRelease
|
|
import java.io.File
|
|
import java.io.FileInputStream
|
|
import java.security.KeyStore
|
|
|
|
@Suppress("DEPRECATION")
|
|
fun createPublisher(auth: File): AndroidPublisher {
|
|
val transport = buildTransport()
|
|
val factory = JacksonFactory.getDefaultInstance()
|
|
|
|
val credential =
|
|
GoogleCredential.fromStream(auth.inputStream(), transport, factory)
|
|
.createScoped(listOf(AndroidPublisherScopes.ANDROIDPUBLISHER))
|
|
|
|
|
|
return AndroidPublisher.Builder(transport, JacksonFactory.getDefaultInstance()) {
|
|
credential.initialize(it.setReadTimeout(0))
|
|
}.setApplicationName("kiwixcustom").build()
|
|
}
|
|
|
|
private fun buildTransport(): NetHttpTransport {
|
|
val trustStore: String? = System.getProperty("javax.net.ssl.trustStore", null)
|
|
val trustStorePassword: String? =
|
|
System.getProperty("javax.net.ssl.trustStorePassword", null)
|
|
|
|
return if (trustStore == null) {
|
|
GoogleNetHttpTransport.newTrustedTransport()
|
|
} else {
|
|
val ks = KeyStore.getInstance(KeyStore.getDefaultType())
|
|
FileInputStream(trustStore).use { fis ->
|
|
ks.load(fis, trustStorePassword?.toCharArray())
|
|
}
|
|
NetHttpTransport.Builder().trustCertificates(ks).build()
|
|
}
|
|
}
|
|
|
|
class Transaction(
|
|
private val publisher: AndroidPublisher,
|
|
private val packageName: String,
|
|
val editId: String
|
|
) {
|
|
fun uploadExpansionTo(
|
|
file: File,
|
|
versionCode: Int
|
|
): ExpansionFilesUploadResponse = publisher.edits().expansionfiles()
|
|
.upload(
|
|
packageName,
|
|
editId,
|
|
versionCode,
|
|
"main",
|
|
FileContent("application/octet-stream", file)
|
|
).execute().prettyPrint()
|
|
|
|
@Suppress("DEPRECATION")
|
|
fun attachExpansionTo(expansionCode: Int, apkVariantOutput: ApkVariantOutput): ExpansionFile =
|
|
publisher.edits().expansionfiles().update(
|
|
packageName,
|
|
editId,
|
|
apkVariantOutput.versionCodeOverride,
|
|
"main",
|
|
ExpansionFile().apply { referencesVersion = expansionCode }
|
|
).execute().prettyPrint()
|
|
|
|
@Suppress("DEPRECATION")
|
|
fun uploadApk(apkVariantOutput: ApkVariantOutput) {
|
|
publisher.edits().apks().upload(
|
|
packageName,
|
|
editId,
|
|
FileContent("application/octet-stream", apkVariantOutput.outputFile)
|
|
).execute().prettyPrint()
|
|
}
|
|
|
|
fun uploadBundle(file: File) {
|
|
publisher.edits().apks().upload(
|
|
packageName,
|
|
editId,
|
|
FileContent("application/octet-stream", file)
|
|
).execute().prettyPrint()
|
|
}
|
|
|
|
@Suppress("DEPRECATION")
|
|
fun addToTrackInDraft(apkVariants: List<ApkVariantOutput>): Track =
|
|
publisher.edits().tracks().update(packageName, editId, "internal", Track().apply {
|
|
releases = listOf(TrackRelease().apply {
|
|
status = "draft"
|
|
name = apkVariants[0].versionNameOverride
|
|
versionCodes = apkVariants.map { it.versionCodeOverride.toLong() }
|
|
})
|
|
track = "internal"
|
|
}).execute().prettyPrint()
|
|
|
|
@Suppress("DEPRECATION")
|
|
fun addBundleToTrackInDraft(versionCode: Int, versionName: String?): Track =
|
|
publisher.edits().tracks().update(packageName, editId, "internal", Track().apply {
|
|
releases = listOf(TrackRelease().apply {
|
|
status = "draft"
|
|
name = versionName
|
|
versionCodes = listOf(versionCode.toLong())
|
|
})
|
|
track = "internal"
|
|
}).execute().prettyPrint()
|
|
}
|
|
|
|
fun AndroidPublisher.transactionWithCommit(packageName: String, func: Transaction.() -> Unit) {
|
|
Transaction(
|
|
this,
|
|
packageName,
|
|
edits().insert(packageName, null).execute().prettyPrint().id
|
|
).apply {
|
|
func()
|
|
edits().validate(packageName, editId).execute().prettyPrint()
|
|
}.also { edits().commit(packageName, it.editId).execute().prettyPrint() }
|
|
}
|
|
|
|
private fun <T : GenericJson> T.prettyPrint() = also { println(it.toPrettyString()) }
|