Search code examples
androidandroidxandroid-jetpack-compose

Jetpack compose new source is not working


I have already tried Jetpack compose with old prebuild source. But recently I downloaded a new source and it isn't working for me. Here is what I have tried.

I have added source in Project/androidx_prebuilts/out/ui/build/support_repo/androidx

below androidx directory, I have compose and ui directories for the source

Module level gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

repositories {
    maven { url "$androidx_home/out/ui/build/support_repo/" }
    google()
    jcenter()
}

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.compose.app"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        kotlinOptions {
            jvmTarget = "1.8"
//           typeuseIR = true
            freeCompilerArgs += "-P"
            freeCompilerArgs +=
                    "plugin:androidx.compose.plugins.kotlin:syntax=FCS"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.41"
    implementation "androidx.appcompat:appcompat:1.0.2"
    implementation "androidx.core:core-ktx:1.0.2"

    implementation "androidx.compose:compose-runtime:1.0.0-alpha01"
    implementation "androidx.ui:ui-animation:1.0.0-alpha01"
    implementation "androidx.ui:ui-animation-core:1.0.0-alpha01"
    implementation "androidx.ui:ui-android-view:1.0.0-alpha01"
    implementation "androidx.ui:ui-android-view-non-ir:1.0.0-alpha01"
    implementation "androidx.ui:ui-android-text:1.0.0-alpha01"
    implementation "androidx.ui:ui-core:1.0.0-alpha01"
    implementation "androidx.ui:ui-framework:1.0.0-alpha01"
    implementation "androidx.ui:ui-layout:1.0.0-alpha01"
    implementation "androidx.ui:ui-material:1.0.0-alpha01"
    implementation "androidx.ui:ui-platform:1.0.0-alpha01"
    implementation "androidx.ui:ui-text:1.0.0-alpha01"


    testImplementation "junit:junit:4.12"
    androidTestImplementation "androidx.test:runner:1.1.1"
    androidTestImplementation "androidx.test.espresso:espresso-core:3.1.1"
}

Application level gradle

buildscript {
    ext.kotlin_version = "1.3.41"
    ext.androidx_home = project.properties["androidx.home"] ?: "$projectDir/androidx_prebuilts"
    repositories {
        maven { url "$androidx_home/out/ui/build/support_repo/" }
        maven {
            url "http://dl.bintray.com/kotlin/kotlin-eap"
        }

        mavenLocal()
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.0-alpha09'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

apply plugin: 'idea'

idea.module {
    excludeDirs += file('$projectDir/androidx_prebuilts')
}

allprojects {
    repositories {

        maven {
            url "https://dl.bintray.com/kotlin/kotlin-dev/"
        }
        maven { url "$androidx_home/out/ui/build/support_repo/" }

        mavenLocal()
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Error I'm getting while building the project

  * What went wrong:
Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> Could not resolve all task dependencies for configuration ':app:debugCompileClasspath'.
   > Could not find androidx.compose:compose-runtime:1.0.0-alpha01.
     Required by:
         project :app
   > Could not find androidx.ui:ui-animation:1.0.0-alpha01.
     Required by:
         project :app
   > Could not find androidx.ui:ui-animation-core:1.0.0-alpha01.
     Required by:
         project :app
   > Could not find androidx.ui:ui-android-view:1.0.0-alpha01.
     Required by:
         project :app
   > Could not find androidx.ui:ui-android-view-non-ir:1.0.0-alpha01.
     Required by:
         project :app
   > Could not find androidx.ui:ui-android-text:1.0.0-alpha01.
     Required by:
         project :app
   > Could not find androidx.ui:ui-core:1.0.0-alpha01.
     Required by:
         project :app
   > Could not find androidx.ui:ui-framework:1.0.0-alpha01.
     Required by:
         project :app
   > Could not find androidx.ui:ui-layout:1.0.0-alpha01.
     Required by:
         project :app
   > Could not find androidx.ui:ui-material:1.0.0-alpha01.
     Required by:
         project :app
   > Could not find androidx.ui:ui-platform:1.0.0-alpha01.
     Required by:
         project :app
   > Could not find androidx.ui:ui-text:1.0.0-alpha01.
     Required by:
         project :app

Solution

  • First, refer to this doc : googlesource

    "This section describes how to set up your local work environment to build the Android source files. You must use Linux or macOS; building under Windows is not currently supported." — Android Docs

    Step 1:

    First, we need to install Repo from (if not installed) https://source.android.com/setup/build/downloading

    and hit below command in your command line

    mkdir bin 
    PATH=~/bin:$PATH
    curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
    chmod a+x ~/bin/repo
    

    Now check repo status

    Step 2:

    Create a directory lets say "AndroidCompose"

    mkdir ~/AndroidCompose
    cd ~/AndroidCompose
    

    and setup git config like (if not setup)

    git config — global user.name “Your Real Name”
    git config — global user.email “[email protected]

    Now initialise repo

    repo init -u https://android.googlesource.com/platform/manifest -b androidx-master-dev
    

    The repo is now initialized and if we look in the ~/AndroidCompose directory we will see a “.repo” listing

    hit

    ls -a ~/AndroidCompose/.repo

    "Now your repository is set to pull only what you need for building and running AndroidX libraries. Download the code (and grab a coffee while we pull down 6GB)" -Android Docs

    Step 3: Command to get all required source

    repo sync -j8 -c
    

    (it’s similar to git fetch)

    Now we have downloaded the correct version of Android JetPack source which contains @Composable

    This places all the Jetpack Compose source in the "ui" directory

    cd ~/AndroidCompose/frameworks/support/ui
    

    hit

    ./studiow to start Android studio

    Be careful when restarting Android Studio. We need to always start from this directory to get the Jetpack Compose libraries.

    Accept the license agreement when prompted. Now we are ready to edit, run, and test!