I’ve been wondering how to migrate my android project to the SO-CALLED gradle build system. Afraid of messing up with my project, I always auto-skip myself to those tutorials about how wonderful to use gradle. Saying so I’m also very frustrated configing those dependencies. Just drive me crazy every time starting a freshing new project.
So, let’s see how to make a android project to a gradle project (Not transferred by Android Studio, just a plain gradle project). Referencing this post of migrating from intellij,
- make a copy of original project
- delete .git directory & other unnecessary files
- touch a build.gradle and settings.gradle in the project parent directory
- Fill in your build.gradle with these code, just copy & paste from that post:
- If you have many projects need to be imported as dependencies, then you need to modify settings.gradle like this:
// Here I declard 2 of my local libraries, pointing to your actionbarsherlock library project from local instead of downlong from maven repo
include 'ActionBarSherlock'
project(':ActionBarSherlock').projectDir = new File('./ext-libs/actionbarsherlock')
include 'FaceBook'
project(':FaceBook').projectDir = new File('./ext-libs/facebook')
- Then you could use these 2 library in the build.gradle like this:
dependencies {
// Google Play Services
//compile 'com.google.android.gms:play-services:3.2.65'
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':ActionBarSherlock')
compile project(':FaceBook')
//compile project(':GPlayServcie')
//compile 'com.android.support:support-v4:18.0.0'
//compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
//compile 'com.googlecode.android-query:android-query:0.25.9'
//compile 'com.google.code.gson:gson:2.2.4'
//compile 'net.liftmodules:google-analytics_2.5_2.9.1:1.0'
} - Most importantly for each library projects also need a build.gradle in their own project directory. It seems that gradle will build them as well to be used in “root" project. If there’s no special config you could just copy this as the build.gradle into your library project:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.6'
}
}
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:13.0.0'
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
} - Finally and hopefully you can run this without any warning or error:
-> $ gradle assembleDebug --info
If success, you’ll find a apk in the /bin directory of root project, which its name isn’t what I thought it is. Install it but still FC…not sure why the class is missing:
E/AndroidRuntime( 4902): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.bono.project/com.bono.project.activity.FragmentTabs}: java.lang.ClassNotFoundException: Didn't find class "com.bono.project.activity.FragmentTabs" on path: /data/app/com.bono.project-1.apk
E/AndroidRuntime( 4902): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
E/AndroidRuntime( 4902): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
E/AndroidRuntime( 4902): at android.app.ActivityThread.access$600(ActivityThread.java:153)
E/AndroidRuntime( 4902): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
E/AndroidRuntime( 4902): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 4902): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 4902): at android.app.ActivityThread.main(ActivityThread.java:5227)
E/AndroidRuntime( 4902): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 4902): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 4902): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
E/AndroidRuntime( 4902): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
E/AndroidRuntime( 4902): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 4902): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.bono.project.activity.FragmentTabs" on path: /data/app/com.bono.project-1.apk
E/AndroidRuntime( 4902): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
E/AndroidRuntime( 4902): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime( 4902): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime( 4902): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
E/AndroidRuntime( 4902): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
E/AndroidRuntime( 4902): ... 11 more
Anyway, according to that post, Android Dev Tool team will relese a migration tool for devlopers like me to do the tedious job. However the IntelliJ also support gradle, which make me wondering what’s difference between Android Studio and IntelliJ, since AndroidStudio is branching from IntelliJ. Hope we’ll find out soon and this article helps.
via Tumblr http://cyberrob.tumblr.com/post/64102567332
沒有留言:
張貼留言