Android Studio with Transfuse

风流意气都作罢 提交于 2019-12-03 03:54:55

Transfuse and Android Studio work remarkably well together. The trick is to get Transfuse integrated with Gradle. Once you get Gradle working, the build will just kick off the annotation processor and run Transfuse.

I've put together an example reference project here: https://github.com/johncarl81/transfuse/tree/master/examples/gradle

Here's the procedure to get there:

  1. Have Android Studio generate a new Android project
  2. Move the AndroidManifest.xml file to the root of the Android project, ie: ~/project/src/main/AndroidManifest.xml -> ~/project/AndroidManifest.xml

  3. Setup the new AndroidManifest.xml location in the gradle.build file:

    android {
        ...
        sourceSets.main {
            manifest.srcFile 'AndroidManifest.xml'
        }
    }
    
  4. Add the APT plugin:

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.neenbedankt.gradle.plugins:android-apt:1.1'
            classpath 'com.android.tools.build:gradle:0.6.+'
        }
    }
    apply plugin: 'android'
    apply plugin: 'android-apt'
    
  5. Finally add transfuse and transfuse-api:

    dependencies {
        apt 'org.androidtransfuse:transfuse:0.2.7'
        compile 'org.androidtransfuse:transfuse-api:0.2.7'
    }
    

Your final gradle.build will look like this:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.1'
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}
apply plugin: 'android'
apply plugin: 'android-apt'

repositories {
    mavenCentral()
}

dependencies {
    apt 'org.androidtransfuse:transfuse:0.2.7'
    compile 'org.androidtransfuse:transfuse-api:0.2.7'
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 19
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }
    sourceSets.main {
        manifest.srcFile 'AndroidManifest.xml'
    }
}

Edit:

Finally you probably want to add the source/apt_generated/debug or source/apt_generated/release folders as source folders under the project configuration.

Second Edit:

I updated the above example with the new Android-APT plugin

For anyone struggling with "Could not find the AndroidManifest.xml" with Transfuse 3 beta 5, I fixed by adding this to my gradle file:

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!