首页 > 解决方案 > Jetpack 撰写新源无法正常工作

问题描述

我已经尝试使用旧的预构建源进行 Jetpack 组合。但最近我下载了一个新资源,但它对我不起作用。这是我尝试过的。

我在 Project/androidx_prebuilts/out/ui/build/support_repo/androidx 中添加了源

在 androidx 目录下,我有源代码的composeui目录

模块级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"
}

应用程序等级

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
}

我在构建项目时遇到的错误

  * 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

标签: androidandroidxandroid-jetpack-compose

解决方案


首先,请参阅此文档:googlesource

“本节介绍如何设置本地工作环境来构建 Android 源文件。您必须使用 Linux 或 macOS;目前不支持在 Windows 下构建。” — 安卓文档

第1步:

首先,我们需要从(如果未安装) https://source.android.com/setup/build/downloading安装 Repo

并在命令行中点击以下命令

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

现在检查repo status

第2步:

创建一个目录让我们说“AndroidCompose”

mkdir ~/AndroidCompose
cd ~/AndroidCompose

并设置 git config (如果没有设置)

git config — global user.name “Your Real Name”
git config — global user.email “you@example.com”

现在初始化回购

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

存储库现在已初始化,如果我们查看 ~/AndroidCompose 目录,我们将看到一个“.repo”列表

ls -a ~/AndroidCompose/.repo

“现在您的存储库设置为仅提取构建和运行 AndroidX 库所需的内容。下载代码(在我们下载 6GB 时喝杯咖啡)”-Android Docs

第 3 步:获取所有必需源的命令

repo sync -j8 -c

(类似于 git fetch)

现在我们已经下载了正确版本的 Android JetPack 源代码,其中包含 @Composable

这会将所有 Jetpack Compose 源代码放在“ui”目录中

cd ~/AndroidCompose/frameworks/support/ui

./studiow 启动 Android 工作室

重新启动 Android Studio 时要小心。我们需要始终从该目录开始获取 Jetpack Compose 库。

出现提示时接受许可协议。现在我们已准备好编辑、运行和测试!


推荐阅读