@@ -2,6 +2,14 @@ apply plugin: 'com.android.library'
22apply plugin : " maven-publish"
33apply plugin : " signing"
44
5+ import org.gradle.internal.logging.text.StyledTextOutputFactory ;
6+ import static org.gradle.internal.logging.text.StyledTextOutput.Style;
7+ import java.nio.file.Files
8+ import java.nio.file.Paths
9+ import static java.nio.file.StandardCopyOption.REPLACE_EXISTING
10+
11+ def log = services. get(StyledTextOutputFactory ). create(" sqlcipher" )
12+
513android {
614 compileSdkVersion 34
715 namespace " net.zetetic.database"
@@ -69,6 +77,75 @@ android {
6977
7078}
7179
80+ static def shellCmd (String workingDirectory ,
81+ String ... arguments ) {
82+ if (! arguments || arguments. length == 0 ) {
83+ throw new IllegalArgumentException (" At least one argument is required" )
84+ }
85+ def command = [" sh" , " -c" , ' exec "$@"' , " sh" , * arguments]
86+ def processBuilder = new ProcessBuilder (command)
87+ processBuilder. directory(new File (workingDirectory))
88+ def process = processBuilder. start()
89+ process. inputStream. eachLine { println it }
90+ process. errorStream. eachLine { System . err. println (it) }
91+ process. waitFor()
92+ }
93+
94+ def generateAmalgamation = tasks. register(" generateAmalgamation" ) {
95+ description = " Generate SQLCipher amalgamation source files"
96+ group = " SQLCipher"
97+ def sqlcipherDir = new File (" ${ project.rootDir} /sqlcipher/src/main/jni/sqlcipher/" )
98+ def submoduleDir = new File (sqlcipherDir. absolutePath, " src/" )
99+ def amalgamationSource = new File (sqlcipherDir. absolutePath, " sqlite3.c" )
100+ def amalgamationHeader = new File (sqlcipherDir. absolutePath, " sqlite3.h" )
101+ doLast {
102+ if (! submoduleDir. exists()) {
103+ log. withStyle(Style.Failure ). println (' SQLCipher submodule missing, exiting' )
104+ throw new GradleException (" SQLCipher submodule missing: please run git submodule update --init" )
105+ }
106+ log. withStyle(Style.Info ). println (' SQLCipher amalgamation not found, generating.' )
107+ shellCmd(submoduleDir. absolutePath, " ./configure" , " --with-tempstore=yes" , " --disable-tcl" )
108+ shellCmd(submoduleDir. absolutePath, " make" , " clean" )
109+ shellCmd(submoduleDir. absolutePath, " make" , " sqlite3.c" )
110+ def filesToMove = [" sqlite3.c" , " sqlite3.h" ]
111+ filesToMove. each {
112+ def sourcePath = Paths . get(submoduleDir. absolutePath, it)
113+ def targetPath = Paths . get(sqlcipherDir. absolutePath, it)
114+ def moveResult = Files . move(sourcePath, targetPath, REPLACE_EXISTING );
115+ if (moveResult == null ){
116+ throw new GradleException (" Faile to move ${ sourcePath} to ${ targetPath} " )
117+ }
118+ }
119+ }
120+ outputs. upToDateWhen {
121+ return amalgamationSource. exists()
122+ && amalgamationHeader. exists()
123+ }
124+ }
125+
126+ tasks. matching { it. name == " preBuild" }. configureEach {
127+ dependsOn generateAmalgamation
128+ }
129+
130+ def cleanAmalgamation = tasks. register(" cleanAmalgamation" ) {
131+ description = " Cleans SQLCipher amalgamation source files"
132+ group = " SQLCipher"
133+ def amalgamationSource = file(" ${ project.rootDir} /sqlcipher/src/main/jni/sqlcipher/sqlite3.c" )
134+ def amalgamationHeader = file(" ${ project.rootDir} /sqlcipher/src/main/jni/sqlcipher/sqlite3.h" )
135+ doLast {
136+ println " Clean SQLCipher amalgamation"
137+ delete(amalgamationSource, amalgamationHeader)
138+ }
139+ outputs. upToDateWhen {
140+ return ! amalgamationSource. exists()
141+ && ! amalgamationHeader. exists()
142+ }
143+ }
144+
145+ tasks. named(" clean" ) {
146+ dependsOn(cleanAmalgamation)
147+ }
148+
72149dependencies {
73150 implementation fileTree(include : [' *.jar' ], dir : ' libs' )
74151
0 commit comments