What is the best way to add LibGDX to an existing Android Studio project?
Because Android Studio already has Gradle built in, I was assuming that I could change some code in the build.gradle file to add LibGDX to my existing project. The only gradle instructions I found on LibGDX was the automated tool for making a new project with LibGDX already baked in.
All idea of libgdx is writing one code for all platforms. So Android specific code should be added in properly way and if you will just add libgdx dependencies to your project it will not help you a lot. You will be able to use only very small part of libgdx features. If you want to do it in any case here is a example of build.gradle which adds libgdx dependencies to regular Java project, compare it with your build.gradle and add needed lines to ext, repositories and dependencies sections.
apply plugin: "eclipse"
apply plugin: "idea"
apply plugin: "java"
apply plugin: "maven"
sourceCompatibility = 1.6
targetCompatibility = 1.6
version = '0.1'
group = 'com.gdx.bream'
ext {
appName = 'bream'
gdxVersion = '1.3.1'
}
repositories {
mavenCentral()
mavenLocal()
maven { url 'https://github.com/steffenschaefer/gwt-gradle-plugin/raw/maven-repo/' }
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
sourceSets {
main {
java { srcDirs = ['src']}
resources {
srcDirs = ['src']
}
}
}
task sourcesJar(type:Jar){
from sourceSets.main.allSource
classifier = 'sources'
}
artifacts { archives sourcesJar }
dependencies { compile "com.badlogicgames.gdx:gdx:$gdxVersion" }
P.S If you want create game only for Android and looking for engine which can be easily integrated to existing project, take a look at AndEngine
Related
I have a kotlin multiplatform project A setup for iOS and Android, it works well. It has a common module for sharing business logic, and platform-android and platform-ios module for implementing the platform API.
After I adding the common and platform-android module from project A to another Android project B, the Android Studio IDE reports tons of syntax error, but the codes build and run from Android studio without a problem.
The syntax looks like the kotlin-stdlib is not there while it's indeed in the build.gradle, otherwise it won't build.
For instance:
val filterMap = mutableMapOf<String, MenuFilter>()
Android studio will say Unresolved reference: mutableMapOf
Some facts:
common module has the problem
no problem for platform-android.
and of course, no problem when I use IDEA to edit project A
my build.gradle for common module looks like this:
apply plugin: 'kotlin-platform-common'
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
}
sourceSets {
main.kotlin.srcDirs += 'main/'
test.kotlin.srcDirs += 'test/'
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.kotlin
}
artifacts {
archives sourcesJar
}
kotlin {
experimental {
coroutines "enable"
}
}
The rootProject build.gradle in Android Studio is:
buildscript {
ext{
kotlin_version = '1.2.41'
anko_version = '0.10.4'
dagger_version = '2.15'
support_lib_version = '27.1.1'
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
And this is the settings.gradle file for Android project B
def projectA_path = "path/to/projectA"
include(":common")
project(":common").projectDir = new File("$projectA_path/common")
include(":platforms:android")
project(":platforms:android").projectDir = new File("$projectA_path/platforms/android")
Even the two are from different projects, according to the setup here. Shouldn't the two just work? What am I missing here?
IDE version:
IDEA Ultimate 2018.1
Android studio 3.1.2
Try adding this in build.gradle
apply plugin: 'org.jetbrains.kotlin.multiplatform'
I'm trying to follow the tutorial here: https://cloud.google.com/solutions/mobile/firebase-app-engine-android-studio to create a Google App Engine Module. I followed the first step to use the UI to generate an App Engine Module, but after that I got stuck on the part about adding dependencies. When I try to use File > Project Structure > + > Add Library Dependencies, none of three dependencies listed in the tutorial show up in the list of available libraries. After reading other StackOverflow posts, I tried the following:
I updated Android Studio, Android Build Tools, Google Play services, and the Google Repository to the newest version.
I tried adding the dependencies manually by writing compile statements in the backend module build.gradle file (e.g. compile 'com.google.appengine:appengine-api-1.0-sdk'). Then the sync failed and Android Studio gave me the following error: Failed to resolve: com.google.appengine:appengine-api-1.0-sdk
I tried File > Restart / Invalidate Cache
I tried using Android Studio on a different computer
All this, yet no success. Below are my gradle files, in case they are of use. Although I would be surprised if they would be, since I just followed the app engine part of the tutorial.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
backend module build.gradle:
// If you would like more information on the gradle-appengine-plugin please refer to the github page
// https://github.com/GoogleCloudPlatform/gradle-appengine-plugin
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.google.appengine:gradle-appengine-plugin:1.9.42'
}
}
repositories {
jcenter();
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'appengine'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
dependencies {
appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.42'
compile 'javax.servlet:servlet-api:2.5'
compile 'com.google.appengine:appengine-api-1.0-sdk'
compile 'com.google.firebase:firebase-server-sdk'
compile 'org.apache.httpcomponents:httpclient'
}
appengine {
downloadSdk = true
appcfg {
oauth2 = true
}
}
Android Studio gives me the error message mentioned above on the last three dependencies in the above file when I try to sync. Any help would be appreciated. Thanks.
What is the preferred way to share some code (E.g. a Utils class) between two projects when building two apps using Gradle to build?
Can I do this without creating extra jar files? I just want my code to sit outside the app projects, be imported/compiled into both app projects. Or is this simply not possible?
I'm familiar with the approach that uses jars or Android library projects, but both seem a bit unwieldy.
My favorite way of doing this is by keeping it in a local Maven repo. The repo can even live in your SCM so it's the same across workspaces.
Create a new Android Studio project and then set it as a maven project your build.gradle config:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android-library'
apply plugin: 'maven'
repositories {
mavenCentral()
}
configurations {
archives {
extendsFrom configurations.default
}
}
group = 'com.mypackage.mylibrary'
version = '1.0.0'
uploadArchives {
configuration = configurations.archives
repositories {
mavenDeployer {
repository(url: uri("relative/path/to/localrepo"))
pom.project {
artifactId 'mylibrary'
name 'My Library'
packaging 'aar'
}
}
}
}
android {
// copy old android config here
}
You'll need to deploy the library before you can use it. Do this by using the uploadArchives task [./gradlew uploadArchives]
Now you should be able to use this library in any project by doing this:
repositories {
maven { url 'relative/path/to/localrepo' }
}
dependencies {
compile ('com.mypackage.mylibrary:1.0.0')
}
When you make changes to your library, you'll have to re-deploy (uploadArchives) with a new version, then update the dependency reference in whatever project needs the new version.
I am new to Gradle. Is there some example how to configure properly gradle-android-plugin for scala classes.
this is what I have now.
buildscript {
repositories { mavenCentral() }
dependencies { classpath 'org.gradle.api.plugins:gradle-android-plugin:1.2.1' }
}
apply plugin: 'android'
apply plugin: 'eclipse'
apply plugin: 'scala'
sourceCompatibility = 1.6
version = "1.0.0"
repositories { mavenCentral() }
dependencies {
compile files('/home/pcu/workspace/workspace-android/emoo/libs/android-support-v4.jar')
compile 'org.scala-lang:scala-library:2.9.1'
scalaTools 'org.scala-lang:scala-compiler:2.9.1'
scalaTools 'org.scala-lang:scala-library:2.9.1'
}
task configureDebug << { jar.classifier = "debug" }
task configureRelease << { proguard.enabled = true }
but compilation fails. Scala class is not compiled.
It is actually much simpler with the right plugin:
https://github.com/saturday06/gradle-android-scala-plugin
This worked perfectly fine for me.
You need only about 3 more lines in your gradle configuration and potentially proguard to reduce the apk size.
The setup is well documented on the github page. Everything beyond steps 1-3 is optional.
Im trying to build a simple android app using gradle build tools. but im getting an error like this
No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.compile() is applicable for argument types: (java.lang.String) values: [org.gradle.api.plugins:gradle-android-plugin:1.2.0-SNAPSHOT]
Possible solutions: module(java.lang.Object)
ang here's a simple configuration of build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
compile 'org.gradle.api.plugins:gradle-android-plugin:1.2.0-SNAPSHOT'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
processResource {
expand (project.properties)
}
task configureDebug << {
jar.classifier = "debug"
}
task configureRelease << {
proguard.enabled = true
}
When applying a plugin you want tell you build script to use it in its classpath. It is not required for compilation so just change the configuration compile to classpath. More more information see 51.5.1. Using your plugin in another project in the Gradle user guide.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.gradle.api.plugins:gradle-android-plugin:1.2.0-SNAPSHOT'
}
}
EDIT: At the moment the plugin does not support r20 of the Android SDK. For more information see this issue.
Make sure you are writing the dependency block on your application build.gradle "YourProjectName->yourprojectname->build.gradle" in android studio hierarchy .
Use android gradle tools
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}