【问题标题】:Jetpack compose new source is not workingJetpack 撰写新源无法正常工作
【发布时间】:2020-01-07 10:07:59
【问题描述】:

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

我已在 项目/androidx_prebuilts/out/ui/build/support_repo/androidx

在 androidx 目录下,我有 composeui 源目录

模块级分级

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

【问题讨论】:

    标签: android androidx android-jetpack-compose


    【解决方案1】:

    首先,参考这个文档:googlesource

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

    第 1 步:

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

    然后在你的命令行中点击下面的命令

    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

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

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

    ls -a ~/AndroidCompose/.repo

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

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

    repo sync -j8 -c
    

    (类似于 git fetch)

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

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

    cd ~/AndroidCompose/frameworks/support/ui
    

    ./studiow 启动 Android 工作室

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

    出现提示时接受许可协议。现在我们可以进行编辑、运行和测试了!

    【讨论】:

    【解决方案2】:

    jetpack compose UI 组件构建现在可作为开发版本使用。 你不必使用特殊的android studio。只需包含所有 compose Gradle 依赖项,版本为 '0.1.0-dev01'

    更新:现在 Android Studio 4.0 canary 自带 Jetpack Compose 和实时预览,开箱即用。

    【讨论】:

      【解决方案3】:

      我也遇到了这个问题,然后我找到了一个适合我的解决方案。 我正在与您共享该 URL。

      https://github.com/Mishkun/try-compose

      【讨论】:

      • 感谢分享,我已经尝试过上面的例子,但是它包含一个我已经设置的旧源,对于新源我找到了一种方法。我会发布它作为答案。
      猜你喜欢
      • 2023-01-12
      • 2023-02-03
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多