forked from libgdx/gdx-liftoff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAndroid.kt
246 lines (226 loc) · 8.85 KB
/
Android.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package gdx.liftoff.data.platforms
import gdx.liftoff.data.files.SourceFile
import gdx.liftoff.data.files.gradle.GradleFile
import gdx.liftoff.data.project.Project
import gdx.liftoff.views.GdxPlatform
/**
* Represents Android backend.
*/
@GdxPlatform
class Android : Platform {
companion object {
const val ID = "android"
const val ORDER = Lwjgl3.ORDER + 1
}
override val id = ID
override val order = ORDER
override val isStandard = false // user should only jump through android hoops on request
override fun initiate(project: Project) {
project.rootGradle.buildDependencies.add("\"com.android.tools.build:gradle:\$androidPluginVersion\"")
project.properties["androidPluginVersion"] = project.advanced.androidPluginVersion
addGradleTaskDescription(project, "lint", "performs Android project validation.")
addCopiedFile(project, "ic_launcher-web.png")
addCopiedFile(project, "proguard-rules.pro")
addCopiedFile(project, "project.properties")
addCopiedFile(project, "res", "drawable-hdpi", "ic_launcher.png")
addCopiedFile(project, "res", "drawable-mdpi", "ic_launcher.png")
addCopiedFile(project, "res", "drawable-xhdpi", "ic_launcher.png")
addCopiedFile(project, "res", "drawable-xxhdpi", "ic_launcher.png")
addCopiedFile(project, "res", "values", "styles.xml")
project.files.add(
SourceFile(
projectName = "", sourceFolderPath = "", packageName = "", fileName = "local.properties",
content = "# Location of the Android SDK:\nsdk.dir=${project.basic.androidSdk}"
)
)
project.files.add(
SourceFile(
projectName = ID, sourceFolderPath = "res", packageName = "values", fileName = "strings.xml",
content = """<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">${project.basic.name}</string>
</resources>
"""
)
)
project.files.add(
SourceFile(
projectName = ID, sourceFolderPath = "", packageName = "", fileName = "AndroidManifest.xml",
content = """<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="${project.basic.rootPackage}">
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<application
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="@drawable/ic_launcher"
android:isGame="true"
android:appCategory="game"
android:label="@string/app_name"
tools:ignore="UnusedAttribute"
android:theme="@style/GdxTheme">
<activity
android:name="${project.basic.rootPackage}.android.AndroidLauncher"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize|screenLayout"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
${project.androidPermissions.joinToString(separator = "\n") { " <uses-permission android:name=\"${it}\" />" }}
</manifest>
"""
)
)
}
override fun createGradleFile(project: Project): GradleFile = AndroidGradleFile(project)
}
/**
* Gradle file of the Android project.
*/
class AndroidGradleFile(val project: Project) : GradleFile(Android.ID) {
val plugins = mutableListOf<String>()
val srcFolders = mutableListOf("'src/main/java'")
val nativeDependencies = mutableSetOf<String>()
var latePlugin = false
init {
dependencies.add("project(':${Core.ID}')")
addDependency("com.badlogicgames.gdx:gdx-backend-android:\$gdxVersion")
addNativeDependency("com.badlogicgames.gdx:gdx-platform:\$gdxVersion:natives-armeabi-v7a")
addNativeDependency("com.badlogicgames.gdx:gdx-platform:\$gdxVersion:natives-arm64-v8a")
addNativeDependency("com.badlogicgames.gdx:gdx-platform:\$gdxVersion:natives-x86")
addNativeDependency("com.badlogicgames.gdx:gdx-platform:\$gdxVersion:natives-x86_64")
plugins.add("com.android.application")
}
fun insertLatePlugin() { latePlugin = true }
/**
* @param dependency will be added as "natives" dependency, quoted.
*/
fun addNativeDependency(dependency: String) = nativeDependencies.add("\"$dependency\"")
override fun getContent(): String = """${plugins.joinToString(separator = "\n") { "apply plugin: '$it'" }}
${if (latePlugin)"apply plugin: \'kotlin-android\'" else ""}
android {
compileSdkVersion ${project.advanced.androidSdkVersion}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = [${srcFolders.joinToString(separator = ", ")}]
aidl.srcDirs = [${srcFolders.joinToString(separator = ", ")}]
renderscript.srcDirs = [${srcFolders.joinToString(separator = ", ")}]
res.srcDirs = ['res']
assets.srcDirs = ['../assets']
jniLibs.srcDirs = ['libs']
}
}
packagingOptions {
// Preventing from license violations (more or less):
pickFirst 'META-INF/LICENSE.txt'
pickFirst 'META-INF/LICENSE'
pickFirst 'META-INF/license.txt'
pickFirst 'META-INF/LGPL2.1'
pickFirst 'META-INF/NOTICE.txt'
pickFirst 'META-INF/NOTICE'
pickFirst 'META-INF/notice.txt'
// Excluding unnecessary meta-data:
exclude 'META-INF/robovm/ios/robovm.xml'
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/dependencies.txt'
}
defaultConfig {
applicationId '${project.basic.rootPackage}'
minSdkVersion 19
targetSdkVersion ${project.advanced.androidSdkVersion}
versionCode 1
versionName "1.0"
multiDexEnabled true
}
compileOptions {
sourceCompatibility "${project.advanced.javaVersion}"
targetCompatibility "${project.advanced.javaVersion}"
${if (project.advanced.javaVersion != "1.6" && project.advanced.javaVersion != "1.7")"coreLibraryDesugaringEnabled true" else ""}
}
${if (latePlugin && project.advanced.javaVersion != "1.6" && project.advanced.javaVersion != "1.7")"kotlinOptions.jvmTarget = \"1.8\"" else ""}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
// needed for AAPT2, may be needed for other tools
google()
}
configurations { natives }
dependencies {
${if (project.advanced.javaVersion != "1.6" && project.advanced.javaVersion != "1.7")"coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'" else ""}
${joinDependencies(dependencies)}
${joinDependencies(nativeDependencies, "natives")}
// This follows advice from https://blog.gradle.org/log4j-vulnerability
constraints {
implementation("org.apache.logging.log4j:log4j-core") {
version {
strictly("[2.17, 3[")
prefer("2.17.0")
}
because("CVE-2021-44228, CVE-2021-45046, CVE-2021-45105: Log4j vulnerable to remote code execution and other critical security vulnerabilities")
}
}
}
// Called every time gradle gets executed, takes the native dependencies of
// the natives configuration, and extracts them to the proper libs/ folders
// so they get packed with the APK.
task copyAndroidNatives() {
doFirst {
file("libs/armeabi-v7a/").mkdirs()
file("libs/arm64-v8a/").mkdirs()
file("libs/x86_64/").mkdirs()
file("libs/x86/").mkdirs()
configurations.getByName("natives").copy().files.each { jar ->
def outputDir = null
if(jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a")
if(jar.name.endsWith("natives-arm64-v8a.jar")) outputDir = file("libs/arm64-v8a")
if(jar.name.endsWith("natives-x86_64.jar")) outputDir = file("libs/x86_64")
if(jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86")
if(outputDir != null) {
copy {
from zipTree(jar)
into outputDir
include "*.so"
}
}
}
}
}
tasks.matching { it.name.contains("merge") && it.name.contains("JniLibFolders") }.configureEach { packageTask ->
packageTask.dependsOn 'copyAndroidNatives'
}
task run(type: Exec) {
def path
def localProperties = project.file("../local.properties")
if (localProperties.exists()) {
Properties properties = new Properties()
localProperties.withInputStream { instr ->
properties.load(instr)
}
def sdkDir = properties.getProperty('sdk.dir')
if (sdkDir) {
path = sdkDir
} else {
path = "${'$'}System.env.ANDROID_SDK_ROOT"
}
} else {
path = "${'$'}System.env.ANDROID_SDK_ROOT"
}
def adb = path + "/platform-tools/adb"
commandLine "${'$'}adb", 'shell', 'am', 'start', '-n', '${project.basic.rootPackage}/${project.basic.rootPackage}.android.AndroidLauncher'
}
eclipse.project.name = appName + "-android"
"""
}