gpt4 book ai didi

android - java.lang.NoClassDefFoundError : com. acme.R$layout 引用 android 库

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:16:11 25 4
gpt4 key购买 nike

我有一个 Android 库项目,其中包含一个自定义身份验证器和一个为身份验证器提供登录屏幕的 Activity。当直接包含在我的主应用程序中时,身份验证器工作正常,但我想将身份验证器放入一个单独的 android 库中。当我运行引用这个库的主要 android 应用程序项目时,当它使用 R.layout 调用 setContentView 时,我在 Activity 的 onCreate 方法中得到一个 'java.lang.NoClassDefFoundError: com.acme.R$layout'。

我正在使用 android gradle 构建。我已经将库发布到本地 Maven 存储库,并且主项目似乎正在构建,没有任何问题。我已经在 build/outputs/apk 的调试 apk 中反编译了 class.dex 文件,我可以看到库中的 R$layout.class 文件存在,所以我完全不知道可能是什么问题.

这是实现自定义身份验证器的服务:

public class AcmeAuthenticatorService extends Service {

public static final String ACME_ACCOUNT_TYPE = "acme-oauth";
public static final String ACME_ACCESS_TOKEN_TYPE = "acme-oauth-access-token";


private static Authenticator authenticator;

private Authenticator getAuthenticator() {
if (authenticator == null)
authenticator = new Authenticator(this);
return authenticator;
}

@Override
public IBinder onBind(Intent intent) {
if (intent.getAction().equals(AccountManager.ACTION_AUTHENTICATOR_INTENT))
return getAuthenticator().getIBinder();
return null;
}

private static class Authenticator extends AbstractAccountAuthenticator {
private final Context context;

Authenticator(Context ctx) {
super(ctx);
this.context = ctx;
}

@Override
public Bundle addAccount(AccountAuthenticatorResponse response,
String accountType,
String authTokenType,
String[] requiredFeatures,
Bundle options) throws NetworkErrorException {
return makeAuthIntentBundle(response, options);
}

private Bundle makeAuthIntentBundle(AccountAuthenticatorResponse response, Bundle options) {
Bundle reply = new Bundle();
Intent i = new Intent(context, AcmeAccountAuthenticatorActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
if (options != null)
i.putExtras(options);
reply.putParcelable(AccountManager.KEY_INTENT, i);
return reply;
}

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response,
Account account,
String authTokenType,
Bundle options) throws NetworkErrorException {
return null;
}

@Override
public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
return null;
}

@Override
public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException {
return null;
}

@Override
public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
return null;
}

@Override
public String getAuthTokenLabel(String authTokenType) {
return null;
}

@Override
public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException {
return null;
}
}
}

这是处理登录的 Activity :

import com.acme.R;

public class AcmeAccountAuthenticatorActivity extends AccountAuthenticatorActivity {
private AccountManager accountManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//NoClassDefFoundError occurs for this line
setContentView(R.layout.activity_account_auth);

accountManager = AccountManager.get(this);

WebView webview = (WebView)findViewById(R.id.web_view);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
if ( webview == null ) {
Log.e("TAG", "Web view not found!!");
} else {
//Do authentication
}
}
}

这是登录 Activity 的布局 xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.acme.AcmeAccountAuthenticatorActivity"
tools:ignore="MergeRootFrame" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</FrameLayout>

在我拥有的库 list 的应用程序部分:

    <activity android:name="com.acme.AcmeAccountAuthenticatorActivity"
android:label="Login">
</activity>

<service android:name="com.acme.AcmeAuthenticatorService"
android:permission="com.acme.AUTHENTICATE_ACME_ACCOUNTS">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>
<meta-data android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator"/>
</service>

还有库的 build.gradle:

buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

version '1.0-SNAPSHOT'
group 'com.acme'

repositories {
mavenLocal()
mavenCentral()
}

android {
compileSdkVersion 19
buildToolsVersion "20.0.0"

defaultConfig {
applicationId "com.acme.test"
minSdkVersion 16
targetSdkVersion 19
}

buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-project.txt'), 'proguard-rules.txt'
}
}
}

dependencies {
compile 'com.android.support:support-v4:19.0.0'
compile 'com.android.support:appcompat-v7:19.0.0'
}

def getArtifactFullPath() {
return "${buildDir}/outputs/aar/${project.name}-${project.version}.aar"
}

android.libraryVariants
publishing {
publications {
maven(MavenPublication) {
artifact getArtifactFullPath()
}
}
}

然后在 android 应用程序中我执行以下操作来触发身份验证 Activity :

    final AccountManagerFuture<Bundle> future = accountManager.addAccount(AcmeAuthenticatorService.ACME_ACCOUNT_TYPE, AcmeAuthenticatorService.ACME_ACCESS_TOKEN_TYPE, null, null, this, new AccountManagerCallback<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> future) {
try {
Bundle bnd = future.getResult();
showMessage("Account was created");
Log.d("acme", "AddNewAccount Bundle is " + bnd);

} catch (Exception e) {
e.printStackTrace();
showMessage(e.getMessage());
}
}
}, null);

这是 android 应用程序的 build.gradle:

buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}

apply plugin: 'com.android.application'
apply plugin: 'maven'

repositories {
mavenLocal()
mavenCentral()
}

android {
compileSdkVersion 19
buildToolsVersion '20.0.0'

defaultConfig {
applicationId "com.acme.myapp"
minSdkVersion 16
targetSdkVersion 19
}

buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}

dependencies {
compile 'com.acme:acme-auth:1.0-SNAPSHOT'
compile 'com.android.support:support-v4:19.+'
compile 'com.android.support:appcompat-v7:19.+'
}

这是 logcat:

E/AndroidRuntime(27992): FATAL EXCEPTION: mainE/AndroidRuntime(27992): Process: com.acme.myapp, PID: 27992
E/AndroidRuntime(27992): java.lang.NoClassDefFoundError: com.acme.R$layout
E/AndroidRuntime(27992): at com.acme.AcmeAccountAuthenticatorActivity.onCreate(AcmeAccountAuthenticatorActivity.java:29)
E/AndroidRuntime(27992): at android.app.Activity.performCreate(Activity.java:5231)
E/AndroidRuntime(27992): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(27992): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
E/AndroidRuntime(27992): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
E/AndroidRuntime(27992): at android.app.ActivityThread.access$800(ActivityThread.java:135)
E/AndroidRuntime(27992): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
E/AndroidRuntime(27992): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(27992): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(27992): at android.app.ActivityThread.main(ActivityThread.java:5001)
E/AndroidRuntime(27992): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(27992): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(27992): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
E/AndroidRuntime(27992): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
E/AndroidRuntime(27992): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 575): Force finishing activity com.acme.myapp/com.acme.AcmeAccountAuthenticatorActivity
W/ActivityManager( 575): Force finishing activity com.acme.myapp/.MainActivity
W/ActivityManager( 575): Activity pause timeout for ActivityRecord{42071a70 u0 com.acme.myapp/com.acme.AcmeAccountAuthenticatorActivity t75 f}

更新

我创建了一个 sample project这证明了 github 上的问题。有关构建和重现错误的说明,请参阅自述文件。

最佳答案

只要当前 Android-Gradle Plugin(版本 0.12.2)中的 Bug 未修复,解决问题的唯一方法是从 build 中删除 applicationId .gradle

一旦这个错误被修复,我会更新这篇文章

更新:当前版本0.13.3仍然报同样的错误

关于android - java.lang.NoClassDefFoundError : com. acme.R$layout 引用 android 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25194024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com