简体   繁体   English

AndroidStudio ClassNotFound gradle

[英]AndroidStudio ClassNotFound gradle

After a couple of hours, I finally got my app to build. 几个小时后,我终于得到了我的应用程序来构建。 Using gradlew, it builds using two submodules. 使用gradlew,它使用两个子模块构建。

Whenever I install the result to my phone, it automatically throw an UndefinedClass error on the first created class (Activity) 每当我将结果安装到我的手机时,它会在第一个创建的类(Activity)上自动抛出UndefinedClass错误

build.gradle 的build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile files('libs/crittercism_v3_0_3_sdkonly.jar')
    compile project(':libraries:slidingmenu')
    compile project(':libraries:actionbarsherlock')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

An settings.gradle 一个settings.gradle

include 'libraries:actionbarsherlock', ':libraries:slidingmenu', ':NewApp'

I'm kind of at lost, it's building and all now but it apparently doesn't add the class to the package.. 我有点迷茫,它现在正在构建,但显然没有将类添加到包中。

I've migrated my android app project for this purpose and it works. 我为此目的迁移了我的Android应用程序项目,它的工作原理。 This is how my build looks like: 这就是我的构建方式:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}

apply plugin: 'android'
apply plugin: 'eclipse'

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile 'com.google.guava:guava:14.0.1'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'commons-io:commons-io:2.4'
    compile 'pepuch:java-socket-server:1.0-SNAPSHOT'
    compile 'pepuch:multiplayergame-socket-server:1.0-SNAPSHOT'
}

android {
    compileSdkVersion 10
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']

            resources.srcDirs = ['src']
            aild.srcDirs = ['src']

            renderscript.srcDirs = ['src']

            res.srcDirs = ['res']
            assets.srcDirs = ['assets']

        }


        instrumentTest.setRoot('tests')
    }
}

I haven't integrate it with any IDE but after I run gradle installDebug it generated *.apk on my phone which I installed. 我没有将它与任何IDE集成,但在我运行gradle installDebug它在我安装的手机上生成* .apk。 BTW I didn't change project structure but saved old directory structure. 顺便说一句,我没有改变项目结构,但保存了旧的目录结构。

Having to get it work with Android studio / Gradle isn't quite straightforward yet. 必须让它与Android studio / Gradle一起使用还不是很简单。 I have to say that how it works is still quite messy but I finally got it to work after reading many answers. 我不得不说它是如何工作的仍然相当混乱,但我终于在阅读了很多答案之后才开始工作。

If you have a project that has these dependencies: 如果您有一个具有这些依赖项的项目:

  • App depends on (Module1, Module2) 应用取决于(Module1,Module2)
  • Module1 depends on (Module2) Module1依赖于(Module2)

  • Open the SDK manager and install the Google repository and Android support repository 打开SDK管理器并安装Google存储库Android支持存储库

  • To make things simpler, move files in a structure such as Gradle expect it to be. 为了简化操作,可以移动Gradle等结构中的文件。 (I can't tell if other structure will mess up with android plugins) (我不知道其他结构是否会搞乱android插件)
  • Add an empty application inside each android library manifest. 在每个android库清单中添加一个空应用程序。

The package, uses-sdk and empty application is the strict minimum for a android-library: 包,uses-sdk和empty应用程序是android-library的严格最低要求:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.mymodule">
    <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="16"/>
    <application />
</manifest>

The modules have this structure 模块具有这种结构

  • Module/build (where things get built) 模块/构建(构建内容的地方)
  • Module/src/main/java (the actual code) Module / src / main / java(实际代码)
  • Module/src/main/res (resources layout and so on) Module / src / main / res(资源布局等)
  • Module/src/main/AndroidManifest.xml 模块/ SRC /主/ AndroidManifest.xml中
  • Module/build.gradle (our gradle config) Module / build.gradle(我们的gradle配置)

My first module as this build.gradle file 我的第一个模块是这个build.gradle文件

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    } } apply plugin: 'android-library'

repositories {
    mavenLocal()
    mavenCentral() }

dependencies {
    compile 'com.android.support:support-v4:13.0.0'
    compile project(':SecondModule') }

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

Note that the module depens on project(:SecondModule) This means that at the root of the project is located SecondModule in which there is also a build.gradle file. 请注意,模块依赖于项目(:SecondModule)这意味着在项目的根目录中找到了SecondModule,其中还有一个build.gradle文件。

The SecondModule build.gradle look like this. SecondModule build.gradle看起来像这样。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android-library'

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile 'com.android.support:support-v4:13.0.0'
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

The second module doesn't depend on anything except android support . 除了android support之外,第二个模块不依赖于任何东西。 Note that in both project, the build is using mavenLocal() and mavenCentral() for building and not just the buildscript. 请注意,在两个项目中,构建都使用mavenLocal()mavenCentral()来构建而不仅仅是构建脚本。 The difference with the default android-library build.gradle file is that here, we are loading com.android.support:support-v4:13.0.0 from maven instead of a jar file. 与默认的android-library build.gradle文件的区别在于,我们从maven而不是jar文件加载com.android.support:support-v4:13.0.0 So we can peacefully add the dependency in each module without fearing that gradle will append it n times. 因此,我们可以在每个模块中平稳地添加依赖项,而不必担心gradle会将其追加n次。 For that to work, you have to install google repository and the android support repository. 为此,您必须安装google存储库和android支持存储库。

The MainApp gradle file look like this: MainApp gradle文件如下所示:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile 'com.android.support:support-v4:13.0.0'
    compile project(':MyModule')
    compile project(':SecondModule')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

This file is applying the android plugin instead of the android-library plugin. 这个文件是应用android插件而不是android-library插件。 If everything is fine, it should compile just well and run. 如果一切都很好,它应该编译得很好并运行。 Also since the project happens to be in development, make sure you have the latest version installed and it's possible that in the future this answer gets pointless. 此外,由于项目正在开发中,请确保安装了最新版本,并且将来可能会得到毫无意义的答案。

Make sure that you never use the android support jar, but instead use the maven repository. 确保您从不使用android支持jar,而是使用maven存储库。 The downside is that it may takes a while to compile but it will just work. 缺点是它可能需要一段时间才能编译,但它只会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM