apk文件不包含java文件

时间:2017-04-15 07:54:51

标签: android gradle

我收到以下错误

E/AndroidRuntime( 3069): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{fun.n.games/fun.n.games.HelloActivity}: java.lang.ClassNotFoundException: Didn't find class "fun.n.games.HelloActivity" on path: DexPathList[[zip file "/data/app/fun.n.games-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]

当我尝试执行我在AVD上安装的Android应用程序时出现此错误。

我的AVD运行如下

$ emulator -avd Nexus_S_API_22

存在HelloActivity的代码。

package fun.n.games;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hello_layout);
    }

    @Override
    public void onStart() {
        super.onStart();
        TextView textView = (TextView) findViewById(R.id.text_view);
        textView.setText("Hello world!");
    }

}

它被以下清单文件拉到一起。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="fun.n.games"
    android:versionCode="1"
    android:versionName="1.0.0" >


<application android:label="@string/app_name" >
        <activity
            android:name="fun.n.games.HelloActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是由一个build.gradle构建的

buildscript {

// use the jcenter repository to download the dependencies
    repositories {
        jcenter()
    }

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


apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
}

我在命令提示符下使用gradle来构建和安装

$ gradle installDebug

除了帖子开头的错误,当我搜索构建文件夹时,我没有看到我期待的HelloActivity.class。

如何将java类添加到apk文件中?

2 个答案:

答案 0 :(得分:1)

android:name="fun.n.games.HelloActivity"替换为android:name=".HelloActivity"

答案 1 :(得分:0)

Gradle确实没有找到要编译的java文件。原因是我的java文件位于错误的位置。

位置不正确

/src/main/fun/n/games/HelloActivity.java

正确的位置

/ SRC /主/的的java /fun/n/games/HelloActivity.java

缺少 java 文件夹,因此gradle没有找到要编译的类。