【问题标题】:apk file does not contain the java fileapk 文件不包含 java 文件
【发布时间】:2017-04-15 07:54:50
【问题描述】:

我收到以下错误

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 文件中?

【问题讨论】:

  • 尝试禁用即时运行
  • 我没有使用 AndroidStudio。如何从命令提示符禁用即时运行?

标签: android gradle


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    位置不正确

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

    正确的位置

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

    java 文件夹丢失,因此 gradle 找不到要编译的类。

    【讨论】:

      猜你喜欢
      • 2012-06-11
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      相关资源
      最近更新 更多